test: make "missing path" assertions hermetic - #34
Merged
Conversation
Fifteen tool test suites hardcoded absolute paths such as "/nonexistent/file.txt" to assert not-found behaviour. That assumes the path is absent from whatever machine runs the tests -- guaranteed on no platform, and actively wrong on Windows, where "/nonexistent/file.txt" resolves to C:\nonexistent\file.txt, an ordinary and creatable location. When such a path does exist the assertion silently inverts. On a machine with a stray C:\nonexistent\path, TestDu_NonExistent walked a real directory tree, collected zero errors, and failed -- while du itself was behaving correctly. The other fourteen sites carry the same latent fault. Add internal/testutil.MissingPath, which anchors the path to t.TempDir(): a directory the test framework creates empty and removes on cleanup, so the leaf name is reliably absent everywhere. Use it at all fifteen sites. Co-authored-by: u84u <u84u@aol.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Problem
Fifteen tool test suites hardcode an absolute path to assert not-found behaviour:
This assumes the path is absent from whatever machine runs the tests. That holds on no platform by guarantee, and on Windows it is actively wrong:
/nonexistent/file.txtresolves toC:\nonexistent\file.txt, an ordinary creatable location.When the path does exist, the assertion silently inverts — the test stops exercising the missing-file branch and starts exercising a real file.
This is not hypothetical. On a machine carrying a stray
C:\nonexistent\path(a bare git repo, unrelated to this project),TestDu_NonExistentfails onmaster:duis correct here — it walked a directory that genuinely exists and correctly reported no errors. The test is what's broken. The other fourteen sites carry the identical latent fault and fail the same way given the right filesystem state.Fix
Add
internal/testutil.MissingPath, which anchors the path tot.TempDir()— a directory the test framework creates empty and removes on cleanup, so the leaf name is reliably absent on every platform:Then swap all fifteen call sites (
awk,cat,checksums,diff,du,file,find,head,ls,realpath,sed,stat,tail,tar,wc). Leaf names are preserved so extension-sensitive tools (file,tar) keep testing what they tested before.Verification
go test ./...— full suite green, with the strayC:\nonexistent\pathstill present on disk, which is what demonstrates the hermeticity rather than a cleanup.go vet ./...clean.gofmt -lreports no new entries (pre-existing formatting drift in non-test sources is untouched and out of scope here).Test-only change: no tool behaviour is modified.