Skip to content

Replace log.Fatal in tests, add t.Helper, cover NewFile#64

Merged
dolph merged 1 commit into
mainfrom
claude/fix-issue-33-test-helpers
May 18, 2026
Merged

Replace log.Fatal in tests, add t.Helper, cover NewFile#64
dolph merged 1 commit into
mainfrom
claude/fix-issue-33-test-helpers

Conversation

@dolph
Copy link
Copy Markdown
Owner

@dolph dolph commented May 18, 2026

Summary

Three test-infrastructure fixes from #33. No production code changes.

  • log.Fatal* -> t.Fatalf / b.Fatalf in newTestFile, newTestDir, and TestHandleFileWithIgnoredDir. log.Fatal calls os.Exit(1), which kills the test binary, leaks temp files (deferred cleanup doesn't run), and prevents subsequent tests from running. newTestFile / newTestDir now take testing.TB as the first arg so they work for both *testing.T and *testing.B call sites; all call sites are updated.
  • Add t.Helper() / b.Helper() as the first line of every test helper that takes *testing.T or *testing.B: assertFileExists, assertFileNonexistent, assertPathExistsAfterRename, assertNewContentsOfFile, assertRandomStringLength, newTestFile, newTestDir, and CloneRepoToTestDir. Failures now point to the call site, not the helper body.
  • Replace empty TestNewFile with table-driven coverage of NewFile's path-resolution behavior: absolute path returned cleaned, redundant separators collapsed, .. resolved, relative path resolved to absolute, and "." resolved to the working directory. Uses t.TempDir() and t.Run(tc.name, ...).

Judgment calls

  • TestNewFile error path not covered. NewFile currently calls log.Fatalf if filepath.Abs fails, which would kill the test binary. Per scope discipline, exposing this error path belongs to log.Fatal from worker goroutines leaves the tree in a partially-modified state #6's refactor, not this PR. A comment in TestNewFile documents the omission.
  • newTestFile / newTestDir signature change to testing.TB. Necessary so CloneRepoToTestDir (a *testing.B helper) can call newTestDir after the latter stops using log.Fatal. All in-tree call sites are updated. No production code is affected.
  • Adjacent defects deliberately left alone (would expand scope into log.Fatal from worker goroutines leaves the tree in a partially-modified state #6 or new issues): CloneRepoToTestDir's defer os.Remove(d.Path) runs before the returned dir is used (pre-existing bug); b.Errorf instead of b.Fatalf after a failed git clone; TestHandleFileWithIgnoredDir's use of os.TempDir() instead of t.TempDir().

Verifying t.Helper() correctness

Temporarily added a throwaway test that calls assertFileExists against a nonexistent path. The reported failure line was the call site (zz_helper_sanity_test.go:10), not the helper body (find_replace_test.go:62), confirming t.Helper() works as intended. The throwaway file was removed before commit.

Test plan

  • gofmt -l . -- no output
  • go vet ./... -- no output
  • go build ./... -- no output
  • go test -race ./... -- PASS (ok github.com/dolph/find-replace 1.027s)
  • ./build.sh -- PASS
  • Verified t.Helper() attribution via throwaway test (described above)

Closes #33


Generated by Claude Code

Three test-infrastructure fixes from issue #33:

1. Replace every test-side log.Fatal* with t.Fatalf / b.Fatalf.
   log.Fatal calls os.Exit(1), which kills the entire test binary
   without running any deferred cleanup (so temp files leak), without
   printing the test name that failed, and prevents subsequent tests
   from running. Affected helpers: newTestFile, newTestDir,
   TestHandleFileWithIgnoredDir. To make newTestFile/newTestDir
   usable from both *testing.T and *testing.B call sites, their
   signatures now take testing.TB as the first argument. All call
   sites are updated.

2. Add t.Helper() / b.Helper() to every test helper that takes
   *testing.T or *testing.B: assertFileExists, assertFileNonexistent,
   assertPathExistsAfterRename, assertNewContentsOfFile,
   assertRandomStringLength, newTestFile, newTestDir, and
   CloneRepoToTestDir. Failures are now attributed to the call site
   instead of the helper body. Verified by inducing a failure in
   assertFileExists from a throwaway test file: the reported
   file:line was the call site, not the helper.

3. Replace the empty TestNewFile with real, table-driven coverage:
   absolute-path inputs returned cleaned, redundant separators
   collapsed, .. resolved, relative paths resolved to absolute, and
   the working-directory case ("."). The filepath.Abs error path is
   not covered here because NewFile currently calls log.Fatalf on
   that failure (kills the test binary); the error path becomes
   testable when NewFile is refactored per issue #6.

No production code is changed.

Closes #33
@dolph dolph added the release:skip label May 18, 2026 — with Claude
@dolph dolph merged commit f060daf into main May 18, 2026
2 checks passed
@dolph dolph deleted the claude/fix-issue-33-test-helpers branch May 18, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test helpers use log.Fatal (kills test binary), assertion helpers lack t.Helper(), and TestNewFile is empty

2 participants