From 7e723b47a0b221542145ced469be0c0dafcf5fa2 Mon Sep 17 00:00:00 2001 From: archie Date: Fri, 24 Jul 2026 05:27:26 +0000 Subject: [PATCH] test: failing repro for #58 --- .git-askpass | 2 + internal/worktree/worktree_test.go | 307 +++++++++++++++++++++++++++++ task.json | 1 + 3 files changed, 310 insertions(+) create mode 100755 .git-askpass create mode 100644 task.json diff --git a/.git-askpass b/.git-askpass new file mode 100755 index 000000000..ac06c1b86 --- /dev/null +++ b/.git-askpass @@ -0,0 +1,2 @@ +#!/bin/sh +echo "$ARCHIE_GIT_TOKEN" diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index bb921d1d7..64b39c732 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -207,3 +207,310 @@ func TestCacheDirCannotEscapeWorkDir(t *testing.T) { t.Fatalf("cache path escaped managed root: %s", cache) } } + +// --------------------------------------------------------------------------- +// Repro tests for uncovered code paths (issue #58) +// --------------------------------------------------------------------------- + +// TestPrepareIdempotentFastPath proves the "already prepared" short-circuit +// in prepare(). The second call must return the same dir/branch without +// error, and the worktree must remain functional (fetch+checkout+reset). +func TestPrepareIdempotentFastPath(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + host := newLocalRemote(t, "acme", "idem") + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused-for-file-remotes", + BotUser: "archie-bot", + BotEmail: "archie-bot@example.com", + BaseURL: "file://" + host, + } + + dir1, branch1, err := m.Prepare(ctx, "acme", "idem", "main", 1, "feat: idempotent", "", "") + if err != nil { + t.Fatalf("first prepare: %v", err) + } + if _, err := os.Stat(filepath.Join(dir1, ".git")); err != nil { + t.Fatalf("worktree not created: %v", err) + } + + // Second Prepare with the same arguments must hit the fast path. + dir2, branch2, err := m.Prepare(ctx, "acme", "idem", "main", 1, "feat: idempotent", "", "") + if err != nil { + t.Fatalf("second prepare (fast path): %v", err) + } + if dir1 != dir2 { + t.Fatalf("fast path returned different dir: %q vs %q", dir1, dir2) + } + if branch1 != branch2 { + t.Fatalf("fast path returned different branch: %q vs %q", branch1, branch2) + } + + // The worktree must still be usable after the fast path. + if _, err := os.Stat(filepath.Join(dir2, ".git")); err != nil { + t.Fatalf("fast path broke .git: %v", err) + } + + // After the fast path, we should be able to commit and push. + if err := os.WriteFile(filepath.Join(dir2, "fast.txt"), []byte("fast path\n"), 0o644); err != nil { + t.Fatal(err) + } + changed, err := m.CommitAll(ctx, dir2, "test fast path") + if err != nil { + t.Fatalf("CommitAll after fast path: %v", err) + } + if !changed { + t.Fatal("CommitAll after fast path reported clean tree, expected changes") + } + if err := m.Push(ctx, dir2, branch2); err != nil { + t.Fatalf("Push after fast path: %v", err) + } +} + +// TestPrepareBadBaseBranch proves that prepare() propagates git clone +// failures when the requested base branch does not exist. +func TestPrepareBadBaseBranch(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + host := newLocalRemote(t, "acme", "badbase") + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused-for-file-remotes", + BotUser: "archie-bot", + BotEmail: "archie-bot@example.com", + BaseURL: "file://" + host, + } + + _, _, err := m.Prepare(ctx, "acme", "badbase", "nonexistent-branch", 1, "feat: test", "", "") + if err == nil { + t.Fatal("expected error for nonexistent base branch, got nil") + } + // The error must mention the git command ("clone") and the bad branch. + if !strings.Contains(err.Error(), "clone") { + t.Fatalf("error should mention 'clone', got: %v", err) + } + if !strings.Contains(err.Error(), "nonexistent-branch") { + t.Fatalf("error should mention bad branch, got: %v", err) + } +} + +// TestPrepareBadCloneURL proves that prepare() propagates git clone +// failures when the clone URL points to a nonexistent repository. +func TestPrepareBadCloneURL(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused-for-file-remotes", + BotUser: "archie-bot", + BotEmail: "archie-bot@example.com", + BaseURL: "file:///nonexistent/path/that/does/not/exist", + } + + _, _, err := m.Prepare(ctx, "acme", "norepo", "main", 1, "feat: test", "", "") + if err == nil { + t.Fatal("expected error for bad clone URL, got nil") + } + // The error must mention the git command ("clone"). + if !strings.Contains(err.Error(), "clone") { + t.Fatalf("error should mention 'clone', got: %v", err) + } +} + +// TestCommitAllInNonGitDir proves that CommitAll returns an error when +// invoked in a directory that is not a git repository. +func TestCommitAllInNonGitDir(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused", + BotUser: "bot", + BotEmail: "bot@example.com", + } + + dir := t.TempDir() // plain directory, no .git + changed, err := m.CommitAll(ctx, dir, "should fail") + if err == nil { + t.Fatalf("expected error for CommitAll in non-git dir, got (changed=%v, err=nil)", changed) + } + if changed { + t.Fatal("CommitAll in non-git dir reported changes, expected false") + } +} + +// TestChangedLinesInNonGitDir proves that ChangedLines returns an error +// when invoked in a directory that is not a git repository. +func TestChangedLinesInNonGitDir(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused", + BotUser: "bot", + BotEmail: "bot@example.com", + } + + dir := t.TempDir() // plain directory, no .git + lines, err := m.ChangedLines(ctx, dir, "main") + if err == nil { + t.Fatalf("expected error for ChangedLines in non-git dir, got (lines=%d, err=nil)", lines) + } + if lines != 0 { + t.Fatalf("ChangedLines in non-git dir returned non-zero lines: %d", lines) + } +} + +// TestDiffInNonGitDir proves that Diff returns an error when invoked +// in a directory that is not a git repository. +func TestDiffInNonGitDir(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused", + BotUser: "bot", + BotEmail: "bot@example.com", + } + + dir := t.TempDir() // plain directory, no .git + diff, err := m.Diff(ctx, dir, "main") + if err == nil { + t.Fatalf("expected error for Diff in non-git dir, got diff=%q, err=nil", diff) + } + if diff != "" { + t.Fatalf("Diff in non-git dir returned non-empty output: %q", diff) + } +} + +// TestPushRejected proves that Push returns an error when the remote +// rejects the push (e.g. non-fast-forward). +func TestPushRejected(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + host := newLocalRemote(t, "acme", "reject") + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused-for-file-remotes", + BotUser: "archie-bot", + BotEmail: "archie-bot@example.com", + BaseURL: "file://" + host, + } + + dir, branch, err := m.Prepare(ctx, "acme", "reject", "main", 1, "feat: push test", "", "") + if err != nil { + t.Fatalf("prepare: %v", err) + } + + // Push once so the branch exists on the remote. + if err := os.WriteFile(filepath.Join(dir, "first.txt"), []byte("first\n"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := m.CommitAll(ctx, dir, "first commit"); err != nil { + t.Fatal(err) + } + if err := m.Push(ctx, dir, branch); err != nil { + t.Fatalf("first push: %v", err) + } + + // Now advance the remote branch independently (simulate another + // client pushing), then try to push again from the stale local branch. + // We do this by creating a second worktree that pushes to the same + // branch, making the first worktree's branch out of date. + dir2, _, err := m.Prepare(ctx, "acme", "reject", "main", 2, "feat: conflicting", "", "") + if err != nil { + t.Fatalf("second prepare: %v", err) + } + // Checkout the same branch in the second worktree. + if _, err := exec.Command("git", "-C", dir2, "fetch", "origin", branch).CombinedOutput(); err != nil { + t.Fatalf("fetch branch in second worktree: %v", err) + } + if _, err := exec.Command("git", "-C", dir2, "checkout", "-b", branch, "origin/"+branch).CombinedOutput(); err != nil { + t.Fatalf("checkout branch in second worktree: %v", err) + } + if err := os.WriteFile(filepath.Join(dir2, "second.txt"), []byte("second\n"), 0o644); err != nil { + t.Fatal(err) + } + // Commit and push from the second worktree using raw git to bypass Manager. + cmd := exec.Command("git", "-C", dir2, "add", "-A") + cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("add in second worktree: %v\n%s", err, out) + } + cmd = exec.Command("git", "-C", dir2, "commit", "-m", "second commit") + cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("commit in second worktree: %v\n%s", err, out) + } + cmd = exec.Command("git", "-C", dir2, "push", "origin", branch) + cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("push from second worktree: %v\n%s", err, out) + } + + // Now the first worktree's branch is behind origin/branch. + // Make a change and try to push — it should be rejected. + if err := os.WriteFile(filepath.Join(dir, "third.txt"), []byte("third\n"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := m.CommitAll(ctx, dir, "third commit"); err != nil { + t.Fatal(err) + } + err = m.Push(ctx, dir, branch) + if err == nil { + t.Fatal("expected push rejection due to non-fast-forward, got nil error") + } +} + +// TestGitErrorWrapping proves that git() wraps errors with the command +// name and preserves the underlying error via %w. +func TestGitErrorWrapping(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := context.Background() + + m := &Manager{ + WorkDir: t.TempDir(), + Token: "unused", + BotUser: "bot", + BotEmail: "bot@example.com", + } + + dir := t.TempDir() // plain directory, no .git + _, err := m.CommitAll(ctx, dir, "test message") + if err == nil { + t.Fatal("expected error from git in non-repo dir") + } + + // The error must contain "git" and the specific command ("add"). + errStr := err.Error() + if !strings.Contains(errStr, "git") { + t.Fatalf("error should contain 'git' prefix: %v", err) + } + if !strings.Contains(errStr, "add") { + t.Fatalf("error should mention the failing command 'add': %v", err) + } +} diff --git a/task.json b/task.json new file mode 100644 index 000000000..025d26439 --- /dev/null +++ b/task.json @@ -0,0 +1 @@ +{"id":4,"owner":"sam","repo":"archie-core","issue_number":58,"title":"Add tests for Prepare's idempotent fast path and git error paths","body":"## Problem\n\n`internal/worktree/worktree_test.go` contains a single test, `TestPrepareCommitPushRoundTrip` (worktree_test.go:51), which only exercises the full happy path: clone → checkout → commit → push → verify remote branch. It never:\n\n1. Calls `Prepare` a second time on the same `(owner, repo, issue)` to verify the existing-worktree short-circuit at `internal/worktree/worktree.go:77-79`:\n ```go\n // Already prepared (daemon cloned before container acquire).\n if st, statErr := os.Stat(filepath.Join(dir, \".git\")); statErr == nil \u0026\u0026 st.IsDir() {\n return dir, branch, nil\n }\n ```\n2. Exercises any git-command failure propagation paths in `git()`, `CommitAll`, `Push`, `ChangedLines`, or `Diff` — e.g. a bad clone URL, an invalid base branch name, or a rejected push.\n\n## Why it matters\n\nThe \"already prepared\" short-circuit is a fragile, filesystem-presence-based idempotency check (it only verifies `.git` exists as a directory, not that the worktree is on the correct branch or otherwise valid). Since it is currently unverified by any test, a regression here (e.g. returning a stale/wrong branch, or failing to detect a corrupt partial clone) would go unnoticed. Similarly, all error-return branches in the git-wrapping methods are untested, so failures in argument construction or error wrapping (`fmt.Errorf(\"git %s: %w\\n%s\", ...)`) could silently break without any test catching it.\n\n## Suggested fix\n\n- Add a case to `TestPrepareCommitPushRoundTrip` (or a new test) that calls `m.Prepare` twice with the same arguments and asserts the second call returns the same `dir`/`branch` without re-cloning (e.g. assert no network/clone call occurs, or that repo state is unchanged).\n- Add table-driven or dedicated tests for failure paths:\n - `Prepare` with a nonexistent base branch or bad clone URL, expecting a non-nil error.\n - `Push` against a URL/remote that rejects the push.\n - `CommitAll`, `ChangedLines`, `Diff` invoked in a directory that is not a valid git repo, expecting errors to propagate correctly.\n","labels":["agent:dead","bug"],"workflow":"","branch":"fix/58-add-tests-for-prepares-idempotent-fast-path-and-git-error-pa"} \ No newline at end of file