test(formatters): FailNow on the running subtest, not the parent#759
Open
nikolauspschuetz wants to merge 1 commit into
Open
test(formatters): FailNow on the running subtest, not the parent#759nikolauspschuetz wants to merge 1 commit into
nikolauspschuetz wants to merge 1 commit into
Conversation
Test_FmtOutput set the package-level tT to the parent *testing.T once, but the scenario/step hooks in fmtOutputTest call assert.FailNow(tT, ...) while running inside each t.Run subtest (on the subtest's goroutine). Calling FailNow on the parent test from a subtest's goroutine is unsafe per the testing docs and can misattribute or mishandle a failure. Assign tT to the current subtest's *testing.T at the start of each returned test function instead, so FailNow targets the subtest that is actually running, on its own goroutine. Signed-off-by: Nikolaus Schuetz <nikolauspschuetz@gmail.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.
Summary
Test_FmtOutputruns each formatter/feature combination as at.Run(...)subtest, and the scenario/step hooks registered infmtOutputTestcallassert.FailNow(tT, ...)when they find unexpected attachments. ButtTis a package-level*testing.Tset once to the parentTest_FmtOutput:So a hook running inside a subtest calls
FailNowon the parent test, from the subtest's goroutine.testingdocuments thatFailNowmust be called from the goroutine running the test it belongs to; calling it on the parent from a child goroutine can misattribute the failure or mishandle theGoexit, rather than cleanly failing the specific subtest that broke.Fix
Assign
tTto the current subtest's*testing.Tat the start of each returned test function, soFailNowtargets the subtest that is actually running, on its own goroutine:Subtests here run serially (no
t.Parallel()) and godog runs the scenarios serially (noOptions.Concurrency), so the sharedtTis only ever written/read for the one subtest in flight — no data race.Verification
go test ./internal/formatters/— passes (ok ... 0.764s); no behaviour change on the passing path.gofmt/go vetclean.This is a correctness fix on the failure path (a mis-targeted
FailNowonly manifests when a hook assertion actually fails), so it does not change the green-path output; the value is that a real failure now fails the right subtest cleanly.