| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/config" |
| 8 | "reasonix/internal/crashreport" |
| 9 | ) |
| 10 | |
| 11 | func TestRunWithCrashCaptureRecordsAndReraises(t *testing.T) { |
| 12 | home := t.TempDir() |
| 13 | t.Setenv("REASONIX_HOME", home) |
| 14 | previous := runCLI |
| 15 | t.Cleanup(func() { runCLI = previous }) |
| 16 | secret := "private prompt from panic" |
| 17 | runCLI = func([]string, string) int { panic(secret) } |
| 18 | |
| 19 | func() { |
| 20 | defer func() { |
| 21 | if recovered := recover(); recovered != secret { |
| 22 | t.Fatalf("reraised panic = %#v", recovered) |
| 23 | } |
| 24 | }() |
| 25 | runWithCrashCapture([]string{"run"}, "v1.20.0") |
| 26 | }() |
| 27 | |
| 28 | reports, err := crashreport.List(config.ReasonixHomeDir()) |
| 29 | if err != nil || len(reports) != 1 { |
| 30 | t.Fatalf("captured reports=%d err=%v", len(reports), err) |
| 31 | } |
| 32 | // Token-like function and file basenames may be redacted by the current |
| 33 | // privacy filter. Keep asserting that the top frame is the test call site |
| 34 | // without requiring its pre-redaction basename. |
| 35 | if got := reports[0].Report.TopFrame; !strings.Contains(got, "_test.go:") { |
| 36 | t.Fatalf("top frame = %q, want sanitized panic call site in a test file", got) |
| 37 | } |
| 38 | preview, err := crashreport.Preview(reports[0].Report) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if strings.Contains(string(preview), secret) { |
| 43 | t.Fatalf("panic value leaked into report: %s", preview) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestRunWithCrashCapturePassesThroughExitCode(t *testing.T) { |
| 48 | previous := runCLI |
| 49 | t.Cleanup(func() { runCLI = previous }) |
| 50 | runCLI = func(args []string, version string) int { |
| 51 | if len(args) != 1 || args[0] != "version" || version != "v1.20.0" { |
| 52 | t.Fatalf("runCLI args=%v version=%q", args, version) |
| 53 | } |
| 54 | return 17 |
| 55 | } |
| 56 | if got := runWithCrashCapture([]string{"version"}, "v1.20.0"); got != 17 { |
| 57 | t.Fatalf("exit code=%d", got) |
| 58 | } |
| 59 | } |
| 60 |