Skip to content

Commit e540c76

Browse files
committed
Add tparallel linter
1 parent 5f93c93 commit e540c76

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

Diff for: go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/denis-tingajkin/go-header v0.3.1
1111
github.com/fatih/color v1.9.0
1212
github.com/go-critic/go-critic v0.5.2
13+
github.com/go-sql-driver/mysql v1.4.0
1314
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
1415
github.com/gofrs/flock v0.8.0
1516
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
@@ -28,12 +29,14 @@ require (
2829
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
2930
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
3031
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
32+
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5
3133
github.com/kyoh86/exportloopref v0.1.7
3234
github.com/maratori/testpackage v1.0.1
3335
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
3436
github.com/mattn/go-colorable v0.1.7
3537
github.com/mitchellh/go-homedir v1.1.0
3638
github.com/mitchellh/go-ps v1.0.0
39+
github.com/moricho/tparallel v0.2.0
3740
github.com/nakabonne/nestif v0.3.0
3841
github.com/nishanths/exhaustive v0.0.0-20200811152831-6cf413ae40e0
3942
github.com/pkg/errors v0.9.1
@@ -58,6 +61,7 @@ require (
5861
github.com/ultraware/whitespace v0.0.4
5962
github.com/uudashr/gocognit v1.0.1
6063
github.com/valyala/quicktemplate v1.6.2
64+
golang.org/x/mod v0.3.0
6165
golang.org/x/tools v0.0.0-20200918232735-d647fc253266
6266
gopkg.in/yaml.v2 v2.3.0
6367
honnef.co/go/tools v0.0.1-2020.1.5

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)