golangci-lint
golangci-lint copied to clipboard
Add "nosleep" linter
Your feature request related to a problem? Please describe.
Using time.Sleep in tests is a mistake in most of cases.
Describe the solution you'd like.
Linter should detect time.Sleep in tests.
Describe alternatives you've considered.
TBH as golangci-link contains nearly hundred of linters it's hard to say is some of existing ones can be configured to do this.
Additional context.
https://xeiaso.net/blog/nosleep
Hi!
You can use forbidigo linter for this case. Just put this lines to config:
linters:
# ...
enable:
# ...
- forbidigo
# ...
# ...
linters-settings:
# ...
forbidigo:
forbid:
- p: time.Sleep
msg: "Avoid time.Sleep in you code please"
#...
Isn't this will apply both to tests and code, instead of just tests?
You can use the issues section to configure files to include/exclude:
issues:
exclude-rules:
- path-except: _test\.go
linters:
- forbidigo
Yes, but this will exclude all rules of the linter, not just this one.
Probably it's possible to except using text:
instead of linters:
, but it feels like a workaround: error message text to match may change in future versions, linter will still spend time applying this rule to non-tests, etc.