Passed
Push — master ( 23e371...7b0ca1 )
by Stefano
02:15
created

cmd_test.createCommand   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
package cmd_test
2
3
import (
4
	"bytes"
5
	"os"
6
	"strings"
7
	"testing"
8
9
	"github.com/sirupsen/logrus"
10
	"github.com/spf13/cobra"
11
	"github.com/stefanoj3/dirstalk/pkg/cmd"
12
	"github.com/stefanoj3/dirstalk/pkg/common/test"
13
	"github.com/stretchr/testify/assert"
14
)
15
16
func TestRootCommand(t *testing.T) {
17
	logger, _ := test.NewLogger()
18
19
	c := createCommand(logger)
20
	assert.NotNil(t, c)
21
22
	err := executeCommand(c)
23
	assert.NoError(t, err)
24
}
25
26
func TestVersionCommand(t *testing.T) {
27
	logger, buf := test.NewLogger()
28
29
	c := createCommand(logger)
30
	assert.NotNil(t, c)
31
32
	err := executeCommand(c, "version")
33
	assert.NoError(t, err)
34
35
	// Ensure the command ran and produced some of the expected output
36
	// it is not in the scope of this test to ensure the correct output
37
	assert.Contains(t, buf.String(), "Version: ")
38
}
39
40
func executeCommand(root *cobra.Command, args ...string) (err error) {
41
	buf := new(bytes.Buffer)
42
	root.SetOut(buf)
43
44
	a := []string{""}
45
	os.Args = append(a, args...) //nolint
46
47
	_, err = root.ExecuteC()
48
49
	return err
50
}
51
52
func removeTestFile(path string) {
53
	if !strings.Contains(path, "testdata") {
54
		return
55
	}
56
57
	_ = os.Remove(path)
58
}
59
60
func createCommand(logger *logrus.Logger) *cobra.Command {
61
	dirStalkCmd := cmd.NewRootCommand(logger)
62
63
	dirStalkCmd.AddCommand(cmd.NewScanCommand(logger))
64
	dirStalkCmd.AddCommand(cmd.NewResultViewCommand(logger.Out))
65
	dirStalkCmd.AddCommand(cmd.NewResultDiffCommand(logger.Out))
66
	dirStalkCmd.AddCommand(cmd.NewGenerateDictionaryCommand(logger.Out))
67
	dirStalkCmd.AddCommand(cmd.NewVersionCommand(logger.Out))
68
69
	return dirStalkCmd
70
}
71