Skip to content

Commit

Permalink
test: added some test for the docs command
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Sep 5, 2024
1 parent 74d0ce1 commit e54f774
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions cmd/docs/docs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package docs

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewDocsCommand(t *testing.T) {
t.Parallel()

t.Run("Docs generate using default path", func(t *testing.T) {
t.Parallel()
cmd := NewDocsCommand()
err := cmd.Execute()
require.NoError(t, err)

// Check if a Markdown file was generated
files, err := os.ReadDir(DefaultPath)
assert.NoError(t, err)
assert.NotEmpty(t, files)

markdownFileFound := false

for _, file := range files {
if strings.HasSuffix(file.Name(), ".md") {
markdownFileFound = true
break
}
}

assert.True(t, markdownFileFound, "No Markdown file was generated in the default path")

os.RemoveAll(DefaultPath)
})

t.Run("Docs generate using a custom path", func(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
customPath := filepath.Join(tempDir, "custom_docs")

cmd := NewDocsCommand()
cmd.SetArgs([]string{customPath})
err := cmd.Execute()
require.NoError(t, err)

// Check if a Markdown file was generated
files, err := os.ReadDir(customPath)
assert.NoError(t, err)
assert.NotEmpty(t, files)

markdownFileFound := false

for _, file := range files {
if strings.HasSuffix(file.Name(), ".md") {
markdownFileFound = true
break
}
}

assert.True(t, markdownFileFound, "No Markdown file was generated in the custom path")
})

t.Run("Docs fail to generate when the output path is invalid", func(t *testing.T) {
t.Parallel()
cmd := NewDocsCommand()
cmd.SetArgs([]string{string([]byte{0x00})})
err := cmd.Execute()
assert.Error(t, err)
})

t.Run("Docs generate using an existing directory", func(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()

cmd := NewDocsCommand()
cmd.SetArgs([]string{tempDir})
err := cmd.Execute()
require.NoError(t, err)

// Check if files were generated in the existing directory
files, err := os.ReadDir(tempDir)
assert.NoError(t, err)
assert.NotEmpty(t, files)

markdownFileFound := false
for _, file := range files {
if strings.HasSuffix(file.Name(), ".md") {
markdownFileFound = true
break
}
}
assert.True(t, markdownFileFound, "No Markdown file was generated in the existing directory")
})

t.Run("TooManyArguments", func(t *testing.T) {
t.Parallel()
cmd := NewDocsCommand()
cmd.SetArgs([]string{"path1", "path2"})
err := cmd.Execute()
assert.Error(t, err)
})
}

0 comments on commit e54f774

Please sign in to comment.