From b90551cdf9c6214075f2a40d1b5595c6b41ffff0 Mon Sep 17 00:00:00 2001 From: Kunwardeep Date: Mon, 23 Nov 2020 06:28:38 +1100 Subject: [PATCH] add new paralleltest linter (#1503) --- go.mod | 1 + go.sum | 4 ++++ pkg/golinters/paralleltest.go | 21 +++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 4 ++++ test/testdata/paralleltest.go | 24 ++++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 pkg/golinters/paralleltest.go create mode 100644 test/testdata/paralleltest.go diff --git a/go.mod b/go.mod index 842b71a68a92..54a695d55430 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/jgautheron/goconst v0.0.0-20201117150253-ccae5bf973f3 github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3 + github.com/kunwardeep/paralleltest v1.0.2 github.com/kyoh86/exportloopref v0.1.8 github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0 diff --git a/go.sum b/go.sum index 0bf2a164e277..9ca26a739454 100644 --- a/go.sum +++ b/go.sum @@ -222,6 +222,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kunwardeep/paralleltest v1.0.2 h1:/jJRv0TiqPoEy/Y8dQxCFJhD56uS/pnvtatgTZBHokU= +github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2GhLxSfY+oqUsvG30= github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77LoN/M= github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= @@ -540,6 +542,8 @@ golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20200426102838-f3a5411a4c3b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200622203043-20e05c1c8ffa/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200624225443-88f3c62a19ff/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200624225443-88f3c62a19ff/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200625211823-6506e20df31f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200625211823-6506e20df31f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200724022722-7017fd6b1305/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200731060945-b5fad4ed8dd6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= diff --git a/pkg/golinters/paralleltest.go b/pkg/golinters/paralleltest.go new file mode 100644 index 000000000000..14ddf36a5629 --- /dev/null +++ b/pkg/golinters/paralleltest.go @@ -0,0 +1,21 @@ +package golinters + +import ( + "github.com/kunwardeep/paralleltest/pkg/paralleltest" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewParallelTest() *goanalysis.Linter { + analyzers := []*analysis.Analyzer{ + paralleltest.NewAnalyzer(), + } + + return goanalysis.NewLinter( + "paralleltest", + "paralleltest detects missing usage of t.Parallel() method in your Go test", + analyzers, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 52a230bd8053..a36ae2f57661 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -324,6 +324,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetBugs). WithLoadForGoAnalysis(). WithURL("https://github.com/polyfloyd/go-errorlint"), + linter.NewConfig(golinters.NewParallelTest()). + WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/kunwardeep/paralleltest"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). diff --git a/test/testdata/paralleltest.go b/test/testdata/paralleltest.go new file mode 100644 index 000000000000..3634aac942d6 --- /dev/null +++ b/test/testdata/paralleltest.go @@ -0,0 +1,24 @@ +//args: -Eparalleltest +package testdata + +import ( + "fmt" + "testing" +) + +func TestFunctionSuccessfulRangeTest(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + }{{name: "foo"}} + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + fmt.Println(tc.name) + }) + } +} + +func TestFunctionMissingCallToParallel(t *testing.T) {} // ERROR "Function TestFunctionMissingCallToParallel missing the call to method parallel"