-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
125 lines (101 loc) · 2.87 KB
/
command_test.go
File metadata and controls
125 lines (101 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cobi
import (
"bytes"
"fmt"
"strings"
"testing"
cobiEditor "github.com/arjit95/cobi/editor"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
var root *Command
type scenario struct {
command string
expectedSuggestion []string
}
func init() {
editor := cobiEditor.NewEditor()
editor.SetUpperPaneTitle("Test")
editor.SetLowerPaneTitle("Logs")
root = NewCommand(editor, &cobra.Command{
Use: "cobi-test",
Short: "Test cases for cobi",
})
testCommands := []*cobra.Command{
{
Use: "test1",
Short: "Description for test1",
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Test1 args %v\n", args)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"Suggestion1", "DiffSuggestion"}, cobra.ShellCompDirectiveNoFileComp
},
},
{
Use: "test2",
Run: func(cmd *cobra.Command, args []string) {
},
},
}
testCommands[1].Flags().BoolP("debug", "d", false, "Testing debug flag")
testCommands[1].Flags().String("namespace", "", "Select namespace")
testCommands[1].RegisterFlagCompletionFunc("namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"ns1", "ns2"}, cobra.ShellCompDirectiveNoFileComp
})
testCommands[1].AddCommand(&cobra.Command{
Use: "deep",
Short: "Nested command for test2",
Run: func(cmd *cobra.Command, args []string) {},
})
for _, cmd := range testCommands {
root.AddCommand(cmd)
}
}
func runScenarios(t *testing.T, scenarios []scenario) {
for _, s := range scenarios {
suggestions := root.generateSuggestions(s.command)
assert.Equal(t, len(s.expectedSuggestion), len(suggestions))
assert.EqualValues(t, s.expectedSuggestion, suggestions)
}
}
func execCommand(cmd *Command, in string) string {
buffer := &bytes.Buffer{}
bOut := root.OutOrStdout()
bErr := root.OutOrStderr()
root.SetOut(buffer)
root.SetErr(buffer)
err := root.execute(in)
root.SetOut(bOut)
root.SetErr(bErr)
if err != nil {
return err.Error()
}
return buffer.String()
}
func TestNumCommands(t *testing.T) {
var visibleCmds []string
for _, cmd := range root.Commands() {
if strings.Index(cmd.Use, "__") != 0 { // Hide complete commands
visibleCmds = append(visibleCmds, cmd.Use)
}
}
assert.Equal(t, 2, len(visibleCmds))
}
func TestInvalidShellCommand(t *testing.T) {
scenarioTable := []scenario{
{
command: `test "S`,
expectedSuggestion: nil,
},
}
runScenarios(t, scenarioTable)
}
func TestSubCommand(t *testing.T) {
usage := execCommand(root, "test1 --help")
test1Desc := "Description for test1"
assert.Equal(t, true, strings.Index(usage, test1Desc) >= 0)
invalid := execCommand(root, `test1 "asdasd --asdgdg`)
assert.Equal(t, true, strings.Index(invalid, "EOF") >= 0)
}