Skip to content
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

Add option ignoremissingsubtests to ignore missing t.Parallel() in subtests #32

Merged
merged 1 commit into from
Aug 3, 2023

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Jul 20, 2023

In our codebase we've coded ourselves into a bit of a corner wherein
although all our top-level tests are safe to run concurrently, subtests
within any particular test are not due to the use of a shared set of
variables. It'd be nice to fix this of course, but doing so would
involved changing thousands of tests and many dozens of hours of work.

For any Go project, if you've managed to get to a place where all your
top-level tests within a package can run in parallel, that's pretty
good. You might not get the same granularity of work balancing that you
would if all your subtests were parallel too, but (depending somewhat on
the variance in timing between your tests) it gets like you like 80% of
the way there.

So here, I propose that the package adds a new -ignoremissingsubtests
flag. It's similar to the existing -i, but only ignores missing calls
to t.Parallel in subtests and ranges.

…n subtests

In our codebase we've coded ourselves into a bit of a corner wherein
although all our top-level tests are safe to run concurrently, subtests
_within_ any particular test are not due to the use of a shared set of
variables. It'd be nice to fix this of course, but doing so would
involved changing thousands of tests and many dozens of hours of work.

For any Go project, if you've managed to get to a place where all your
top-level tests within a package can run in parallel, that's pretty
good. You might not get the same granularity of work balancing that you
would if all your subtests were parallel too, but (depending somewhat on
the variance in timing between your tests) it gets like you like 80% of
the way there.

So here, I propose that the package adds a new `-ignoremissingsubtests`
flag. It's similar to the existing `-i`, but only ignores missing calls
to `t.Parallel` in subtests and ranges.
A few options can be activated by flag:

* `-i`: Ignore missing calls to `t.Parallel` and only report incorrect uses of it.
* `-ignoremissingsubtests`: Require that top-level tests specify `t.Parallel`, but don't require it in subtests (`t.Run(...)`).
Copy link
Contributor Author

@brandur brandur Jul 20, 2023

Choose a reason for hiding this comment

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

The flag naming here might be a bit of a mouthful, but I didn't want to pick another arbitrary one letter name like -i because longer term, that'll add to confusion (say more flags are added), and these flags will rarely be typed by human anyway.

In terms of -ignoremissingsubtests versus ignore-missing-subtests versus ignore_missing_subtests, I went with the first option because that seems to be the convention preferred by Go's Plan 9 style flag system (see go help build for some examples of this). With Linux flags I would've used ignore-missing-subtests. I did notice that some other linters use underscores for option names. I have no strong preference and happy to change this to whatever.

type parallelAnalyzer struct {
analyzer *analysis.Analyzer
ignoreMissing bool
ignoreMissingSubtests bool
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put in a wrapper struct like this to make it much simpler and less hacky to set extra options from the test suite. The convention is inspired by Forbidigo (another Go linter) whose code I referenced.

@brandur
Copy link
Contributor Author

brandur commented Jul 20, 2023

@kunwardeep Hey, I saw that the project seems to be in a semi-maintained state, but it's still in golangci-lint's core, and AFAIK there isn't a better maintained alternative. Thoughts on this idea? It'd be useful for us, but I figured others might find it useful too.

@@ -0,0 +1,163 @@
package t
Copy link
Contributor Author

@brandur brandur Jul 20, 2023

Choose a reason for hiding this comment

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

This is just a full carbon copy of t_test.go with some of the expected warnings removed.

@kunwardeep
Copy link
Owner

Hey @brandur i am glad this liner is helping your and your team. I have been sick for a week, hence the delay in response.

I'll have a look at this PR this week. Thanks for contributing.

@brandur
Copy link
Contributor Author

brandur commented Jul 24, 2023

Thanks Kunwardeep! And no worries — hope you're feeling better soon.

@kunwardeep kunwardeep merged commit 1adbbc8 into kunwardeep:main Aug 3, 2023
@brandur brandur deleted the brandur-ignore-subtests branch August 3, 2023 01:16
@brandur
Copy link
Contributor Author

brandur commented Aug 3, 2023

Thanks @kunwardeep. Opened golangci/golangci-lint#3985 to see if we can get this into golangci-lint!

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.

2 participants