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

testifylint->nil-compare - Should we enable it? #15537

Closed
zak-pawel opened this issue Jun 20, 2024 · 2 comments · Fixed by #15566
Closed

testifylint->nil-compare - Should we enable it? #15537

zak-pawel opened this issue Jun 20, 2024 · 2 comments · Fixed by #15566
Labels

Comments

@zak-pawel
Copy link
Collaborator

Description

This issue starts a discussion about enabling:

  • linter: testifylint - Checks usage of github.com/stretchr/testify.
  • checker: nil-compare - Protection from bugs and more appropriate testify API with clearer failure message.

Example

Before:

assert.Equal(t, nil, value)
assert.EqualValues(t, nil, value)
assert.Exactly(t, nil, value)

assert.NotEqual(t, nil, value)
assert.NotEqualValues(t, nil, value)

After:

assert.Nil(t, value)
assert.NotNil(t, value)

Using untyped nil in the functions above along with a non-interface type does not make sense:

assert.Equal(t, nil, eventsChan)    // Always fail.
assert.NotEqual(t, nil, eventsChan) // Always pass.

The right way:

assert.Equal(t, (chan Event)(nil), eventsChan)
assert.NotEqual(t, (chan Event)(nil), eventsChan)

But in the case of Equal, NotEqual and Exactly type casting approach still doesn't work for the function type.
The best option here is to just use Nil / NotNil (see stretchr/testify#1524).

Expected output

Decision about enabling or not enabling this checker.

Findings

For this checker, the following findings were found in the current codebase:

plugins/inputs/intel_dlb/intel_dlb_test.go:424:3       testifylint  nil-compare: use require.Nil
plugins/outputs/postgresql/table_source_test.go:280:2  testifylint  nil-compare: use require.Nil
plugins/outputs/postgresql/table_source_test.go:283:2  testifylint  nil-compare: use require.Nil

Additional configuration

For this checker, no additional configuration can be provided.

@srebhan
Copy link
Member

srebhan commented Jun 24, 2024

I'm absolutely in favor of this one! This makes tests much more readable I think.

@srebhan srebhan removed their assignment Jun 24, 2024
@powersj
Copy link
Contributor

powersj commented Jun 25, 2024

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants