From e268d0bf534fb58c9e8ce93099f07abe6d27eb36 Mon Sep 17 00:00:00 2001 From: Boden Fuller Date: Sat, 6 Jun 2026 02:46:44 -0400 Subject: [PATCH] test(cli): migrate dedup_test.go to t.Chdir + lock isolation ratchet 163->151 (ag-4nif #dedup-tchdir) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrate dedup_test.go's 6 manual `os.Getwd()/os.Chdir/defer-restore` blocks to Go 1.20+ t.Chdir(tmp), which auto-restores cwd AND refuses t.Parallel — pre-empting the latent flake class flagged by ag-4nif/ag-k38x. No t.Parallel in the file; behavior-preserving. Each block held two `os.Chdir(` refs (the call + the defer), so 6 blocks drop the repo-wide test-isolation count by 12 (163->151). Lower BASELINE_CHDIR in scripts/check-test-isolation.sh to lock the gain (the ratchet is only-goes-down). Scope: os.Chdir only. The 3 redundant os.Setenv restores in rpi_reliability_test.go are already handled by the open PR #772 (ag-k38x), so they are deliberately NOT touched here to avoid a merge collision. ag-k38x is the canonical tracker; ag-4nif/ag-y4ac are its duplicates. Closes-scenario: ag-4nif#dedup-tchdir Bounded-context: BC5-Runtime Evidence: scripts/check-test-isolation.sh PASS (os.Chdir=151/151); go build ./... + go vet ./cmd/ao + go test ./cmd/ao -run 'TestRunDedup|TestMergeDedupGroups' (17 pass); gofmt clean --- cli/cmd/ao/dedup_test.go | 24 ++++++------------------ scripts/check-test-isolation.sh | 3 ++- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/cli/cmd/ao/dedup_test.go b/cli/cmd/ao/dedup_test.go index aebdd64cb..f12d18c48 100644 --- a/cli/cmd/ao/dedup_test.go +++ b/cli/cmd/ao/dedup_test.go @@ -588,9 +588,7 @@ func TestMergeDedupGroups_DryRun(t *testing.T) { func TestRunDedup_NoDirs(t *testing.T) { tmp := t.TempDir() - origDir, _ := os.Getwd() - os.Chdir(tmp) - defer func() { _ = os.Chdir(origDir) }() + t.Chdir(tmp) r, w, _ := os.Pipe() origStdout := os.Stdout os.Stdout = w @@ -610,9 +608,7 @@ func TestRunDedup_NoDirs(t *testing.T) { func TestRunDedup_EmptyDirs(t *testing.T) { tmp := t.TempDir() os.MkdirAll(filepath.Join(tmp, ".agents", "learnings"), 0o755) - origDir, _ := os.Getwd() - os.Chdir(tmp) - defer func() { _ = os.Chdir(origDir) }() + t.Chdir(tmp) r, w, _ := os.Pipe() origStdout := os.Stdout os.Stdout = w @@ -634,9 +630,7 @@ func TestRunDedup_TextOutput(t *testing.T) { ld := filepath.Join(tmp, ".agents", "learnings") os.MkdirAll(ld, 0o755) os.WriteFile(filepath.Join(ld, "a.md"), []byte("---\ntitle: A\n---\nUnique A."), 0o644) - origDir, _ := os.Getwd() - os.Chdir(tmp) - defer func() { _ = os.Chdir(origDir) }() + t.Chdir(tmp) origOutput := output output = "table" defer func() { output = origOutput }() @@ -664,9 +658,7 @@ func TestRunDedup_TextOutputWithDuplicates(t *testing.T) { body := "Identical body." os.WriteFile(filepath.Join(ld, "d1.md"), []byte("---\ntitle: D1\n---\n"+body), 0o644) os.WriteFile(filepath.Join(ld, "d2.md"), []byte("---\ntitle: D2\n---\n"+body), 0o644) - origDir, _ := os.Getwd() - os.Chdir(tmp) - defer func() { _ = os.Chdir(origDir) }() + t.Chdir(tmp) origOutput := output output = "table" defer func() { output = origOutput }() @@ -693,9 +685,7 @@ func TestRunDedup_MergeDryRun(t *testing.T) { body := "Same merge." os.WriteFile(filepath.Join(ld, "m1.md"), []byte("---\nutility: 0.9\n---\n"+body), 0o644) os.WriteFile(filepath.Join(ld, "m2.md"), []byte("---\nutility: 0.3\n---\n"+body), 0o644) - origDir, _ := os.Getwd() - os.Chdir(tmp) - defer func() { _ = os.Chdir(origDir) }() + t.Chdir(tmp) origMerge := dedupMerge dedupMerge = true defer func() { dedupMerge = origMerge }() @@ -723,9 +713,7 @@ func TestRunDedup_MergeNoDuplicates(t *testing.T) { ld := filepath.Join(tmp, ".agents", "learnings") os.MkdirAll(ld, 0o755) os.WriteFile(filepath.Join(ld, "u.md"), []byte("---\ntitle: U\n---\nUnique."), 0o644) - origDir, _ := os.Getwd() - os.Chdir(tmp) - defer func() { _ = os.Chdir(origDir) }() + t.Chdir(tmp) origMerge := dedupMerge dedupMerge = true defer func() { dedupMerge = origMerge }() diff --git a/scripts/check-test-isolation.sh b/scripts/check-test-isolation.sh index cfb4abc7c..f2cb05e8b 100755 --- a/scripts/check-test-isolation.sh +++ b/scripts/check-test-isolation.sh @@ -19,7 +19,8 @@ REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$REPO_ROOT" # Baselines captured 2026-06-03 (excludes the testutil_test.go helper home). -BASELINE_CHDIR=163 +# CHDIR lowered 163->151 (ag-4nif): dedup_test.go migrated 6 raw os.Chdir blocks to t.Chdir. +BASELINE_CHDIR=151 BASELINE_SETENV=22 chdir=$(grep -rho --include='*_test.go' --exclude='testutil_test.go' 'os\.Chdir(' cli 2>/dev/null | wc -l | tr -d ' ')