beads/cmd/bd/worktree_gitignore_eol_test.go
Julian Knutsen ba2f0afe4a
refactor(worktree): route addToGitignore through gitignore.AppendLines (post-merge #5677) (#7006)
#5677 made addToGitignore preserve a CRLF .gitignore by consuming
gitignore.AppendLineEnding, but it re-implemented the rest of the #6343
append policy inline: the final-line completion, including the rule that a
trailing CR is completed with a bare LF. That policy already lives in
gitignore.AppendLines, which the three other project-ignore writers call.
The two copies are byte-equivalent today, and only one of them is
discoverable when the policy changes.

Derive the appended bytes from AppendLines instead. Its contract leaves the
existing bytes unchanged, so everything past len(content) is exactly the
append, and writing only that suffix keeps the O_APPEND handle: the user's
.gitignore is still appended to, never truncated and rewritten. The write
also becomes a single Write call instead of a separator write followed by an
Fprintf.

No behavior change. The call-site tests pass unmodified, byte for byte. The
doc comment on TestAddToGitignoreCompletesUnterminatedCRLFLine is reworded
because it described the removed separator variable; the arm it pins, and
why only that test reaches it, are unchanged.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR

Co-authored-by: Eddie the Engineer <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 05:24:11 -07:00

104 lines
3.6 KiB
Go

package main
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
)
func TestAddToGitignoreCompletesTrailingCR(t *testing.T) {
repoRoot := initGitRepoForGitignoreTest(t)
path := filepath.Join(repoRoot, ".gitignore")
initial := []byte("a/\r\nb/\r")
if err := os.WriteFile(path, initial, 0644); err != nil {
t.Fatal(err)
}
want := []byte("a/\r\nb/\r\n# bd worktree\r\nworktree-feature/\r\n")
for i := 0; i < 2; i++ {
if err := addToGitignore(context.Background(), repoRoot, "worktree-feature"); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Fatalf("append %d bytes = %q, want %q", i+1, got, want)
}
}
}
// TestAddToGitignoreCompletesUnterminatedCRLFLine covers the one arm where
// addToGitignore's append completes the final line with the detected line ending.
//
// gitignore.AppendLines guards that completion twice over: it only completes a
// file that does not end in "\n", and it completes a trailing "\r" with a bare
// "\n" instead. So the detected "\r\n" is used only for a CRLF file whose
// final byte is neither — an unterminated final line like "build/". The sibling
// fixtures in this file cannot reach it: "a/\r\nb/\r" ends in CR and takes the
// bare "\n" completion, and "node_modules/\r\n" ends in LF and needs no
// completion at all. Without this case, completing the final line with a
// hardcoded "\n" still passes every test here while writing exactly the
// mixed-terminator .gitignore this change exists to prevent.
//
// internal/gitignore's own tables already cover this input (append_test.go,
// "CRLF with unterminated final line" for the detector and "CRLF unterminated"
// for AppendLines); this is the call site that consumes them.
func TestAddToGitignoreCompletesUnterminatedCRLFLine(t *testing.T) {
repoRoot := initGitRepoForGitignoreTest(t)
path := filepath.Join(repoRoot, ".gitignore")
initial := []byte("node_modules/\r\nbuild/")
if err := os.WriteFile(path, initial, 0644); err != nil {
t.Fatal(err)
}
want := []byte("node_modules/\r\nbuild/\r\n# bd worktree\r\nworktree-feature/\r\n")
// Twice: the append must be idempotent as well as CRLF-correct.
for i := 0; i < 2; i++ {
if err := addToGitignore(context.Background(), repoRoot, "worktree-feature"); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Fatalf("append %d bytes = %q, want %q", i+1, got, want)
}
}
}
func TestAddToGitignorePreservesCRLFWhenAppending(t *testing.T) {
repoRoot := initGitRepoForGitignoreTest(t)
gitignorePath := filepath.Join(repoRoot, ".gitignore")
initial := []byte("node_modules/\r\n")
if err := os.WriteFile(gitignorePath, initial, 0644); err != nil {
t.Fatalf("failed to write .gitignore: %v", err)
}
entry := "worktree-feature"
if err := addToGitignore(context.Background(), repoRoot, entry); err != nil {
t.Fatalf("first addToGitignore failed: %v", err)
}
want := []byte("node_modules/\r\n# bd worktree\r\nworktree-feature/\r\n")
updated, err := os.ReadFile(gitignorePath)
if err != nil {
t.Fatalf("failed to read .gitignore: %v", err)
}
if !bytes.Equal(updated, want) {
t.Fatalf(".gitignore bytes after append:\nwant: %q\ngot: %q", want, updated)
}
if err := addToGitignore(context.Background(), repoRoot, entry); err != nil {
t.Fatalf("second addToGitignore failed: %v", err)
}
unchanged, err := os.ReadFile(gitignorePath)
if err != nil {
t.Fatalf("failed to reread .gitignore: %v", err)
}
if !bytes.Equal(unchanged, want) {
t.Fatalf("second append changed .gitignore:\nwant: %q\ngot: %q", want, unchanged)
}
}