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

new checker suite-broken-parallel #118

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ https://golangci-lint.run/usage/linters/#testifylint
| [negative-positive](#negative-positive) | ✅ | ✅ |
| [nil-compare](#nil-compare) | ✅ | ✅ |
| [require-error](#require-error) | ✅ | ❌ |
| [suite-broken-parallel](#suite-broken-parallel) | ✅ | ✅ |
| [suite-dont-use-pkg](#suite-dont-use-pkg) | ✅ | ✅ |
| [suite-extra-assert-call](#suite-extra-assert-call) | ✅ | ✅ |
| [suite-subtest-run](#suite-subtest-run) | ✅ | ❌ |
Expand Down Expand Up @@ -667,11 +668,61 @@ Also, to minimize false positives, `require-error` ignores:

---

### suite-dont-use-pkg
### suite-broken-parallel

```go
import "github.com/stretchr/testify/assert"
func (s *MySuite) SetupTest() {
s.T().Parallel() ❌
}

// And other hooks...

func (s *MySuite) TestSomething() {
s.T().Parallel() ❌

for _, tt := range cases {
s.Run(tt.name, func() {
s.T().Parallel() ❌
})

s.T().Run(tt.name, func(t *testing.T) {
t.Parallel() ❌
})
}
}
```

**Autofix**: true. <br>
**Enabled by default**: true. <br>
**Reason**: Protection from undefined behaviour.

`v1` of `testify` doesn't support suite's parallel tests and subtests.

Depending on [case](./analyzer/testdata/src/debug/suite_broken_parallel_test.go) using of `t.Parallel()` leads to

- data race
- panic
- non-working suite hooks
- silent ignoring of this directive

So, `testify`'s maintainers recommend discourage parallel tests inside suite.

<details>
<summary>Related issues...</summary>

- https://github.com/stretchr/testify/issues/187
- https://github.com/stretchr/testify/issues/466
- https://github.com/stretchr/testify/issues/934
- https://github.com/stretchr/testify/issues/1139
- https://github.com/stretchr/testify/issues/1253

</details>

---

### suite-dont-use-pkg

```go
func (s *MySuite) TestSomething() {
❌ assert.Equal(s.T(), 42, value)
✅ s.Equal(42, value)
Expand Down
2 changes: 2 additions & 0 deletions analyzer/checkers_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ func Test_newCheckers(t *testing.T) {
checkers.NewBlankImport(),
checkers.NewGoRequire(),
checkers.NewRequireError(),
checkers.NewSuiteBrokenParallel(),
checkers.NewSuiteSubtestRun(),
}
allAdvancedCheckers := []checkers.AdvancedChecker{
checkers.NewBlankImport(),
checkers.NewGoRequire(),
checkers.NewRequireError(),
checkers.NewSuiteBrokenParallel(),
checkers.NewSuiteSubtestRun(),
checkers.NewSuiteTHelper(),
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Code generated by testifylint/internal/testgen. DO NOT EDIT.

package suitebrokenparallel

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type SuiteBrokenParallelCheckerSuite struct {
suite.Suite
}

func TestSuiteBrokenParallelCheckerSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(SuiteBrokenParallelCheckerSuite))
}

func (s *SuiteBrokenParallelCheckerSuite) BeforeTest(_, _ string) {
}

func (s *SuiteBrokenParallelCheckerSuite) SetupTest() {
}

func (s *SuiteBrokenParallelCheckerSuite) TestAll() {

s.Run("", func() {
s.Equal(1, 2)
})

s.T().Run("", func(t *testing.T) {
assert.Equal(t, 1, 2)
})

s.Run("", func() {
s.Equal(1, 2)

s.Run("", func() {
s.Equal(1, 2)

s.Run("", func() {
s.Equal(1, 2)
})

s.T().Run("", func(t *testing.T) {
assert.Equal(t, 1, 2)
})
})
})
}

func (s *SuiteBrokenParallelCheckerSuite) TestTable() {
cases := []struct{ Name string }{}

for _, tt := range cases {
tt := tt
s.T().Run(tt.Name, func(t *testing.T) {
s.Equal(1, 2)
})
}

for _, tt := range cases {
tt := tt
s.Run(tt.Name, func() {
s.Equal(1, 2)
})
}
}

func TestSimpleTable(t *testing.T) {
t.Parallel()

cases := []struct{ Name string }{}
for _, tt := range cases {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, 1, 2)
})
}
}
Loading
Loading