Files
boshu2__agentops/scripts/check-test-isolation.sh
T
Bo 3bab897009 test(cli): migrate dedup_test.go to t.Chdir + lock isolation ratchet 163->151 (ag-4nif #dedup-tchdir) (#773)
## What

Migrate `dedup_test.go`'s 6 manual `origDir, _ := os.Getwd();
os.Chdir(tmp); defer os.Chdir(origDir)` blocks to Go 1.20+
`t.Chdir(tmp)` — auto-restores cwd **and** refuses `t.Parallel`,
pre-empting the latent flake class flagged by **ag-4nif** / **ag-k38x**
(raw forms mutate process-global cwd). No `t.Parallel` in the file;
behavior-preserving.

Each block held two `os.Chdir(` references (the call + the defer
restore), so 6 blocks drop the repo-wide test-isolation count by **12
(163→151)**. I lowered `BASELINE_CHDIR` in
`scripts/check-test-isolation.sh` to **lock the gain** (the ratchet is
only-goes-down).

## Scope / no-collision note

`os.Chdir` only. The 3 redundant `os.Setenv` restores in
`rpi_reliability_test.go` are **already handled by the open PR #772
(ag-k38x)** — I deliberately do **not** touch that file here, to avoid a
merge collision (an earlier draft of this branch did; it was reverted).
**ag-k38x is the canonical tracker; ag-4nif/ag-y4ac are duplicates** —
worth closing as dup-of-ag-k38x during triage.

## Evidence

```
bash scripts/check-test-isolation.sh        # PASS (os.Chdir=151/151, os.Setenv=22/22)
cd cli
go build ./...                              # OK
go vet ./cmd/ao/                            # No issues found
go test ./cmd/ao/ -run 'TestRunDedup|TestMergeDedupGroups' -count=1   # 17 pass
gofmt -l                                     # clean
```

Net: −19 / +8 across `dedup_test.go` + the ratchet baseline.

Closes-scenario: ag-4nif#dedup-tchdir
Bounded-context: BC5-Runtime
Evidence: ratchet PASS + go build/vet/test (17 pass) + gofmt clean
2026-06-06 11:23:54 -04:00

54 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# check-test-isolation.sh (ag-k38x) — ratchet: raw os.Chdir / os.Setenv in Go test
# files must not GROW.
#
# Idiomatic test isolation is t.Chdir / t.Setenv: they auto-restore on cleanup AND
# fail fast if the test (or a parent) called t.Parallel. Raw os.Chdir/os.Setenv mutate
# process-global cwd/env and are a latent flake landmine the moment any test opts into
# parallelism (cf. ag-jfzs — the memory-sync flake was exactly this class). This gate
# pre-empts the class by preventing NEW raw forms.
#
# This is a BASELINE RATCHET, not a zero-tolerance ban: ~163 os.Chdir + 22 os.Setenv
# inline sites predate it. The baseline only goes DOWN — new raw forms must use the
# t.* idiom (or the sanctioned chdirTemp/chdirTo helpers in cli/cmd/ao/testutil_test.go,
# which are excluded here and themselves use t.Chdir). Migrate inline sites to drive the
# baselines toward zero, then lower the constants below to lock each gain (ag-k38x.1).
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
# Baselines captured 2026-06-03 (excludes the testutil_test.go helper home).
# CHDIR lowered to 137 (ag-4nif): dedup_test.go migrated 6 raw os.Chdir blocks to t.Chdir,
# combined with the in-flight migrations already on main, locks the merged actual count.
# os.Setenv lowered 22->12 on 2026-06-06 (ag-k38x #bulk-migration). The remaining 12
# are intentional and cannot use t.Setenv: TestMain sites (no *testing.T), string-literal
# fixtures in fix_cliconfig_test.go, and unset-semantics helpers (t.Setenv cannot unset).
BASELINE_CHDIR=137
BASELINE_SETENV=12
chdir=$(grep -rho --include='*_test.go' --exclude='testutil_test.go' 'os\.Chdir(' cli 2>/dev/null | wc -l | tr -d ' ')
setenv=$(grep -rho --include='*_test.go' --exclude='testutil_test.go' 'os\.Setenv(' cli 2>/dev/null | wc -l | tr -d ' ')
rc=0
if [ "$chdir" -gt "$BASELINE_CHDIR" ]; then
echo "FAIL: raw os.Chdir in *_test.go rose to $chdir (baseline $BASELINE_CHDIR)."
echo " Use t.Chdir (auto-restores, blocks t.Parallel) or the testutil chdirTemp/chdirTo helpers."
rc=1
fi
if [ "$setenv" -gt "$BASELINE_SETENV" ]; then
echo "FAIL: raw os.Setenv in *_test.go rose to $setenv (baseline $BASELINE_SETENV)."
echo " Use t.Setenv (auto-restores, blocks t.Parallel)."
rc=1
fi
if [ "$chdir" -lt "$BASELINE_CHDIR" ] || [ "$setenv" -lt "$BASELINE_SETENV" ]; then
echo "NOTE: isolation ratchet improved (os.Chdir=$chdir<=$BASELINE_CHDIR, os.Setenv=$setenv<=$BASELINE_SETENV)."
echo " Lower BASELINE_CHDIR/BASELINE_SETENV in $(basename "$0") to lock the gain."
fi
if [ "$rc" -eq 0 ]; then
echo "PASS: test-isolation ratchet (os.Chdir=$chdir/$BASELINE_CHDIR, os.Setenv=$setenv/$BASELINE_SETENV)"
fi
exit "$rc"