cmd_test.TestGenerateDictionaryWithoutOutputPath   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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 := createCommand(logger)
15
	assert.NotNil(t, c)
16
17
	testFilePath := "testdata/" + test.RandStringRunes(10)
18
	defer removeTestFile(testFilePath)
19
	err := executeCommand(c, "dictionary.generate", ".", "-o", testFilePath)
20
	assert.NoError(t, err)
21
22
	//nolint:gosec
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 := createCommand(logger)
35
	assert.NotNil(t, c)
36
37
	err := executeCommand(c, "dictionary.generate")
38
	assert.Error(t, err)
39
40
	assert.Contains(t, err.Error(), "no path provided")
41
}
42
43
func TestDictionaryGenerateShouldFailWhenAFilePathIsProvidedInsteadOfADirectory(t *testing.T) {
44
	logger, _ := test.NewLogger()
45
46
	c := createCommand(logger)
47
	assert.NotNil(t, c)
48
49
	testFilePath := "testdata/" + test.RandStringRunes(10)
50
	defer removeTestFile(testFilePath)
51
52
	err := executeCommand(c, "dictionary.generate", "./root_integration_test.go")
53
	assert.Error(t, err)
54
55
	assert.Contains(t, err.Error(), "the path should be a directory")
56
}
57
58
func TestGenerateDictionaryWithoutOutputPath(t *testing.T) {
59
	logger, loggerBuffer := test.NewLogger()
60
61
	c := createCommand(logger)
62
	assert.NotNil(t, c)
63
64
	err := executeCommand(c, "dictionary.generate", ".")
65
	assert.NoError(t, err)
66
67
	assert.Contains(t, loggerBuffer.String(), "root_integration_test.go")
68
}
69
70
func TestGenerateDictionaryWithInvalidDirectory(t *testing.T) {
71
	logger, _ := test.NewLogger()
72
73
	fakePath := "./" + test.RandStringRunes(10)
74
	c := createCommand(logger)
75
	assert.NotNil(t, c)
76
77
	err := executeCommand(c, "dictionary.generate", fakePath)
78
	assert.Error(t, err)
79
80
	assert.Contains(t, err.Error(), "unable to use the provided path")
81
	assert.Contains(t, err.Error(), fakePath)
82
}
83