golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

Add "nosleep" linter

Open powerman opened this issue 1 year ago • 4 comments

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

powerman avatar Jul 19 '23 15:07 powerman

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"
  #...

dshemin avatar Aug 24 '23 03:08 dshemin

Isn't this will apply both to tests and code, instead of just tests?

powerman avatar Aug 24 '23 16:08 powerman

You can use the issues section to configure files to include/exclude:

issues:
  exclude-rules:
    - path-except: _test\.go
      linters:
        - forbidigo

bombsimon avatar Aug 27 '23 13:08 bombsimon

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.

powerman avatar Aug 27 '23 18:08 powerman