-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_test.go
More file actions
231 lines (214 loc) · 6.03 KB
/
run_test.go
File metadata and controls
231 lines (214 loc) · 6.03 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
package cli
import (
"bytes"
"context"
"flag"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
t.Parallel()
t.Run("print version", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "printer",
Usage: "printer [flags] [command]",
SubCommands: []*Command{
{
Name: "version",
Usage: "show version",
Exec: func(ctx context.Context, s *State) error {
_, _ = s.Stdout.Write([]byte("1.0.0\n"))
return nil
},
},
},
Exec: func(ctx context.Context, s *State) error { return nil },
}
err := Parse(root, []string{"version"})
require.NoError(t, err)
output := bytes.NewBuffer(nil)
require.NoError(t, err)
err = Run(context.Background(), root, &RunOptions{Stdout: output})
require.NoError(t, err)
require.Equal(t, "1.0.0\n", output.String())
})
t.Run("parse and run", func(t *testing.T) {
t.Parallel()
var count int
root := &Command{
Name: "count",
Usage: "count [flags] [command]",
Flags: FlagsFunc(func(f *flag.FlagSet) {
f.Bool("dry-run", false, "dry run")
}),
Exec: func(ctx context.Context, s *State) error {
if !GetFlag[bool](s, "dry-run") {
count++
}
return nil
},
}
err := Parse(root, nil)
require.NoError(t, err)
// Run the command 3 times
for i := 0; i < 3; i++ {
err := Run(context.Background(), root, nil)
require.NoError(t, err)
}
require.Equal(t, 3, count)
// Run with dry-run flag
err = Parse(root, []string{"--dry-run"})
require.NoError(t, err)
err = Run(context.Background(), root, nil)
require.NoError(t, err)
require.Equal(t, 3, count)
})
t.Run("typo suggestion", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "count",
Usage: "count [flags] [command]",
SubCommands: []*Command{
{
Name: "version",
Usage: "show version",
Exec: func(ctx context.Context, s *State) error {
_, _ = s.Stdout.Write([]byte("1.0.0\n"))
return nil
},
},
},
Exec: func(ctx context.Context, s *State) error { return nil },
}
err := Parse(root, []string{"verzion"})
require.Error(t, err)
require.Contains(t, err.Error(), `unknown command "verzion". Did you mean one of these?`)
require.Contains(t, err.Error(), ` version`)
})
t.Run("command that panics during execution", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "panic",
Exec: func(ctx context.Context, s *State) error {
panic("test panic")
},
}
err := Parse(root, nil)
require.NoError(t, err)
err = Run(context.Background(), root, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "panic")
})
t.Run("run before parse", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "test",
Exec: func(ctx context.Context, s *State) error { return nil },
}
err := Run(context.Background(), root, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "command not parsed")
})
t.Run("concurrent state access", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "concurrent",
Flags: FlagsFunc(func(f *flag.FlagSet) {
f.String("value", "default", "test value")
}),
Exec: func(ctx context.Context, s *State) error {
// Simulate concurrent access to state
go func() {
_ = GetFlag[string](s, "value")
}()
return nil
},
}
err := Parse(root, []string{"--value", "test"})
require.NoError(t, err)
err = Run(context.Background(), root, nil)
require.NoError(t, err)
})
t.Run("io redirection", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "io",
Exec: func(ctx context.Context, s *State) error {
_, err := s.Stdout.Write([]byte("stdout output\n"))
if err != nil {
return err
}
_, err = s.Stderr.Write([]byte("stderr output\n"))
return err
},
}
err := Parse(root, nil)
require.NoError(t, err)
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
err = Run(context.Background(), root, &RunOptions{
Stdout: stdout,
Stderr: stderr,
})
require.NoError(t, err)
require.Equal(t, "stdout output\n", stdout.String())
require.Equal(t, "stderr output\n", stderr.String())
})
t.Run("numeric flag boundary values", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "numeric",
Flags: FlagsFunc(func(f *flag.FlagSet) {
f.Int("int", 0, "integer value")
f.Int64("int64", 0, "int64 value")
}),
Exec: func(ctx context.Context, s *State) error { return nil },
}
// Test max int
err := Parse(root, []string{"--int", "2147483647"})
require.NoError(t, err)
require.Equal(t, 2147483647, GetFlag[int](root.state, "int"))
// Test min int
err = Parse(root, []string{"--int", "-2147483648"})
require.NoError(t, err)
require.Equal(t, -2147483648, GetFlag[int](root.state, "int"))
// Test that parsing still works with large values (may not overflow in Go flag package)
err = Parse(root, []string{"--int", "999999999"})
require.NoError(t, err)
require.Equal(t, 999999999, GetFlag[int](root.state, "int"))
})
t.Run("location file path is relative", func(t *testing.T) {
t.Parallel()
loc := location(0)
// location returns "funcName file:line"
parts := strings.SplitN(loc, " ", 2)
require.Len(t, parts, 2, "location should return 'func file:line'")
// File path should be relative, not an absolute path
require.False(t, strings.HasPrefix(parts[1], "/"), "file path should be relative, not absolute: %s", parts[1])
})
t.Run("string flags with special characters", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "special",
Flags: FlagsFunc(func(f *flag.FlagSet) {
f.String("text", "", "text value")
}),
Exec: func(ctx context.Context, s *State) error { return nil },
}
specialValues := []string{
"text with spaces",
"text\"with\"quotes",
"text'with'apostrophes",
"text\nwith\nnewlines",
"text\twith\ttabs",
"text@with#symbols$",
}
for _, val := range specialValues {
err := Parse(root, []string{"--text", val})
require.NoError(t, err)
require.Equal(t, val, GetFlag[string](root.state, "text"))
}
})
}