Skip to content

fix: Fix cleanup in test helpers, prefer defer in tests #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 25, 2022

Conversation

mafredri
Copy link
Member

This PR was sparked by #3109 (comment).

Taking a closer look at uses of t.Cleanup there were some places where require interrupts cleanup.

Motivation for preferring defer outside of helpers is that mixed use of t.Cleanup and defer result in unexpected execution order, ideally we should strive to only use t.Cleanup in helper functions.

  • fix: Change uses of t.Cleanup -> defer in test bodies
  • fix: Ensure t.Cleanup is not aborted by require
  • chore: Add helper annotations

@mafredri mafredri self-assigned this Jul 22, 2022
@mafredri mafredri requested a review from a team July 22, 2022 12:18
if runtime.GOOS == "windows" {
// Closing the opened files for cleanup.
err = urlFile.Close()
require.NoError(t, err)
assert.NoError(t, err)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert here, to avoid interrupting Close below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should comment this in the file so it doesn't get reverted

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of agree, but I'm less worried about this old code changing than new (incorrect) code going in. This is something everyone using require must be very aware of (it halts execution flow).

@mafredri
Copy link
Member Author

Looks like I overlooked the fact that sometimes t.Cleanup is used to clean up after parallel sub-tests, will revert those changes.

@kylecarbs
Copy link
Member

We should probably create a linter for this. cc @Emyrk our in-house linting DSL expert

Copy link
Member

@deansheather deansheather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Linter would be cool and should be easy to detect all instances of t.Cleanup, with nolint for every exception

@@ -94,6 +94,8 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
t.Cleanup(func() { close(tickerCh) })

ctx, cancelFunc := context.WithCancel(context.Background())
defer t.Cleanup(cancelFunc) // Defer to ensure cancelFunc is executed first.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does defer cancelFunc() not work here? I would expect defer to run before cleanups.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on the other instance: https://github.com/coder/coder/pull/3113/files#r928922910.

@@ -143,6 +143,8 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)
}

ctx, cancelFunc := context.WithCancel(context.Background())
defer t.Cleanup(cancelFunc) // Defer to ensure cancelFunc is executed first.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a helper function, defer cancelFunc() would run before the calling test gets a chance to do anything with it. And a t.Cleanup without defer would cancel the context too late, it would not trigger teardown. So I realize this is a bit of a sneaky way to do it, but this way t.Cleanup(cancelFunc) will be left on top of the cleanup stack when this function exits, triggering the teardown we want (and same order as before the refactor).

@mafredri mafredri force-pushed the mafredri/test-cleanup-defer-fixes branch from 193bb78 to b624871 Compare July 25, 2022 14:59
@mafredri
Copy link
Member Author

mafredri commented Jul 25, 2022

Writing the rule would be fairly straightforward:

func testCleanup(m dsl.Matcher) {
	m.Import("testing")

	m.Match(`t.Cleanup($*fn)`).
		Where(m["fn"].Type.Is("func()")).
		Report("Only call t.Cleanup in helpers.").
		Suggest("defer $fn()")
}

But I'm not sure demanding a //nolint for every cleanup in every helper function is a great developer experience.

Is there a way we could improve upon this @Emyrk? For instance, if t.Helper() is called before t.Cleanup == OK?

EDIT: Looks the above ruleguard also only works for single lines, if it's a multi-line function it won't be tagged.

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I always kinda wondered if t.Cleanup vs defer actually made an impact.

@Emyrk
Copy link
Member

Emyrk commented Jul 25, 2022

Writing the rule would be fairly straightforward:

func testCleanup(m dsl.Matcher) {
	m.Import("testing")

	m.Match(`t.Cleanup($*fn)`).
		Where(m["fn"].Type.Is("func()")).
		Report("Only call t.Cleanup in helpers.").
		Suggest("defer $fn()")
}

But I'm not sure demanding a //nolint for every cleanup in every helper function is a great developer experience.

Is there a way we could improve upon this @Emyrk? For instance, if t.Helper() is called before t.Cleanup == OK?

EDIT: Looks the above ruleguard also only works for single lines, if it's a multi-line function it won't be tagged.

Yea matching negative cases is really tricky, but I think we can dial the Cleanup match to prevent false positives (helper funcs). We can expand the match to match in a whole test function, as I think helper functions have different signatures.

	m.Import("testing")

	m.Match(`
	$f(t *testing.T) {
		$*_
		t.Cleanup($*_)
		$*_
	}
	`).
		Where(m["f"].Text.Matches("^Test")).
		Report("Only call t.Cleanup in helpers.").
		At(m["c"])

The downside to this is that if the function calls t.Cleanup() twice, only the 1st will match. You fix it, then run the linter, and the second matches. So you don't get both matches in 1 go 🤷‍♂️

@mafredri
Copy link
Member Author

@Emyrk Yeah, that may be the best approach. Thanks for the sample. We might want to add a separate case for t.Run functions as well since those won't match ^Test.

@mafredri
Copy link
Member Author

Created #3181 to track the linting request and get this merged.

@mafredri mafredri merged commit 6916d34 into main Jul 25, 2022
@mafredri mafredri deleted the mafredri/test-cleanup-defer-fixes branch July 25, 2022 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants