Passed
Pull Request — master (#52)
by Stefano
02:15
created

alidDirectory   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 1
dl 0
loc 13
rs 9.9
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, 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 TestDictionaryGenerateShouldFailWhenAFilePathIsProvidedInsteadOfADirectory(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
	testFilePath := "testdata/" + test.RandStringRunes(10)
39
	defer removeTestFile(testFilePath)
40
	_, _, err = executeCommand(c, "dictionary.generate", "./root_integration_test.go")
41
	assert.Error(t, err)
42
43
	assert.Contains(t, err.Error(), "the path should be a directory")
44
}
45
46
func TestGenerateDictionaryWithoutOutputPath(t *testing.T) {
47
	logger, loggerBuffer := test.NewLogger()
48
49
	c, err := createCommand(logger)
50
	assert.NoError(t, err)
51
	assert.NotNil(t, c)
52
53
	_, _, err = executeCommand(c, "dictionary.generate", ".")
54
	assert.NoError(t, err)
55
56
	assert.Contains(t, loggerBuffer.String(), "root_integration_test.go")
57
}
58
59
func TestGenerateDictionaryWithInvalidDirectory(t *testing.T) {
60
	logger, _ := test.NewLogger()
61
62
	fakePath := "./" + test.RandStringRunes(10)
63
	c, err := createCommand(logger)
64
	assert.NoError(t, err)
65
	assert.NotNil(t, c)
66
67
	_, _, err = executeCommand(c, "dictionary.generate", fakePath)
68
	assert.Error(t, err)
69
70
	assert.Contains(t, err.Error(), "unable to use the provided path")
71
	assert.Contains(t, err.Error(), fakePath)
72
}
73