| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func strp(s string) *string { return &s } |
| 10 | |
| 11 | // write drops a file with content under dir, failing the test on error. |
| 12 | func write(t *testing.T, dir, name, content string) { |
| 13 | t.Helper() |
| 14 | if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | const brokenGo = "package main\nfunc main() { this is not valid go }\n" |
| 20 | const validGo = "package main\nfunc main() {}\n" |
| 21 | const goMod = "module verifytest\n\ngo 1.21\n" |
| 22 | |
| 23 | // TestVerifyRebasedWorktree exercises the real post-rebase verification: it must |
| 24 | // NOT run go build in a non-Go worktree (no go.mod), MUST run go build when a |
| 25 | // go.mod is present, and MUST run a configured command when set. |
| 26 | func TestVerifyRebasedWorktree(t *testing.T) { |
| 27 | t.Run("no go.mod, no command → skip (non-Go repo merges cleanly)", func(t *testing.T) { |
| 28 | dir := t.TempDir() |
| 29 | // A broken .go file is present but must be ignored — no go.mod means skip. |
| 30 | write(t, dir, "main.go", brokenGo) |
| 31 | if err := verifyRebasedWorktree(dir, nil); err != nil { |
| 32 | t.Fatalf("expected skip (nil), got %v", err) |
| 33 | } |
| 34 | }) |
| 35 | |
| 36 | t.Run("go.mod present → go build runs and catches breakage", func(t *testing.T) { |
| 37 | dir := t.TempDir() |
| 38 | write(t, dir, "go.mod", goMod) |
| 39 | write(t, dir, "main.go", brokenGo) |
| 40 | if err := verifyRebasedWorktree(dir, nil); err == nil { |
| 41 | t.Fatal("expected go build to fail on broken source, got nil") |
| 42 | } |
| 43 | }) |
| 44 | |
| 45 | t.Run("go.mod present + valid source → passes", func(t *testing.T) { |
| 46 | dir := t.TempDir() |
| 47 | write(t, dir, "go.mod", goMod) |
| 48 | write(t, dir, "main.go", validGo) |
| 49 | if err := verifyRebasedWorktree(dir, nil); err != nil { |
| 50 | t.Fatalf("expected pass, got %v", err) |
| 51 | } |
| 52 | }) |
| 53 | |
| 54 | t.Run("configured command runs (success)", func(t *testing.T) { |
| 55 | dir := t.TempDir() |
| 56 | marker := filepath.Join(dir, "ran") |
| 57 | if err := verifyRebasedWorktree(dir, strp("touch ran")); err != nil { |
| 58 | t.Fatalf("expected pass, got %v", err) |
| 59 | } |
| 60 | if _, err := os.Stat(marker); err != nil { |
| 61 | t.Fatalf("configured command did not run in worktree: %v", err) |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | t.Run("configured command runs (failure surfaces)", func(t *testing.T) { |
| 66 | dir := t.TempDir() |
| 67 | if err := verifyRebasedWorktree(dir, strp("exit 3")); err == nil { |
| 68 | t.Fatal("expected configured command failure to surface, got nil") |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | t.Run("configured command takes precedence over go build", func(t *testing.T) { |
| 73 | dir := t.TempDir() |
| 74 | // Broken go.mod source would fail go build; the command must win and pass. |
| 75 | write(t, dir, "go.mod", goMod) |
| 76 | write(t, dir, "main.go", brokenGo) |
| 77 | if err := verifyRebasedWorktree(dir, strp("true")); err != nil { |
| 78 | t.Fatalf("expected configured command to take precedence and pass, got %v", err) |
| 79 | } |
| 80 | }) |
| 81 | } |