1
|
|
|
package cmd_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"io/ioutil" |
5
|
|
|
"testing" |
6
|
|
|
|
7
|
|
|
"github.com/stefanoj3/dirstalk/pkg/common/test" |
8
|
|
|
"github.com/stretchr/testify/assert" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
func TestDictionaryGenerateCommand(t *testing.T) { |
12
|
|
|
logger, _ := test.NewLogger() |
13
|
|
|
|
14
|
|
|
c, err := createCommand(logger) |
15
|
|
|
assert.NoError(t, err) |
16
|
|
|
assert.NotNil(t, c) |
17
|
|
|
|
18
|
|
|
testFilePath := "testdata/" + test.RandStringRunes(10) |
19
|
|
|
defer removeTestFile(testFilePath) |
20
|
|
|
_, _, err = executeCommand(c, "dictionary.generate", ".", "-o", testFilePath) |
21
|
|
|
assert.NoError(t, err) |
22
|
|
|
|
23
|
|
|
content, err := ioutil.ReadFile(testFilePath) |
24
|
|
|
assert.NoError(t, err) |
25
|
|
|
|
26
|
|
|
// Ensure the command ran and produced some of the expected output |
27
|
|
|
// it is not in the scope of this test to ensure the correct output |
28
|
|
|
assert.Contains(t, string(content), "root_integration_test.go") |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
func TestDictionaryGenerateCommandShouldErrWhenNoTargetIsProvided(t *testing.T) { |
32
|
|
|
logger, _ := test.NewLogger() |
33
|
|
|
|
34
|
|
|
c, err := createCommand(logger) |
35
|
|
|
assert.NoError(t, err) |
36
|
|
|
assert.NotNil(t, c) |
37
|
|
|
|
38
|
|
|
_, _, err = executeCommand(c, "dictionary.generate") |
39
|
|
|
assert.Error(t, err) |
40
|
|
|
|
41
|
|
|
assert.Contains(t, err.Error(), "no path provided") |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func TestDictionaryGenerateShouldFailWhenAFilePathIsProvidedInsteadOfADirectory(t *testing.T) { |
45
|
|
|
logger, _ := test.NewLogger() |
46
|
|
|
|
47
|
|
|
c, err := createCommand(logger) |
48
|
|
|
assert.NoError(t, err) |
49
|
|
|
assert.NotNil(t, c) |
50
|
|
|
|
51
|
|
|
testFilePath := "testdata/" + test.RandStringRunes(10) |
52
|
|
|
defer removeTestFile(testFilePath) |
53
|
|
|
_, _, err = executeCommand(c, "dictionary.generate", "./root_integration_test.go") |
54
|
|
|
assert.Error(t, err) |
55
|
|
|
|
56
|
|
|
assert.Contains(t, err.Error(), "the path should be a directory") |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
func TestGenerateDictionaryWithoutOutputPath(t *testing.T) { |
60
|
|
|
logger, loggerBuffer := test.NewLogger() |
61
|
|
|
|
62
|
|
|
c, err := createCommand(logger) |
63
|
|
|
assert.NoError(t, err) |
64
|
|
|
assert.NotNil(t, c) |
65
|
|
|
|
66
|
|
|
_, _, err = executeCommand(c, "dictionary.generate", ".") |
67
|
|
|
assert.NoError(t, err) |
68
|
|
|
|
69
|
|
|
assert.Contains(t, loggerBuffer.String(), "root_integration_test.go") |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
func TestGenerateDictionaryWithInvalidDirectory(t *testing.T) { |
73
|
|
|
logger, _ := test.NewLogger() |
74
|
|
|
|
75
|
|
|
fakePath := "./" + test.RandStringRunes(10) |
76
|
|
|
c, err := createCommand(logger) |
77
|
|
|
assert.NoError(t, err) |
78
|
|
|
assert.NotNil(t, c) |
79
|
|
|
|
80
|
|
|
_, _, err = executeCommand(c, "dictionary.generate", fakePath) |
81
|
|
|
assert.Error(t, err) |
82
|
|
|
|
83
|
|
|
assert.Contains(t, err.Error(), "unable to use the provided path") |
84
|
|
|
assert.Contains(t, err.Error(), fakePath) |
85
|
|
|
} |
86
|
|
|
|