| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | ) |
| 15 | |
| 16 | // TestPreCommitHookFormats commits through the pre-commit hook a real launch |
| 17 | // installs, with the project's format command set. A staged file is committed |
| 18 | // formatted, a file not staged stays out of the commit as it was, a file |
| 19 | // partly staged is committed as staged, and a formatter failure stops the |
| 20 | // commit with its output shown. |
| 21 | func TestPreCommitHookFormats(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | h := NewHarness(t) |
| 24 | |
| 25 | // Upper-cases every top-level .md file; refuses one that says |
| 26 | // UNFORMATTABLE. |
| 27 | formatter := filepath.Join(h.E2EDir, "fmt.sh") |
| 28 | script := `#!/bin/sh |
| 29 | for f in *.md; do |
| 30 | if grep -q UNFORMATTABLE "$f"; then echo "fmt: cannot format $f" >&2; exit 3; fi |
| 31 | tr a-z A-Z < "$f" > "$f.tmp" && mv "$f.tmp" "$f" |
| 32 | done |
| 33 | ` |
| 34 | if err := os.WriteFile(formatter, []byte(script), 0o755); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | h.AmendRepoConfig(map[string]any{"format": []string{formatter}}) |
| 38 | |
| 39 | res := h.RunKlaus("launch", "write some notes") |
| 40 | if res.ExitCode != 0 { |
| 41 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 42 | } |
| 43 | st, err := h.ReadState(h.RunIDs()[0]) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | h.WaitForClaudeStart(30 * time.Second) |
| 48 | wt := st.Worktree |
| 49 | write := func(name, content string) { |
| 50 | t.Helper() |
| 51 | if err := os.WriteFile(filepath.Join(wt, name), []byte(content), 0o644); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | write("notes.md", "hello\n") |
| 57 | h.git(wt, "add", "notes.md") |
| 58 | write("README.md", "# widget\nnot staged\n") |
| 59 | write("todo.md", "staged\n") |
| 60 | h.git(wt, "add", "todo.md") |
| 61 | write("todo.md", "staged\nnot staged\n") |
| 62 | h.git(wt, "commit", "-qm", "Add notes") |
| 63 | |
| 64 | if got := h.GitOutput(wt, "show", "HEAD:notes.md"); got != "HELLO" { |
| 65 | t.Errorf("committed notes.md = %q, want the formatted %q", got, "HELLO") |
| 66 | } |
| 67 | if got := h.GitOutput(wt, "show", "HEAD:README.md"); got != "# widget" { |
| 68 | t.Errorf("committed README.md = %q, want it unchanged", got) |
| 69 | } |
| 70 | if got := h.GitOutput(wt, "show", "HEAD:todo.md"); got != "staged" { |
| 71 | t.Errorf("committed todo.md = %q, want only its staged content %q", got, "staged") |
| 72 | } |
| 73 | if staged, unstaged := h.GitOutput(wt, "diff", "--cached", "--name-only"), h.GitOutput(wt, "diff", "--name-only"); staged != "" || unstaged != "README.md\ntodo.md" { |
| 74 | t.Errorf("after the commit, staged = %q and not staged = %q, want none staged and README.md, todo.md not staged", staged, unstaged) |
| 75 | } |
| 76 | |
| 77 | head := h.GitOutput(wt, "rev-parse", "HEAD") |
| 78 | write("bad.md", "UNFORMATTABLE\n") |
| 79 | h.git(wt, "add", "bad.md") |
| 80 | cmd := exec.Command("git", "commit", "-qm", "Add bad notes") |
| 81 | cmd.Dir = wt |
| 82 | cmd.Env = append(os.Environ(), "HOME="+h.Home, "GIT_CONFIG_GLOBAL="+filepath.Join(h.Home, ".gitconfig"), "GIT_CONFIG_NOSYSTEM=1") |
| 83 | out, err := cmd.CombinedOutput() |
| 84 | if err == nil { |
| 85 | t.Errorf("commit with a failing formatter should fail:\n%s", out) |
| 86 | } |
| 87 | if !strings.Contains(string(out), "fmt: cannot format bad.md") { |
| 88 | t.Errorf("failed commit should show the formatter's output:\n%s", out) |
| 89 | } |
| 90 | if got := h.GitOutput(wt, "rev-parse", "HEAD"); got != head { |
| 91 | t.Errorf("HEAD moved to %s after a failed format, want %s", got, head) |
| 92 | } |
| 93 | |
| 94 | h.ReleaseClaude() |
| 95 | h.WaitForState(st.ID, func(s *run.State) bool { return s.TmuxPane == nil && s.Worktree == "" }, 30*time.Second) |
| 96 | } |