-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added some test for the docs command
- Loading branch information
1 parent
74d0ce1
commit e54f774
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |