refactor: testing from the beginning. a journey in 150k lines of tests to none
This commit is contained in:
651 files changed
-146443
No files matched your search
@@ -1,107 +0,0 @@
|
||||
package worktree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
gitconfig "github.com/go-git/go-git/v6/config"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
)
|
||||
|
||||
// pushHeadBranch pushes a "feature/widget" branch carrying widget.go onto the
|
||||
// bare remote, giving CheckoutPR a real head to materialise.
|
||||
func pushHeadBranch(t *testing.T, host string) {
|
||||
t.Helper()
|
||||
cloneDir := filepath.Join(t.TempDir(), "clone")
|
||||
r, err := git.PlainClone(cloneDir, &git.CloneOptions{URL: filepath.Join(host, "acme", "todo.git")})
|
||||
if err != nil {
|
||||
t.Fatalf("clone bare remote: %v", err)
|
||||
}
|
||||
wt, err := r.Worktree()
|
||||
if err != nil {
|
||||
t.Fatalf("worktree: %v", err)
|
||||
}
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Branch: plumbing.NewBranchReferenceName("feature/widget"),
|
||||
Create: true,
|
||||
}); err != nil {
|
||||
t.Fatalf("create feature branch: %v", err)
|
||||
}
|
||||
writeSeedCommit(t, r, cloneDir, "widget.go", "package widget\n", "add widget")
|
||||
if err := r.Push(&git.PushOptions{
|
||||
RemoteName: git.DefaultRemoteName,
|
||||
RefSpecs: []gitconfig.RefSpec{"refs/heads/feature/widget:refs/heads/feature/widget"},
|
||||
}); err != nil {
|
||||
t.Fatalf("push feature branch: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckoutPRMaterializesHeadForDiffAndSnapshot(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
pushHeadBranch(t, host)
|
||||
m := newManager(t, host)
|
||||
|
||||
dir, cleanup, err := m.CheckoutPR(ctx, "acme", "todo", "feature/widget", "main")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckoutPR error = %v", err)
|
||||
}
|
||||
|
||||
diff, err := m.Diff(ctx, dir, "main")
|
||||
if err != nil {
|
||||
t.Fatalf("Diff error = %v", err)
|
||||
}
|
||||
if !strings.Contains(diff, "widget.go") {
|
||||
t.Errorf("Diff = %q, want the widget.go change", diff)
|
||||
}
|
||||
|
||||
snapshotDir := filepath.Join(t.TempDir(), "snap")
|
||||
if err := m.Snapshot(ctx, dir, snapshotDir); err != nil {
|
||||
t.Fatalf("Snapshot error = %v", err)
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(snapshotDir, "widget.go"))
|
||||
if err != nil {
|
||||
t.Fatalf("read snapshotted widget.go: %v", err)
|
||||
}
|
||||
if string(content) != "package widget\n" {
|
||||
t.Errorf("snapshotted widget.go = %q, want %q", content, "package widget\n")
|
||||
}
|
||||
|
||||
cleanup()
|
||||
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
||||
t.Errorf("CheckoutPR dir %q still exists after cleanup: %v", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckoutPRRefusesMissingHead(t *testing.T) {
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
m := newManager(t, host)
|
||||
|
||||
_, cleanup, err := m.CheckoutPR(context.Background(), "acme", "todo", "feature/nonexistent", "main")
|
||||
if err == nil {
|
||||
t.Fatal("CheckoutPR error = nil, want failure for a head branch that is not pushed")
|
||||
}
|
||||
if cleanup != nil {
|
||||
t.Error("cleanup should be nil when CheckoutPR fails before materialising")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckoutPRRefusesMissingBase(t *testing.T) {
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
pushHeadBranch(t, host)
|
||||
m := newManager(t, host)
|
||||
|
||||
_, cleanup, err := m.CheckoutPR(context.Background(), "acme", "todo", "feature/widget", "no-such-base")
|
||||
if err == nil {
|
||||
t.Fatal("CheckoutPR error = nil, want failure for an unresolvable base branch")
|
||||
}
|
||||
// On failure CheckoutPR cleans up internally and returns a nil cleanup,
|
||||
// matching the missing-head path: cleanup is non-nil only on success.
|
||||
if cleanup != nil {
|
||||
t.Error("cleanup should be nil on failure; CheckoutPR removes the clone itself")
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
package worktree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
)
|
||||
|
||||
// headHash reads a worktree's HEAD commit, so a test can assert which commit a
|
||||
// prepare landed on.
|
||||
func headHash(t *testing.T, dir string) plumbing.Hash {
|
||||
t.Helper()
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("PlainOpen(%s): %v", dir, err)
|
||||
}
|
||||
ref, err := r.Head()
|
||||
if err != nil {
|
||||
t.Fatalf("Head(%s): %v", dir, err)
|
||||
}
|
||||
return ref.Hash()
|
||||
}
|
||||
|
||||
// prepareResume models what a task retry starts from: a fresh prepare, a commit
|
||||
// pushed onto the task branch, and the pushed tip. It returns the worktree
|
||||
// directory, branch and tip.
|
||||
func prepareResume(t *testing.T, m *Manager, issue int, title string) (dir, branch string, tip plumbing.Hash) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, issue, title, "", "bug", Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("PlainOpen: %v", err)
|
||||
}
|
||||
writeSeedCommit(t, r, dir, "widget.go", "package widget\n", "add widget")
|
||||
if err := m.Push(ctx, dir, branch); err != nil {
|
||||
t.Fatalf("Push() error = %v", err)
|
||||
}
|
||||
return dir, branch, headHash(t, dir)
|
||||
}
|
||||
|
||||
// TestPrepareResumeLandsOnTheBranchTipNotBase pins the resume target's
|
||||
// semantics: it must land the worktree on the PR branch's remote tip (the
|
||||
// committed work), not reset to base the way a fresh prepare does.
|
||||
func TestPrepareResumeLandsOnTheBranchTipNotBase(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
m := newManager(t, newLocalRemote(t, "acme", "todo"))
|
||||
dir, branch, tip := prepareResume(t, m, 7, "feat: add widget")
|
||||
|
||||
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 7, "feat: add widget", "", "bug", Target(branch)); err != nil {
|
||||
t.Fatalf("Prepare(resume) error = %v", err)
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(dir, "widget.go"))
|
||||
if err != nil {
|
||||
t.Fatalf("widget.go missing after resume: %v (resume reset to base and discarded the branch work)", err)
|
||||
}
|
||||
if string(content) != "package widget\n" {
|
||||
t.Fatalf("widget.go = %q, want %q", content, "package widget\n")
|
||||
}
|
||||
if got := headHash(t, dir); got != tip {
|
||||
t.Fatalf("HEAD = %s, want the branch tip %s", got, tip)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrepareResumeClonesAMissingWorktree is the root-cause test for
|
||||
// archie-core-866m: a resume must create the clone when it is missing, so a
|
||||
// retry reaches a task whose worktree the terminal cleanup removed, an expired
|
||||
// volume dropped, or a different host never had.
|
||||
func TestPrepareResumeClonesAMissingWorktree(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
m := newManager(t, newLocalRemote(t, "acme", "todo"))
|
||||
dir, branch, tip := prepareResume(t, m, 8, "fix: thing")
|
||||
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatalf("remove worktree: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); !os.IsNotExist(err) {
|
||||
t.Fatalf("worktree not removed: sentinel stat err = %v", err)
|
||||
}
|
||||
|
||||
gotDir, gotBranch, err := m.Prepare(ctx, "acme", "todo", testBase, 8, "fix: thing", "", "bug", Target(branch))
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare(resume) on a missing worktree error = %v, want it cloned", err)
|
||||
}
|
||||
if gotDir != dir || gotBranch != branch {
|
||||
t.Fatalf("Prepare(resume) = (%q, %q), want (%q, %q)", gotDir, gotBranch, dir, branch)
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(gotDir, "widget.go"))
|
||||
if err != nil {
|
||||
t.Fatalf("widget.go missing after resume cloned: %v", err)
|
||||
}
|
||||
if string(content) != "package widget\n" {
|
||||
t.Fatalf("widget.go = %q, want %q", content, "package widget\n")
|
||||
}
|
||||
if got := headHash(t, gotDir); got != tip {
|
||||
t.Fatalf("HEAD = %s, want the branch tip %s", got, tip)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrepareResumeFailsClosedWhenTheBranchIsNotPushed pins that a resume
|
||||
// never falls back to base: a branch that is not on the remote is a dispatch
|
||||
// defect, and a base checkout would silently remediate the wrong tree.
|
||||
func TestPrepareResumeFailsClosedWhenTheBranchIsNotPushed(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
m := newManager(t, newLocalRemote(t, "acme", "todo"))
|
||||
dir, _, tip := prepareResume(t, m, 9, "fix: thing")
|
||||
|
||||
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 9, "fix: thing", "", "bug", Target("fix/9-never-pushed")); err == nil {
|
||||
t.Fatal("Prepare(resume) with an unpushed branch = nil error, want a fail-closed error")
|
||||
}
|
||||
if got := headHash(t, dir); got != tip {
|
||||
t.Fatalf("HEAD = %s, want the worktree left on the branch tip %s", got, tip)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrepareResumeCleansAbandonedFiles ensures a resume clears untracked
|
||||
// residue the way a fresh prepare does, so a prior interrupted run cannot leak
|
||||
// files into the next commit.
|
||||
func TestPrepareResumeCleansAbandonedFiles(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
m := newManager(t, newLocalRemote(t, "acme", "todo"))
|
||||
dir, branch, _ := prepareResume(t, m, 10, "fix: thing")
|
||||
|
||||
if err := os.WriteFile(filepath.Join(dir, "scratch.txt"), []byte("junk\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 10, "fix: thing", "", "bug", Target(branch)); err != nil {
|
||||
t.Fatalf("Prepare(resume) error = %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "scratch.txt")); !os.IsNotExist(err) {
|
||||
t.Errorf("scratch.txt still exists after resume; untracked residue was not cleaned")
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package worktree
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestScratchWorkspaceStartsEmptyAndStaysOutOfWorktrees(t *testing.T) {
|
||||
m := &Manager{WorkDir: t.TempDir()}
|
||||
dir, err := m.PrepareScratch(7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "left-over"), []byte("x"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := m.PrepareScratch(7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if entries, _ := os.ReadDir(again); len(entries) != 0 {
|
||||
t.Fatalf("second attempt's workspace holds %d entries, want an empty one", len(entries))
|
||||
}
|
||||
if strings.HasPrefix(again, m.Dir("scratch", "x", 7)) {
|
||||
t.Fatalf("scratch dir %q lies inside a worktree path", again)
|
||||
}
|
||||
if err := m.RemoveScratch(7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(again); !os.IsNotExist(err) {
|
||||
t.Fatalf("scratch dir still exists after RemoveScratch: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,462 +0,0 @@
|
||||
package worktree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
)
|
||||
|
||||
// slogCaptureHandler records all log records for asserting log output.
|
||||
type slogCaptureHandler struct {
|
||||
records []slog.Record
|
||||
}
|
||||
|
||||
func (h *slogCaptureHandler) Enabled(context.Context, slog.Level) bool { return true }
|
||||
|
||||
func (h *slogCaptureHandler) Handle(_ context.Context, r slog.Record) error {
|
||||
h.records = append(h.records, r)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *slogCaptureHandler) WithAttrs([]slog.Attr) slog.Handler { return h }
|
||||
func (h *slogCaptureHandler) WithGroup(string) slog.Handler { return h }
|
||||
|
||||
func setSlogCapture(t *testing.T) *slogCaptureHandler {
|
||||
t.Helper()
|
||||
prev := slog.Default()
|
||||
h := &slogCaptureHandler{}
|
||||
slog.SetDefault(slog.New(h))
|
||||
t.Cleanup(func() { slog.SetDefault(prev) })
|
||||
return h
|
||||
}
|
||||
|
||||
// TestPushSucceedsWhenPostPublicationMetadataFails covers BUG 1 (archie-core-1ysw.6).
|
||||
// A post-publication upstream metadata failure (e.g. read-only config or corrupted config)
|
||||
// must NOT cause Push to report an error when the remote ref was successfully published.
|
||||
// The failure must be logged as a warning instead of failing the task.
|
||||
func TestPushSucceedsWhenPostPublicationMetadataFails(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
issue int
|
||||
title string
|
||||
labels string
|
||||
file string
|
||||
content string
|
||||
commitMsg string
|
||||
configPerm os.FileMode
|
||||
useLogger bool
|
||||
}{
|
||||
{
|
||||
name: "read-only config causes warning with default logger but push succeeds",
|
||||
issue: 101,
|
||||
title: "feat: push robustness",
|
||||
labels: "feature",
|
||||
file: "feature.txt",
|
||||
content: "data\n",
|
||||
commitMsg: "feat: push robustness",
|
||||
configPerm: 0o400,
|
||||
useLogger: false,
|
||||
},
|
||||
{
|
||||
name: "read-only config causes warning with manager logger but push succeeds",
|
||||
issue: 102,
|
||||
title: "feat: custom logger push",
|
||||
labels: "feature",
|
||||
file: "custom.txt",
|
||||
content: "custom data\n",
|
||||
commitMsg: "feat: custom logger push",
|
||||
configPerm: 0o400,
|
||||
useLogger: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
logs := setSlogCapture(t)
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "push-test")
|
||||
m := newManager(t, host)
|
||||
if tc.useLogger {
|
||||
m.Log = slog.New(logs)
|
||||
}
|
||||
|
||||
dir, branch, err := m.Prepare(ctx, "acme", "push-test", testBase, tc.issue, tc.title, "", tc.labels, Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filepath.Join(dir, tc.file), []byte(tc.content), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
committed, err := m.CommitAll(ctx, dir, tc.commitMsg)
|
||||
if err != nil {
|
||||
t.Fatalf("CommitAll() error = %v", err)
|
||||
}
|
||||
if !committed {
|
||||
t.Fatal("CommitAll() = false, want true")
|
||||
}
|
||||
|
||||
// Make .git/config read-only so SetConfig will fail when recording upstream.
|
||||
cfgPath := filepath.Join(dir, ".git", "config")
|
||||
if err := os.Chmod(cfgPath, tc.configPerm); err != nil {
|
||||
t.Fatalf("Chmod() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chmod(cfgPath, 0o644) })
|
||||
|
||||
// Push should succeed despite the local metadata failure.
|
||||
err = m.Push(ctx, dir, branch)
|
||||
if err != nil {
|
||||
t.Fatalf("Push() error = %v, want nil (post-publication metadata failure should not fail push)", err)
|
||||
}
|
||||
|
||||
// Remote repository MUST have the branch published.
|
||||
bare := filepath.Join(host, "acme", "push-test.git")
|
||||
remote, err := git.PlainOpen(bare)
|
||||
if err != nil {
|
||||
t.Fatalf("open bare remote: %v", err)
|
||||
}
|
||||
if _, err := remote.Reference(plumbing.NewBranchReferenceName(branch), true); err != nil {
|
||||
t.Fatalf("branch %q not published on remote after Push: %v", branch, err)
|
||||
}
|
||||
|
||||
// A warning log must have been recorded for the upstream metadata failure.
|
||||
var warningFound bool
|
||||
for _, r := range logs.records {
|
||||
if r.Level >= slog.LevelWarn && strings.Contains(r.Message, "upstream") {
|
||||
warningFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !warningFound {
|
||||
t.Errorf("expected warning log about upstream metadata failure, got records: %v", logs.records)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecordUpstreamErrorPaths explicitly tests the error paths of recordUpstream.
|
||||
func TestRecordUpstreamErrorPaths(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(t *testing.T, dir string)
|
||||
wantSubstr string
|
||||
}{
|
||||
{
|
||||
name: "write repo config fails when config is read only",
|
||||
setup: func(t *testing.T, dir string) {
|
||||
cfgPath := filepath.Join(dir, ".git", "config")
|
||||
if err := os.Chmod(cfgPath, 0o400); err != nil {
|
||||
t.Fatalf("Chmod config read-only error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chmod(cfgPath, 0o644) })
|
||||
},
|
||||
wantSubstr: "write repo config",
|
||||
},
|
||||
{
|
||||
name: "read repo config fails when config is corrupted",
|
||||
setup: func(t *testing.T, dir string) {
|
||||
cfgPath := filepath.Join(dir, ".git", "config")
|
||||
if err := os.WriteFile(cfgPath, []byte("[unclosed section\nkey ="), 0o644); err != nil {
|
||||
t.Fatalf("corrupt config error = %v", err)
|
||||
}
|
||||
},
|
||||
wantSubstr: "read repo config",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "record-upstream")
|
||||
m := newManager(t, host)
|
||||
|
||||
dir, branch, err := m.Prepare(ctx, "acme", "record-upstream", testBase, 105, "feat: record upstream", "", "feature", Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("PlainOpen() error = %v", err)
|
||||
}
|
||||
|
||||
tc.setup(t, dir)
|
||||
|
||||
ref := plumbing.NewBranchReferenceName(branch)
|
||||
err = m.recordUpstream(r, branch, ref)
|
||||
if err == nil {
|
||||
t.Fatalf("recordUpstream() error = nil, want error containing %q", tc.wantSubstr)
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.wantSubstr) {
|
||||
t.Errorf("recordUpstream() error = %q, want substring %q", err.Error(), tc.wantSubstr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCommitAllHonoursCancellation covers BUG 2 (archie-core-1ysw.8).
|
||||
// A cancelled context must abort CommitAll before mutating index or HEAD.
|
||||
func TestCommitAllHonoursCancellation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cancelPre bool
|
||||
timeout bool
|
||||
wantError error
|
||||
}{
|
||||
{
|
||||
name: "pre-cancelled context returns context.Canceled without mutating index or HEAD",
|
||||
cancelPre: true,
|
||||
wantError: context.Canceled,
|
||||
},
|
||||
{
|
||||
name: "expired deadline returns context.DeadlineExceeded without mutating index or HEAD",
|
||||
timeout: true,
|
||||
wantError: context.DeadlineExceeded,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "cancel-test")
|
||||
m := newManager(t, host)
|
||||
|
||||
dir, _, err := m.Prepare(ctx, "acme", "cancel-test", testBase, 201, "feat: cancel test", "", "feature", Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
|
||||
// Capture HEAD commit before attempting any commit.
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("PlainOpen() error = %v", err)
|
||||
}
|
||||
headBefore, err := r.Head()
|
||||
if err != nil {
|
||||
t.Fatalf("Head() before error = %v", err)
|
||||
}
|
||||
|
||||
// Write a new file to the worktree.
|
||||
newFile := filepath.Join(dir, "work.txt")
|
||||
if err := os.WriteFile(newFile, []byte("uncommitted work\n"), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
|
||||
var testCtx context.Context
|
||||
var cancel context.CancelFunc
|
||||
if tc.cancelPre {
|
||||
testCtx, cancel = context.WithCancel(context.Background())
|
||||
cancel() // Pre-cancel
|
||||
} else if tc.timeout {
|
||||
var timeoutCancel context.CancelFunc
|
||||
testCtx, timeoutCancel = context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
|
||||
cancel = timeoutCancel
|
||||
}
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
committed, commitErr := m.CommitAll(testCtx, dir, "feat: should be aborted")
|
||||
if commitErr == nil {
|
||||
t.Fatalf("CommitAll() error = nil, want %v", tc.wantError)
|
||||
}
|
||||
if !errors.Is(commitErr, tc.wantError) {
|
||||
t.Fatalf("CommitAll() error = %v, want errors.Is(..., %v)", commitErr, tc.wantError)
|
||||
}
|
||||
if committed {
|
||||
t.Errorf("CommitAll() returned committed=true on cancelled context, want false")
|
||||
}
|
||||
|
||||
// Verify HEAD commit has NOT moved.
|
||||
headAfter, err := r.Head()
|
||||
if err != nil {
|
||||
t.Fatalf("Head() after error = %v", err)
|
||||
}
|
||||
if headBefore.Hash() != headAfter.Hash() {
|
||||
t.Errorf("HEAD mutated: before=%s, after=%s", headBefore.Hash(), headAfter.Hash())
|
||||
}
|
||||
|
||||
// Verify index has NOT staged the new file (worktree status should show it untracked).
|
||||
wt, err := r.Worktree()
|
||||
if err != nil {
|
||||
t.Fatalf("Worktree() error = %v", err)
|
||||
}
|
||||
status, err := wt.Status()
|
||||
if err != nil {
|
||||
t.Fatalf("Status() error = %v", err)
|
||||
}
|
||||
fileStatus := status.File("work.txt")
|
||||
if fileStatus.Staging != git.Untracked {
|
||||
t.Errorf("work.txt Staging status = %v, want Untracked (no index mutation)", fileStatus.Staging)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBranchNamingPrecedence covers BUG 3 (archie-core-1ysw.9).
|
||||
// Tests title-vs-label precedence, dead parameter removal, no-op removal,
|
||||
// and 60-char slug boundary agreement.
|
||||
func TestBranchNamingPrecedence(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
issue int
|
||||
title string
|
||||
labels string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "unlabeled issue with conventional prefix in title derives prefix from title",
|
||||
issue: 12,
|
||||
title: "fix: crash on empty payload",
|
||||
labels: "",
|
||||
want: "fix/12-crash-on-empty-payload",
|
||||
},
|
||||
{
|
||||
name: "unlabeled issue with scoped conventional prefix in title",
|
||||
issue: 13,
|
||||
title: "chore(deps): update go to 1.26",
|
||||
labels: "",
|
||||
want: "chore/13-update-go-to-126",
|
||||
},
|
||||
{
|
||||
name: "title conventional prefix takes precedence when title and labels disagree",
|
||||
issue: 14,
|
||||
title: "fix: wrong calculation",
|
||||
labels: "feature,enhancement",
|
||||
want: "fix/14-wrong-calculation",
|
||||
},
|
||||
{
|
||||
name: "title feat takes precedence when label says bug",
|
||||
issue: 15,
|
||||
title: "feat: new export option",
|
||||
labels: "bug",
|
||||
want: "feat/15-new-export-option",
|
||||
},
|
||||
{
|
||||
name: "unlabeled issue without conventional prefix falls back to default feat",
|
||||
issue: 16,
|
||||
title: "plain title without prefix",
|
||||
labels: "",
|
||||
want: "feat/16-plain-title-without-prefix",
|
||||
},
|
||||
{
|
||||
name: "title without prefix falls back to label",
|
||||
issue: 17,
|
||||
title: "unexpected crash on startup",
|
||||
labels: "bug",
|
||||
want: "fix/17-unexpected-crash-on-startup",
|
||||
},
|
||||
{
|
||||
name: "title without prefix with docs label",
|
||||
issue: 18,
|
||||
title: "update installation instructions",
|
||||
labels: "docs",
|
||||
want: "docs/18-update-installation-instructions",
|
||||
},
|
||||
{
|
||||
name: "title without prefix with refactor label",
|
||||
issue: 19,
|
||||
title: "clean up worker loops",
|
||||
labels: "refactor",
|
||||
want: "refactor/19-clean-up-worker-loops",
|
||||
},
|
||||
{
|
||||
name: "title without prefix with test label",
|
||||
issue: 20,
|
||||
title: "add integration tests",
|
||||
labels: "test",
|
||||
want: "test/20-add-integration-tests",
|
||||
},
|
||||
{
|
||||
name: "title with uppercase prefix normalizes correctly",
|
||||
issue: 21,
|
||||
title: "FIX: critical issue",
|
||||
labels: "",
|
||||
want: "fix/21-critical-issue",
|
||||
},
|
||||
{
|
||||
name: "colon in title that is not a commit type falls back to labels",
|
||||
issue: 22,
|
||||
title: "step 1: initial setup",
|
||||
labels: "chore",
|
||||
want: "chore/22-step-1-initial-setup",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := archieBranch(tc.issue, tc.title, tc.labels)
|
||||
if got != tc.want {
|
||||
t.Errorf("archieBranch(%d, %q, %q) = %q, want %q", tc.issue, tc.title, tc.labels, got, tc.want)
|
||||
}
|
||||
gotPrefix := branchPrefix(tc.title, tc.labels)
|
||||
wantPrefix, _, _ := strings.Cut(tc.want, "/")
|
||||
if gotPrefix != wantPrefix {
|
||||
t.Errorf("branchPrefix(%q, %q) = %q, want %q", tc.title, tc.labels, gotPrefix, wantPrefix)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBranchSlugBoundaryAgreesWithLimit tests the 60-char slug boundary.
|
||||
func TestBranchSlugBoundaryAgreesWithLimit(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
title string
|
||||
wantLen int
|
||||
wantSlug string
|
||||
maxCapLen int
|
||||
}{
|
||||
{
|
||||
name: "short slug under 60 chars",
|
||||
title: "short title",
|
||||
wantLen: 11,
|
||||
wantSlug: "short-title",
|
||||
maxCapLen: 60,
|
||||
},
|
||||
{
|
||||
name: "exact 60 char slug",
|
||||
title: strings.Repeat("a", 60),
|
||||
wantLen: 60,
|
||||
wantSlug: strings.Repeat("a", 60),
|
||||
maxCapLen: 60,
|
||||
},
|
||||
{
|
||||
name: "slug is capped at exactly 60 chars for long title",
|
||||
title: strings.Repeat("a", 80),
|
||||
wantLen: 60,
|
||||
wantSlug: strings.Repeat("a", 60),
|
||||
maxCapLen: 60,
|
||||
},
|
||||
{
|
||||
name: "long title with hyphens capped at 60",
|
||||
title: "this is a very long title that exceeds sixty characters easily and goes on and on",
|
||||
wantLen: 60,
|
||||
wantSlug: "this-is-a-very-long-title-that-exceeds-sixty-characters-easi",
|
||||
maxCapLen: 60,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
slug := branchSlug(tc.title)
|
||||
if len(slug) > tc.maxCapLen {
|
||||
t.Errorf("branchSlug(%q) length = %d, exceeds max cap %d", tc.title, len(slug), tc.maxCapLen)
|
||||
}
|
||||
if len(slug) != tc.wantLen {
|
||||
t.Errorf("branchSlug(%q) length = %d, want %d", tc.title, len(slug), tc.wantLen)
|
||||
}
|
||||
if slug != tc.wantSlug {
|
||||
t.Errorf("branchSlug(%q) = %q, want %q", tc.title, slug, tc.wantSlug)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user