-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
185 lines (162 loc) · 4.4 KB
/
command_test.go
File metadata and controls
185 lines (162 loc) · 4.4 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cli
import (
"context"
"testing"
)
// TestCommandCreation tests basic command creation
func TestCommandCreation(t *testing.T) {
tests := []struct {
name string
createCmd func() *Command
expectName string
expectDesc string
expectHidden bool
}{
{
name: "root command",
createCmd: func() *Command {
return Root("myapp").Description("My application")
},
expectName: "myapp",
expectDesc: "My application",
expectHidden: false,
},
{
name: "regular command",
createCmd: func() *Command {
return Cmd("deploy").Description("Deploy app").Hidden()
},
expectName: "deploy",
expectDesc: "Deploy app",
expectHidden: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := tt.createCmd()
if cmd.GetName() != tt.expectName {
t.Errorf("expected name %q, got %q", tt.expectName, cmd.GetName())
}
if cmd.GetDescription() != tt.expectDesc {
t.Errorf("expected description %q, got %q", tt.expectDesc, cmd.GetDescription())
}
if cmd.IsHidden() != tt.expectHidden {
t.Errorf("expected hidden %v, got %v", tt.expectHidden, cmd.IsHidden())
}
})
}
}
// TestCommandHierarchy tests parent-child relationships
func TestCommandHierarchy(t *testing.T) {
root := Root("app")
child := Cmd("child")
grandchild := Cmd("grandchild")
root.AddCommand(child)
child.AddCommand(grandchild)
if child.GetParent() != root {
t.Error("child parent should be root")
}
if grandchild.GetParent() != child {
t.Error("grandchild parent should be child")
}
rootCommands := root.GetCommands()
if len(rootCommands) != 1 {
t.Error("root should have 1 child command")
}
if rootCommands["child"] != child {
t.Error("root's child should be the child command")
}
childCommands := child.GetCommands()
if len(childCommands) != 1 {
t.Error("child should have 1 grandchild command")
}
if childCommands["grandchild"] != grandchild {
t.Error("child's grandchild should be the grandchild command")
}
}
// TestCommandArguments tests argument configuration
func TestCommandArguments(t *testing.T) {
cmd := Cmd("test").
Arg("name", "User name", true).
Arg("age", "User age", false)
args := cmd.GetArgs()
if len(args) != 2 {
t.Fatalf("expected 2 args, got %d", len(args))
}
if args[0].Name != "name" || !args[0].Required {
t.Error("first arg should be required 'name'")
}
if args[1].Name != "age" || args[1].Required {
t.Error("second arg should be optional 'age'")
}
}
// TestCommandAction tests action function setting
func TestCommandAction(t *testing.T) {
executed := false
cmd := Root("test").
Action(func(ctx context.Context, cmd *Command) error {
executed = true
return nil
})
if err := cmd.ExecuteWithArgs([]string{}); err != nil {
t.Fatalf("execution failed: %v", err)
}
if !executed {
t.Error("action was not executed")
}
}
// TestCommandHiddenSubcommands tests that hidden commands are excluded from GetCommands
func TestCommandHiddenSubcommands(t *testing.T) {
root := Root("app")
visible := Cmd("visible")
hidden := Cmd("hidden").Hidden()
root.AddCommand(visible)
root.AddCommand(hidden)
commands := root.GetCommands()
// GetCommands should return all commands, including hidden ones
// (filtering happens at display time, not retrieval)
if len(commands) != 2 {
t.Errorf("expected 2 commands, got %d", len(commands))
}
}
// TestMultipleActionsOnSameCommand tests that only the last action is kept
func TestMultipleActionsOnSameCommand(t *testing.T) {
counter := 0
cmd := Root("test").
Action(func(ctx context.Context, cmd *Command) error {
counter = 1
return nil
}).
Action(func(ctx context.Context, cmd *Command) error {
counter = 2
return nil
})
if err := cmd.ExecuteWithArgs([]string{}); err != nil {
t.Fatalf("execution failed: %v", err)
}
if counter != 2 {
t.Errorf("expected counter=2 (last action), got %d", counter)
}
}
// TestCommandChaining tests fluent API chaining
func TestCommandChaining(t *testing.T) {
cmd := Root("app").
Description("Test app").
Arg("name", "Name arg", true).
Hidden().
Action(func(ctx context.Context, c *Command) error {
return nil
})
if cmd.GetName() != "app" {
t.Error("chaining broke name")
}
if cmd.GetDescription() != "Test app" {
t.Error("chaining broke description")
}
if !cmd.IsHidden() {
t.Error("chaining broke hidden")
}
if len(cmd.GetArgs()) != 1 {
t.Error("chaining broke args")
}
}