Skip to content

Commit 364eea6

Browse files
committed
Add tparallel linter
1 parent 5f93c93 commit 364eea6

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/mattn/go-colorable v0.1.7
3535
github.com/mitchellh/go-homedir v1.1.0
3636
github.com/mitchellh/go-ps v1.0.0
37+
github.com/moricho/tparallel v0.2.0
3738
github.com/nakabonne/nestif v0.3.0
3839
github.com/nishanths/exhaustive v0.0.0-20200811152831-6cf413ae40e0
3940
github.com/pkg/errors v0.9.1

Diff for: go.sum

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/golinters/tparallel.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package golinters
2+
3+
import (
4+
"github.com/moricho/tparallel"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewTparallel() *goanalysis.Linter {
11+
analyzers := []*analysis.Analyzer{
12+
tparallel.Analyzer,
13+
}
14+
15+
return goanalysis.NewLinter(
16+
"tparallel",
17+
"tparallel detects inappropriate usage of t.Parallel() method in your Go test codes",
18+
analyzers,
19+
nil,
20+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
21+
}

Diff for: pkg/lint/lintersdb/manager.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ func (m *Manager) WithCustomLinters() *Manager {
5656
}
5757

5858
func (Manager) AllPresets() []string {
59-
return []string{linter.PresetBugs, linter.PresetComplexity, linter.PresetFormatting,
60-
linter.PresetPerformance, linter.PresetStyle, linter.PresetUnused}
59+
return []string{
60+
linter.PresetBugs, linter.PresetComplexity, linter.PresetFormatting,
61+
linter.PresetPerformance, linter.PresetStyle, linter.PresetUnused,
62+
}
6163
}
6264

6365
func (m Manager) allPresetsSet() map[string]bool {
@@ -305,6 +307,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
305307
WithPresets(linter.PresetStyle).
306308
WithLoadForGoAnalysis().
307309
WithURL("https://github.com/ssgreg/nlreturn"),
310+
linter.NewConfig(golinters.NewTparallel()).
311+
WithPresets(linter.PresetStyle).
312+
WithLoadForGoAnalysis().
313+
WithURL("https://github.com/moricho/tparallel"),
308314
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
309315
linter.NewConfig(golinters.NewNoLintLint()).
310316
WithPresets(linter.PresetStyle).

Diff for: test/linters_test.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,18 @@ func TestExtractRunContextFromComments(t *testing.T) {
252252
assert.Equal(t, []string{"-Egoimports"}, rc.args)
253253
}
254254

255-
func TestGolintConsumesXTestFiles(t *testing.T) {
256-
dir := getTestDataDir("withxtest")
257-
const expIssue = "`if` block ends with a `return` statement, so drop this `else` and outdent its block"
255+
func TestTparallel(t *testing.T) {
256+
sourcePath := filepath.Join(testdataDir, "tparallel", "tparallel_test.go")
257+
args := []string{
258+
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
259+
sourcePath,
260+
}
261+
rc := extractRunContextFromComments(t, sourcePath)
262+
args = append(args, rc.args...)
258263

259-
r := testshared.NewLintRunner(t)
260-
r.Run("--no-config", "--disable-all", "-Egolint", dir).ExpectHasIssue(expIssue)
261-
r.Run("--no-config", "--disable-all", "-Egolint", filepath.Join(dir, "p_test.go")).ExpectHasIssue(expIssue)
264+
cfg, err := yaml.Marshal(rc.config)
265+
assert.NoError(t, err)
266+
267+
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
268+
ExpectHasIssue("testdata/tparallel/tparallel_test.go:7:6: TestSomething should call t.Parallel on the top level as well as its subtests\n")
262269
}

Diff for: test/testdata/tparallel/tparallel_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package testdata
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSomething(t *testing.T) {
8+
t.Run("", func(t *testing.T) {
9+
t.Parallel()
10+
})
11+
}

0 commit comments

Comments
 (0)