-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle_test.go
More file actions
247 lines (215 loc) · 6 KB
/
lifecycle_test.go
File metadata and controls
247 lines (215 loc) · 6 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package cli
import (
"context"
"testing"
)
// TestLifecycleHooksOrder tests that hooks execute in correct order
func TestLifecycleHooksOrder(t *testing.T) {
order := []string{}
cmd := Root("test").
PersistentPreRun(func(ctx context.Context, c *Command) error {
order = append(order, "persistent-pre")
return nil
}).
PreRun(func(ctx context.Context, c *Command) error {
order = append(order, "pre")
return nil
}).
Action(func(ctx context.Context, c *Command) error {
order = append(order, "action")
return nil
}).
PostRun(func(ctx context.Context, c *Command) error {
order = append(order, "post")
return nil
}).
PersistentPostRun(func(ctx context.Context, c *Command) error {
order = append(order, "persistent-post")
return nil
})
err := cmd.ExecuteWithArgs([]string{})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
expected := []string{"persistent-pre", "pre", "action", "post", "persistent-post"}
if len(order) != len(expected) {
t.Fatalf("expected %d hooks, got %d", len(expected), len(order))
}
for i, exp := range expected {
if order[i] != exp {
t.Errorf("position %d: expected %s, got %s", i, exp, order[i])
}
}
}
// TestLifecycleErrorInPreRun tests that error in PreRun stops execution
func TestLifecycleErrorInPreRun(t *testing.T) {
actionExecuted := false
postExecuted := false
cmd := Root("test").
PreRun(func(ctx context.Context, c *Command) error {
return &ArgumentError{Arg: "test", Msg: "validation failed", Cmd: c}
}).
Action(func(ctx context.Context, c *Command) error {
actionExecuted = true
return nil
}).
PostRun(func(ctx context.Context, c *Command) error {
postExecuted = true
return nil
})
err := cmd.Execute()
if err == nil {
t.Fatal("expected error from PreRun")
}
if actionExecuted {
t.Error("action should not execute after PreRun error")
}
if postExecuted {
t.Error("PostRun should not execute after PreRun error")
}
}
// TestLifecycleErrorInAction tests that error in Action still runs PostRun
func TestLifecycleErrorInAction(t *testing.T) {
postExecuted := false
persistentPostExecuted := false
cmd := Root("test").
Action(func(ctx context.Context, c *Command) error {
return &ArgumentError{Arg: "test", Msg: "action error", Cmd: c}
}).
PostRun(func(ctx context.Context, c *Command) error {
postExecuted = true
return nil
}).
PersistentPostRun(func(ctx context.Context, c *Command) error {
persistentPostExecuted = true
return nil
})
err := cmd.ExecuteWithArgs([]string{})
if err == nil {
t.Fatal("expected error from Action")
}
if !postExecuted {
t.Error("PostRun should execute even after Action error")
}
if !persistentPostExecuted {
t.Error("PersistentPostRun should execute even after Action error")
}
}
// TestPersistentHooksInheritance tests persistent hooks run for child commands
func TestPersistentHooksInheritance(t *testing.T) {
order := []string{}
root := Root("app").
PersistentPreRun(func(ctx context.Context, c *Command) error {
order = append(order, "root-persistent-pre")
return nil
}).
PersistentPostRun(func(ctx context.Context, c *Command) error {
order = append(order, "root-persistent-post")
return nil
})
child := Cmd("child").
PreRun(func(ctx context.Context, c *Command) error {
order = append(order, "child-pre")
return nil
}).
Action(func(ctx context.Context, c *Command) error {
order = append(order, "child-action")
return nil
}).
PostRun(func(ctx context.Context, c *Command) error {
order = append(order, "child-post")
return nil
})
root.AddCommand(child)
err := root.ExecuteWithArgs([]string{"child"})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
expected := []string{
"root-persistent-pre",
"child-pre",
"child-action",
"child-post",
"root-persistent-post",
}
if len(order) != len(expected) {
t.Fatalf("expected %d hooks, got %d: %v", len(expected), len(order), order)
}
for i, exp := range expected {
if order[i] != exp {
t.Errorf("position %d: expected %s, got %s", i, exp, order[i])
}
}
}
// TestLifecycleNoAction tests execution without Action function
func TestLifecycleNoAction(t *testing.T) {
preExecuted := false
postExecuted := false
cmd := Root("test").
PreRun(func(ctx context.Context, c *Command) error {
preExecuted = true
return nil
}).
PostRun(func(ctx context.Context, c *Command) error {
postExecuted = true
return nil
})
err := cmd.ExecuteWithArgs([]string{})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
if !preExecuted {
t.Error("PreRun should execute")
}
if !postExecuted {
t.Error("PostRun should execute")
}
}
// TestLifecycleMultiplePersistentLevels tests nested persistent hooks
func TestLifecycleMultiplePersistentLevels(t *testing.T) {
order := []string{}
root := Root("app").
PersistentPreRun(func(ctx context.Context, c *Command) error {
order = append(order, "root-persistent-pre")
return nil
}).
PersistentPostRun(func(ctx context.Context, c *Command) error {
order = append(order, "root-persistent-post")
return nil
})
mid := Cmd("mid").
PersistentPreRun(func(ctx context.Context, c *Command) error {
order = append(order, "mid-persistent-pre")
return nil
}).
PersistentPostRun(func(ctx context.Context, c *Command) error {
order = append(order, "mid-persistent-post")
return nil
})
leaf := Cmd("leaf").
Action(func(ctx context.Context, c *Command) error {
order = append(order, "leaf-action")
return nil
})
root.AddCommand(mid)
mid.AddCommand(leaf)
err := root.ExecuteWithArgs([]string{"mid", "leaf"})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
expected := []string{
"root-persistent-pre",
"mid-persistent-pre",
"leaf-action",
"mid-persistent-post",
"root-persistent-post",
}
if len(order) != len(expected) {
t.Fatalf("expected %d hooks, got %d: %v", len(expected), len(order), order)
}
for i, exp := range expected {
if order[i] != exp {
t.Errorf("position %d: expected %s, got %s", i, exp, order[i])
}
}
}