-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain_test.go
More file actions
126 lines (116 loc) · 2.86 KB
/
main_test.go
File metadata and controls
126 lines (116 loc) · 2.86 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
126
package main
import (
"testing"
"github.com/spf13/cobra"
)
// buildCommandHierarchy creates a cobra command hierarchy from a path like
// ["talm", "completion", "bash"] and returns the leaf command.
func buildCommandHierarchy(path []string) *cobra.Command {
if len(path) == 0 {
return nil
}
root := &cobra.Command{Use: path[0]}
parent := root
for _, name := range path[1:] {
child := &cobra.Command{Use: name}
parent.AddCommand(child)
parent = child
}
return parent
}
func TestIsCommandOrParent(t *testing.T) {
tests := []struct {
name string
cmdPath []string
names []string
expected bool
}{
{
name: "direct completion command",
cmdPath: []string{"talm", "completion"},
names: []string{"init", "completion"},
expected: true,
},
{
name: "completion bash subcommand",
cmdPath: []string{"talm", "completion", "bash"},
names: []string{"init", "completion"},
expected: true,
},
{
name: "init command",
cmdPath: []string{"talm", "init"},
names: []string{"init", "completion"},
expected: true,
},
{
name: "apply command should not match",
cmdPath: []string{"talm", "apply"},
names: []string{"init", "completion"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
leaf := buildCommandHierarchy(tt.cmdPath)
result := isCommandOrParent(leaf, tt.names...)
if result != tt.expected {
t.Errorf("isCommandOrParent() = %v, want %v", result, tt.expected)
}
})
}
}
func TestSkipConfigCommands(t *testing.T) {
tests := []struct {
name string
cmdPath []string
expected bool // true = should skip config loading
}{
{
name: "completion command",
cmdPath: []string{"talm", "completion"},
expected: true,
},
{
name: "completion bash",
cmdPath: []string{"talm", "completion", "bash"},
expected: true,
},
{
name: "completion zsh",
cmdPath: []string{"talm", "completion", "zsh"},
expected: true,
},
{
name: "__complete (cobra internal for shell autocompletion)",
cmdPath: []string{"talm", "__complete"},
expected: true,
},
{
name: "init command",
cmdPath: []string{"talm", "init"},
expected: true,
},
{
name: "apply command should load config",
cmdPath: []string{"talm", "apply"},
expected: false,
},
{
name: "template command should load config",
cmdPath: []string{"talm", "template"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
leaf := buildCommandHierarchy(tt.cmdPath)
// This uses the actual skipConfigCommands from main.go
result := isCommandOrParent(leaf, skipConfigCommands...)
if result != tt.expected {
t.Errorf("skipConfigCommands check = %v, want %v (skipConfigCommands = %v)",
result, tt.expected, skipConfigCommands)
}
})
}
}