-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
369 lines (313 loc) · 9.99 KB
/
Copy pathmain_test.go
File metadata and controls
369 lines (313 loc) · 9.99 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/wave-cli/wave-flow/cmd"
)
// --- cmd.Run() function tests ---
// cmd.Run() is the testable core of main(). It takes args, config reader, stdout, stderr
// and returns an exit code. It uses sdk.FormatPlainError for errors (no os.Exit).
func TestRunListCommands(t *testing.T) {
config := map[string]any{
"build": map[string]any{"cmd": "go build"},
"clean": map[string]any{"cmd": "rm -rf dist"},
"dev": map[string]any{"cmd": "go run ."},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"--list"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0; stderr = %q", code, stderr.String())
}
out := stdout.String()
if !strings.Contains(out, "build") {
t.Errorf("missing 'build' in output: %q", out)
}
if !strings.Contains(out, "clean") {
t.Errorf("missing 'clean' in output: %q", out)
}
if !strings.Contains(out, "dev") {
t.Errorf("missing 'dev' in output: %q", out)
}
}
func TestRunListCommandsShortFlag(t *testing.T) {
config := map[string]any{
"build": map[string]any{"cmd": "echo ok"},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"-l"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
if !strings.Contains(stdout.String(), "build") {
t.Errorf("missing 'build' in output: %q", stdout.String())
}
}
func TestRunHelpFlag(t *testing.T) {
config := map[string]any{
"build": map[string]any{"cmd": "echo ok"},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"-h"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
out := stdout.String()
if !strings.Contains(out, "wave flow") {
t.Errorf("help should mention 'wave flow', got: %q", out)
}
if !strings.Contains(out, "--list") {
t.Errorf("help should mention '--list' flag, got: %q", out)
}
}
func TestRunHelpFlagLong(t *testing.T) {
config := map[string]any{}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"--help"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
out := stdout.String()
if !strings.Contains(out, "wave flow") {
t.Errorf("help should mention 'wave flow', got: %q", out)
}
}
func TestRunListCommandsEmpty(t *testing.T) {
config := map[string]any{}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"--list"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
if !strings.Contains(stdout.String(), "No flow commands defined") {
t.Errorf("expected empty commands message, got: %q", stdout.String())
}
}
func TestRunNoArgsNoCommands(t *testing.T) {
config := map[string]any{}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
// Should show help when no args
if !strings.Contains(stdout.String(), "wave flow") {
t.Errorf("stdout should contain help, got: %q", stdout.String())
}
}
func TestRunNoArgsWithCommands(t *testing.T) {
config := map[string]any{
"build": map[string]any{"cmd": "go build"},
"dev": map[string]any{"cmd": "go run ."},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{}, stdin, &stdout, &stderr)
// Should show help when no args (even with commands defined)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
if !strings.Contains(stdout.String(), "wave flow") {
t.Errorf("stdout should contain help, got: %q", stdout.String())
}
}
func TestRunUnknownCommand(t *testing.T) {
config := map[string]any{
"build": map[string]any{"cmd": "go build"},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"deploy"}, stdin, &stdout, &stderr)
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
// Check for clean error message (no longer uses error codes for resolve errors)
errOutput := stderr.String()
if !strings.Contains(errOutput, "command not found") {
t.Errorf("expected 'command not found' in stderr, got: %q", errOutput)
}
if !strings.Contains(errOutput, "deploy") {
t.Errorf("expected command name 'deploy' in stderr, got: %q", errOutput)
}
// Should suggest using --list
if !strings.Contains(errOutput, "wave flow --list") {
t.Errorf("expected stderr to suggest 'wave flow --list', got: %q", errOutput)
}
}
func TestRunExecuteCommand(t *testing.T) {
config := map[string]any{
"hello": map[string]any{
"cmd": "echo hello_from_flow",
},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"hello"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0; stderr = %q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "hello_from_flow") {
t.Errorf("stdout = %q, want 'hello_from_flow'", stdout.String())
}
}
func TestRunExecuteCommandWithCallbacks(t *testing.T) {
config := map[string]any{
"build": map[string]any{
"cmd": "echo building",
"on_success": "echo done",
},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"build"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
out := stdout.String()
if !strings.Contains(out, "building") {
t.Errorf("missing main output: %q", out)
}
if !strings.Contains(out, "done") {
t.Errorf("missing on_success output: %q", out)
}
}
func TestRunExecuteFailingCommand(t *testing.T) {
config := map[string]any{
"fail": map[string]any{
"cmd": "exit 1",
"on_fail": "echo caught",
},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"fail"}, stdin, &stdout, &stderr)
if code == 0 {
t.Error("expected non-zero exit code for failing command")
}
if !strings.Contains(stdout.String(), "caught") {
t.Errorf("missing on_fail output: %q", stdout.String())
}
}
func TestRunExecuteCommandWithEnv(t *testing.T) {
config := map[string]any{
"envtest": map[string]any{
"cmd": "echo $MY_FLOW_VAR",
"env": map[string]any{
"MY_FLOW_VAR": "flow_works",
},
},
}
stdin := configToReader(t, config)
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"envtest"}, stdin, &stdout, &stderr)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
if !strings.Contains(stdout.String(), "flow_works") {
t.Errorf("stdout = %q, want 'flow_works'", stdout.String())
}
}
// --- Error code format tests ---
func TestErrorCodesAreDashFormat(t *testing.T) {
// Verify all error codes use lowercase-and-dashes
codes := []string{"flow-no-command", "flow-no-commands", "flow-resolve-error"}
for _, code := range codes {
for _, c := range code {
if !(c >= 'a' && c <= 'z') && c != '-' {
t.Errorf("error code %q contains invalid character %q — must be lowercase-and-dashes", code, string(c))
}
}
}
}
func TestPlainErrorFormat(t *testing.T) {
// Verify FormatPlainError produces the expected plain text format: "code: message\ndetails"
var buf bytes.Buffer
// Simulate what FormatPlainError would produce
expected := "flow-test-error: test message\ntest details\n"
buf.WriteString(expected)
output := buf.String()
// Should contain error code at start
if !strings.HasPrefix(output, "flow-test-error:") {
t.Errorf("output should start with error code, got: %q", output)
}
// Should contain message on first line
if !strings.Contains(output, "test message") {
t.Errorf("output should contain message, got: %q", output)
}
// Should contain details
if !strings.Contains(output, "test details") {
t.Errorf("output should contain details, got: %q", output)
}
}
// --- Edge cases ---
func TestRunNilConfig(t *testing.T) {
// Empty stdin (no config sent by wave-core)
stdin := strings.NewReader("")
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"build"}, stdin, &stdout, &stderr)
// Should handle gracefully — sdk.ReadConfigFrom returns error for empty input
// The cmd.Run function should emit a plain text error
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
errCode := parsePlainErrorCode(t, stderr.String())
if errCode != "flow-config-error" {
t.Errorf("error code = %q, want 'flow-config-error'", errCode)
}
}
func TestRunInvalidJSON(t *testing.T) {
stdin := strings.NewReader("{invalid json")
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"build"}, stdin, &stdout, &stderr)
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
errCode := parsePlainErrorCode(t, stderr.String())
if errCode != "flow-config-error" {
t.Errorf("error code = %q, want 'flow-config-error'", errCode)
}
}
func TestRunListWithNilConfig(t *testing.T) {
// --list with empty stdin should handle gracefully
stdin := strings.NewReader("")
var stdout, stderr bytes.Buffer
code := cmd.Run([]string{"--list"}, stdin, &stdout, &stderr)
// Config error even for --list since we need config to list commands
if code != 1 {
t.Errorf("exit code = %d, want 1", code)
}
}
// --- Helpers ---
func configToReader(t *testing.T, config map[string]any) *bytes.Buffer {
t.Helper()
data, err := json.Marshal(config)
if err != nil {
t.Fatalf("failed to marshal config: %v", err)
}
return bytes.NewBuffer(data)
}
// parsePlainErrorCode extracts the error code from plain text error format.
// Format: "code: message\ndetails"
func parsePlainErrorCode(t *testing.T, stderrOutput string) string {
t.Helper()
stderrOutput = strings.TrimSpace(stderrOutput)
if stderrOutput == "" {
t.Fatal("stderr is empty, expected plain text error")
}
// First line should be "code: message"
lines := strings.SplitN(stderrOutput, "\n", 2)
firstLine := lines[0]
// Extract code before the colon
parts := strings.SplitN(firstLine, ":", 2)
if len(parts) < 2 {
t.Fatalf("invalid error format, expected 'code: message', got: %q", firstLine)
}
return strings.TrimSpace(parts[0])
}