|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +func TestLocalTestRedirectedOutput(t *testing.T) { |
| 17 | + dir := t.TempDir() |
| 18 | + binary := filepath.Join(dir, "bootdev.exe") |
| 19 | + if output, err := exec.Command("go", "build", "-o", binary, ".").CombinedOutput(); err != nil { |
| 20 | + t.Fatalf("build CLI: %v\n%s", err, output) |
| 21 | + } |
| 22 | + config := filepath.Join(dir, "config.yaml") |
| 23 | + if err := os.WriteFile(config, []byte("{}\n"), 0o600); err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + |
| 27 | + command := `printf '\033[31mstdout-marker\033[0m\n'; printf 'stderr-marker\n' >&2` |
| 28 | + if runtime.GOOS == "windows" { |
| 29 | + command = `[Console]::Out.WriteLine([char]27 + '[31mstdout-marker' + [char]27 + '[0m'); [Console]::Error.WriteLine('stderr-marker')` |
| 30 | + } |
| 31 | + for _, tt := range []struct { |
| 32 | + name string |
| 33 | + fail bool |
| 34 | + verbose bool |
| 35 | + }{ |
| 36 | + {"pass verbose", false, true}, |
| 37 | + {"fail verbose", true, true}, |
| 38 | + {"pass compact", false, false}, |
| 39 | + {"fail compact", true, false}, |
| 40 | + } { |
| 41 | + t.Run(tt.name, func(t *testing.T) { |
| 42 | + wantExit := 0 |
| 43 | + if tt.fail { |
| 44 | + wantExit = 1 |
| 45 | + } |
| 46 | + manifest := filepath.Join(t.TempDir(), "cli.yaml") |
| 47 | + data := fmt.Sprintf("allowedOperatingSystems: [%s]\nsteps:\n - description: Output check\n cliCommand:\n command: %s\n tests:\n - exitCode: %d\n", runtime.GOOS, command, wantExit) |
| 48 | + if err := os.WriteFile(manifest, []byte(data), 0o600); err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 52 | + defer cancel() |
| 53 | + args := []string{"--config", config, "local-test", manifest} |
| 54 | + if tt.verbose { |
| 55 | + args = append(args, "--verbose") |
| 56 | + } |
| 57 | + cmd := exec.CommandContext(ctx, binary, args...) |
| 58 | + var stdout, stderr bytes.Buffer |
| 59 | + cmd.Stdout, cmd.Stderr = &stdout, &stderr |
| 60 | + err := cmd.Run() |
| 61 | + if cmd.ProcessState == nil || cmd.ProcessState.ExitCode() != wantExit { |
| 62 | + t.Fatalf("exit status: %v; stdout: %s; stderr: %s", err, &stdout, &stderr) |
| 63 | + } |
| 64 | + output := stdout.String() |
| 65 | + if strings.Contains(output+stderr.String(), "\x1b") || strings.Contains(stderr.String(), "TTY") { |
| 66 | + t.Fatalf("terminal output leaked: stdout=%q stderr=%q", output, stderr.String()) |
| 67 | + } |
| 68 | + if strings.Count(output, "Output check") != 1 { |
| 69 | + t.Fatalf("expected one final report: %s", output) |
| 70 | + } |
| 71 | + for _, diagnostic := range []string{"Command stdout:\n\nstdout-marker", "Command stderr:\n\nstderr-marker"} { |
| 72 | + if got, want := strings.Contains(output, diagnostic), tt.verbose || tt.fail; got != want { |
| 73 | + t.Fatalf("diagnostic %q present = %t, want %t; output: %s", diagnostic, got, want, output) |
| 74 | + } |
| 75 | + } |
| 76 | + if tt.fail { |
| 77 | + if !strings.Contains(stderr.String(), "local checks failed") { |
| 78 | + t.Fatalf("missing failure error: %s", &stderr) |
| 79 | + } |
| 80 | + } else if stderr.Len() != 0 || !strings.Contains(output, "All tests passed!") { |
| 81 | + t.Fatalf("unexpected success output: stdout=%q stderr=%q", output, stderr.String()) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments