-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_test.go
More file actions
176 lines (158 loc) · 4.66 KB
/
Copy pathrun_test.go
File metadata and controls
176 lines (158 loc) · 4.66 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
package cli
import (
"bytes"
"context"
"errors"
"strings"
"testing"
)
func TestExecuteSubcommand(t *testing.T) {
root := NewCommand("root", "", "")
var subCalled bool
sub := NewCommand("sub", "", "")
sub.Run = func(c *Command, args []string) error {
subCalled = true
return nil
}
root.AddCommand(sub)
err := root.ExecuteContext(context.Background(), []string{"sub"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !subCalled {
t.Error("subcommand Run not called")
}
}
func TestExecuteUnknownCommand(t *testing.T) {
root := NewCommand("root", "", "")
root.Run = func(c *Command, args []string) error { return nil }
root.DisableSuggestions = true
err := root.ExecuteContext(context.Background(), []string{"unknown"})
if err == nil || !strings.Contains(err.Error(), "unknown command") {
t.Errorf("expected unknown command error, got %v", err)
}
}
func TestExecuteSuggestions(t *testing.T) {
root := NewCommand("root", "", "")
sub := NewCommand("serve", "", "")
root.AddCommand(sub)
root.SuggestionsMinimumDistance = 2
err := root.ExecuteContext(context.Background(), []string{"server"})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "Did you mean") || !strings.Contains(err.Error(), "serve") {
t.Errorf("suggestion missing: %v", err)
}
}
func TestExecuteTraverseChildren(t *testing.T) {
root := NewCommand("root", "", "")
var flagParsed bool
root.Flags().Bool("global", false, "")
root.TraverseChildren = true
sub := NewCommand("sub", "", "")
sub.Run = func(c *Command, args []string) error {
// Проверяем флаг через родителя
globalFlag := c.Parent().Flags().Lookup("global")
if globalFlag == nil || globalFlag.Value.String() != "true" {
t.Error("global flag not parsed")
}
flagParsed = true
return nil
}
root.AddCommand(sub)
err := root.ExecuteContext(context.Background(), []string{"--global", "sub"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !flagParsed {
t.Error("subcommand not executed")
}
}
func TestExecuteDisableFlagParsing(t *testing.T) {
root := NewCommand("root", "", "")
root.children = []*Command{}
root.childrenMap = nil
var args []string
root.Run = func(c *Command, a []string) error {
args = a
return nil
}
root.DisableFlagParsing = true
err := root.ExecuteContext(context.Background(), []string{"--foo", "bar"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(args) != 2 || args[0] != "--foo" || args[1] != "bar" {
t.Errorf("expected args [--foo bar], got %v", args)
}
}
func TestHooks(t *testing.T) {
var order []string
root := NewCommand("root", "", "")
root.PersistentPreRunE = func(c *Command, args []string) error {
order = append(order, "rootPersistentPre")
return nil
}
root.PersistentPostRunE = func(c *Command, args []string) error {
order = append(order, "rootPersistentPost")
return nil
}
sub := NewCommand("sub", "", "")
sub.PreRunE = func(c *Command, args []string) error {
order = append(order, "subPre")
return nil
}
sub.PostRunE = func(c *Command, args []string) error {
order = append(order, "subPost")
return nil
}
sub.Run = func(c *Command, args []string) error {
order = append(order, "subRun")
return nil
}
root.AddCommand(sub)
err := root.ExecuteContext(context.Background(), []string{"sub"})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
expected := []string{"rootPersistentPre", "subPre", "subRun", "subPost", "rootPersistentPost"}
if len(order) != len(expected) {
t.Fatalf("expected %v, got %v", expected, order)
}
for i := range order {
if order[i] != expected[i] {
t.Errorf("hook order mismatch at %d: expected %q, got %q", i, expected[i], order[i])
}
}
}
func TestHooksError(t *testing.T) {
root := NewCommand("root", "", "")
root.PersistentPreRunE = func(c *Command, args []string) error {
return errors.New("hook error")
}
sub := NewCommand("sub", "", "")
sub.Run = func(c *Command, args []string) error { return nil }
root.AddCommand(sub)
err := root.ExecuteContext(context.Background(), []string{"sub"})
if err == nil || err.Error() != "hook error" {
t.Errorf("expected hook error, got %v", err)
}
}
func TestSilenceErrors(t *testing.T) {
cmd := NewCommand("test", "", "")
cmd.Flags().Bool("foo", false, "")
cmd.Run = func(c *Command, args []string) error { return errors.New("runtime error") }
cmd.SilenceErrors = true
oldErr := cmd.errWriter
buf := &bytes.Buffer{}
cmd.errWriter = buf
defer func() { cmd.errWriter = oldErr }()
err := cmd.ExecuteContext(context.Background(), []string{})
if err == nil {
t.Error("expected error")
}
if buf.Len() > 0 {
t.Errorf("expected no error output, got %q", buf.String())
}
}