Replace log.Fatal in tests, add t.Helper, cover NewFile#64
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three test-infrastructure fixes from #33. No production code changes.
log.Fatal*->t.Fatalf/b.FatalfinnewTestFile,newTestDir, andTestHandleFileWithIgnoredDir.log.Fatalcallsos.Exit(1), which kills the test binary, leaks temp files (deferred cleanup doesn't run), and prevents subsequent tests from running.newTestFile/newTestDirnow taketesting.TBas the first arg so they work for both*testing.Tand*testing.Bcall sites; all call sites are updated.t.Helper()/b.Helper()as the first line of every test helper that takes*testing.Tor*testing.B:assertFileExists,assertFileNonexistent,assertPathExistsAfterRename,assertNewContentsOfFile,assertRandomStringLength,newTestFile,newTestDir, andCloneRepoToTestDir. Failures now point to the call site, not the helper body.TestNewFilewith table-driven coverage ofNewFile's path-resolution behavior: absolute path returned cleaned, redundant separators collapsed,..resolved, relative path resolved to absolute, and"."resolved to the working directory. Usest.TempDir()andt.Run(tc.name, ...).Judgment calls
TestNewFileerror path not covered.NewFilecurrently callslog.Fatalfiffilepath.Absfails, 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 inTestNewFiledocuments the omission.newTestFile/newTestDirsignature change totesting.TB. Necessary soCloneRepoToTestDir(a*testing.Bhelper) can callnewTestDirafter the latter stops usinglog.Fatal. All in-tree call sites are updated. No production code is affected.CloneRepoToTestDir'sdefer os.Remove(d.Path)runs before the returned dir is used (pre-existing bug);b.Errorfinstead ofb.Fatalfafter a failedgit clone;TestHandleFileWithIgnoredDir's use ofos.TempDir()instead oft.TempDir().Verifying
t.Helper()correctnessTemporarily added a throwaway test that calls
assertFileExistsagainst 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), confirmingt.Helper()works as intended. The throwaway file was removed before commit.Test plan
gofmt -l .-- no outputgo vet ./...-- no outputgo build ./...-- no outputgo test -race ./...-- PASS (ok github.com/dolph/find-replace 1.027s)./build.sh-- PASSt.Helper()attribution via throwaway test (described above)Closes #33
Generated by Claude Code