|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "testing" |
| 6 | + |
| 7 | + api "github.com/bootdotdev/bootdev/client" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRunCLICommandCapturesStdoutVariables(t *testing.T) { |
| 11 | + variables := map[string]string{} |
| 12 | + result := runCLICommand(api.CLIStepCLICommand{ |
| 13 | + Command: `go env GOOS`, |
| 14 | + StdoutVariables: []api.CLICommandStdoutVariable{{ |
| 15 | + Name: "goos", |
| 16 | + Regex: `([a-z0-9]+)`, |
| 17 | + }}, |
| 18 | + }, variables) |
| 19 | + |
| 20 | + if result.Err != "" { |
| 21 | + t.Fatalf("unexpected command error: %s", result.Err) |
| 22 | + } |
| 23 | + if result.Variables["goos"] != runtime.GOOS { |
| 24 | + t.Fatalf("captured goos = %q, want %q", result.Variables["goos"], runtime.GOOS) |
| 25 | + } |
| 26 | + if variables["goos"] != runtime.GOOS { |
| 27 | + t.Fatalf("shared goos = %q, want %q", variables["goos"], runtime.GOOS) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestRunCLICommandInterpolatesCapturedStdoutVariables(t *testing.T) { |
| 32 | + variables := map[string]string{} |
| 33 | + |
| 34 | + first := runCLICommand(api.CLIStepCLICommand{ |
| 35 | + Command: `go env -json GOOS`, |
| 36 | + StdoutVariables: []api.CLICommandStdoutVariable{{ |
| 37 | + Name: "goenv", |
| 38 | + Regex: `"([A-Z]+)"`, |
| 39 | + }}, |
| 40 | + }, variables) |
| 41 | + if first.Err != "" { |
| 42 | + t.Fatalf("unexpected first command error: %s", first.Err) |
| 43 | + } |
| 44 | + |
| 45 | + second := runCLICommand(api.CLIStepCLICommand{ |
| 46 | + Command: `go env ${goenv}`, |
| 47 | + }, variables) |
| 48 | + if second.Stdout != runtime.GOOS { |
| 49 | + t.Fatalf("second stdout = %q, want %q", second.Stdout, runtime.GOOS) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestParseStdoutVariablesRequiresOneCaptureGroup(t *testing.T) { |
| 54 | + variables := map[string]string{} |
| 55 | + err := parseStdoutVariables("token=abc123", []api.CLICommandStdoutVariable{{ |
| 56 | + Name: "token", |
| 57 | + Regex: `token=([a-z]+)([0-9]+)`, |
| 58 | + }}, variables) |
| 59 | + |
| 60 | + if err == nil { |
| 61 | + t.Fatal("expected parse error") |
| 62 | + } |
| 63 | + if err.Error() != "invalid stdout variable configuration" { |
| 64 | + t.Fatalf("error = %q, want invalid stdout variable configuration", err.Error()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestParseStdoutVariablesUsesGenericConfigurationError(t *testing.T) { |
| 69 | + tests := []struct { |
| 70 | + name string |
| 71 | + vardef api.CLICommandStdoutVariable |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "missing name", |
| 75 | + vardef: api.CLICommandStdoutVariable{Regex: `token=([a-z0-9]+)`}, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "missing regex", |
| 79 | + vardef: api.CLICommandStdoutVariable{Name: "token"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "invalid regex", |
| 83 | + vardef: api.CLICommandStdoutVariable{Name: "token", Regex: `token=([a-z0-9]+`}, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "too many capture groups", |
| 87 | + vardef: api.CLICommandStdoutVariable{Name: "token", Regex: `token=([a-z]+)([0-9]+)`}, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + variables := map[string]string{} |
| 94 | + err := parseStdoutVariables("token=abc123", []api.CLICommandStdoutVariable{tt.vardef}, variables) |
| 95 | + if err == nil { |
| 96 | + t.Fatal("expected parse error") |
| 97 | + } |
| 98 | + if err.Error() != "invalid stdout variable configuration" { |
| 99 | + t.Fatalf("error = %q, want invalid stdout variable configuration", err.Error()) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments