Skip to content

Commit 2c666ed

Browse files
authored
dev: group linter implementation and integration tests (#4603)
1 parent 93d3a38 commit 2c666ed

File tree

597 files changed

+2788
-1353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

597 files changed

+2788
-1353
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/*.pdf
44
/*.pprof
55
/*.txt
6+
/test.lock
67
/.idea/
78
/.vscode/
89
/dist/

.golangci.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,27 @@ issues:
163163
text: "SA1019: c.cfg.Run.ShowStats is deprecated: use Output.ShowStats instead."
164164

165165
# Deprecated linter options.
166-
- path: pkg/golinters/errcheck.go
166+
- path: pkg/golinters/errcheck/errcheck.go
167167
linters: [staticcheck]
168168
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
169-
- path: pkg/golinters/govet.go
169+
- path: pkg/golinters/govet/govet.go
170170
linters: [staticcheck]
171171
text: "SA1019: cfg.CheckShadowing is deprecated: the linter should be enabled inside Enable."
172-
- path: pkg/golinters/godot.go
172+
- path: pkg/golinters/godot/godot.go
173173
linters: [staticcheck]
174174
text: "SA1019: settings.CheckAll is deprecated: use Scope instead"
175-
- path: pkg/golinters/gci.go
175+
- path: pkg/golinters/gci/gci.go
176176
linters: [staticcheck]
177177
text: "SA1019: settings.LocalPrefixes is deprecated: use Sections instead."
178-
- path: pkg/golinters/mnd.go
178+
- path: pkg/golinters/mnd/mnd.go
179179
linters: [staticcheck]
180180
text: "SA1019: settings.Settings is deprecated: use root level settings instead."
181-
- path: pkg/golinters/mnd.go
181+
- path: pkg/golinters/mnd/mnd.go
182182
linters: [staticcheck]
183183
text: "SA1019: config.GoMndSettings is deprecated: use MndSettings."
184184

185185
# Related to `run.go`, it cannot be removed.
186-
- path: pkg/golinters/gofumpt.go
186+
- path: pkg/golinters/gofumpt/gofumpt.go
187187
linters: [staticcheck]
188188
text: "SA1019: settings.LangVersion is deprecated: use the global `run.go` instead."
189189
- path: pkg/golinters/internal/staticcheck_common.go
@@ -194,7 +194,7 @@ issues:
194194
text: "SA1019: (.+).(GoVersion|LangVersion) is deprecated: use the global `run.go` instead."
195195

196196
# Based on existing code, the modifications should be limited to make maintenance easier.
197-
- path: pkg/golinters/unused.go
197+
- path: pkg/golinters/unused/unused.go
198198
linters: [gocritic]
199199
text: "rangeValCopy: each iteration copies 160 bytes \\(consider pointers or indexing\\)"
200200

Makefile

+9-11
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,21 @@ test: build
3333
GL_TEST_RUN=1 go test -v -parallel 2 ./...
3434
.PHONY: test
3535

36-
# ex: T=gofmt.go make test_fix
37-
# the value of `T` is the name of a file from `test/testdata/fix`
38-
test_fix: build
39-
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestFix/$T
40-
.PHONY: test_fix
41-
4236
test_race: build_race
4337
GL_TEST_RUN=1 ./$(BINARY) run -v --timeout=5m
4438
.PHONY: test_race
4539

46-
test_linters:
40+
# ex: T=output.go make test_integration
41+
# the value of `T` is the name of a file from `test/testdata`
42+
test_integration:
4743
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdata/$T
48-
.PHONY: test_linters
44+
.PHONY: test_integration
4945

50-
test_linters_sub:
51-
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdataSubDir/$T
52-
.PHONY: test_linters_sub
46+
# ex: T=multiple-issues-fix.go make test_integration_fix
47+
# the value of `T` is the name of a file from `test/testdata/fix`
48+
test_integration_fix: build
49+
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestFix/$T
50+
.PHONY: test_integration_fix
5351

5452
# Maintenance
5553

docs/src/docs/contributing/new-linters.mdx

+5-8
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ We don't accept non `go/analysis` linters.
1515
After that:
1616

1717
1. Implement functional tests for the linter:
18-
- Add one file into directory [`test/testdata`](https://github.com/golangci/golangci-lint/tree/master/test/testdata).
19-
- Run the test to ensure that test fails:
20-
```bash
21-
T=yourlintername.go make test_linters
22-
```
18+
- Add one file into directory `pkg/golinters/{yourlintername}/testdata/`.
19+
- Run the test to ensure that test fails.
2320
- Run:
2421
```bash
25-
go run ./cmd/golangci-lint/ run --no-config --disable-all --enable=yourlintername ./test/testdata/yourlintername.go
22+
go run ./cmd/golangci-lint/ run --no-config --disable-all --enable={yourlintername} ./pkg/golinters/{yourlintername}/testdata/{yourlintername}.go
2623
```
27-
2. Add a new file `pkg/golinters/{yourlintername}.go`.
24+
2. Add a new file `pkg/golinters/{yourlintername}/{yourlintername}.go`.
2825
Look at other linters in this directory.
2926
Implement linter integration and check that test passes.
30-
3. Add the new struct for the linter (which you've implemented in `pkg/golinters/{yourlintername}.go`) to the
27+
3. Add the new struct for the linter (which you've implemented in `pkg/golinters/{yourlintername}/{yourlintername}.go`) to the
3128
list of all supported linters in [`pkg/lint/lintersdb/builder_linter.go`](https://github.com/golangci/golangci-lint/blob/master/pkg/lint/lintersdb/builder_linter.go)
3229
to the method `LinterBuilder.Build`.
3330
- Add `WithSince("next_version")`, where `next_version` must be replaced by the next minor version. (ex: v1.2.0 if the current version is v1.1.0)

pkg/golinters/asasalint.go pkg/golinters/asasalint/asasalint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package asasalint
22

33
import (
44
"github.com/alingse/asasalint"
@@ -9,7 +9,7 @@ import (
99
"github.com/golangci/golangci-lint/pkg/golinters/internal"
1010
)
1111

12-
func NewAsasalint(setting *config.AsasalintSettings) *goanalysis.Linter {
12+
func New(setting *config.AsasalintSettings) *goanalysis.Linter {
1313
cfg := asasalint.LinterSetting{}
1414
if setting != nil {
1515
cfg.Exclude = setting.Exclude
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package asasalint
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
File renamed without changes.

pkg/golinters/asciicheck.go pkg/golinters/asciicheck/asciicheck.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package asciicheck
22

33
import (
44
"github.com/tdakkota/asciicheck"
@@ -7,7 +7,7 @@ import (
77
"github.com/golangci/golangci-lint/pkg/goanalysis"
88
)
99

10-
func NewAsciicheck() *goanalysis.Linter {
10+
func New() *goanalysis.Linter {
1111
a := asciicheck.NewAnalyzer()
1212

1313
return goanalysis.NewLinter(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package asciicheck
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
File renamed without changes.

pkg/golinters/bidichk.go pkg/golinters/bidichk/bidichk.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package bidichk
22

33
import (
44
"strings"
@@ -10,7 +10,7 @@ import (
1010
"github.com/golangci/golangci-lint/pkg/goanalysis"
1111
)
1212

13-
func NewBiDiChk(cfg *config.BiDiChkSettings) *goanalysis.Linter {
13+
func New(cfg *config.BiDiChkSettings) *goanalysis.Linter {
1414
a := bidichk.NewAnalyzer()
1515

1616
cfgMap := map[string]map[string]any{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package bidichk
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
File renamed without changes.

pkg/golinters/bodyclose.go pkg/golinters/bodyclose/bodyclose.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package bodyclose
22

33
import (
44
"github.com/timakin/bodyclose/passes/bodyclose"
@@ -7,7 +7,7 @@ import (
77
"github.com/golangci/golangci-lint/pkg/goanalysis"
88
)
99

10-
func NewBodyclose() *goanalysis.Linter {
10+
func New() *goanalysis.Linter {
1111
a := bodyclose.Analyzer
1212

1313
return goanalysis.NewLinter(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package bodyclose
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
File renamed without changes.

pkg/golinters/containedctx.go pkg/golinters/containedctx/containedctx.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package containedctx
22

33
import (
44
"github.com/sivchari/containedctx"
@@ -7,7 +7,7 @@ import (
77
"github.com/golangci/golangci-lint/pkg/goanalysis"
88
)
99

10-
func NewContainedCtx() *goanalysis.Linter {
10+
func New() *goanalysis.Linter {
1111
a := containedctx.Analyzer
1212

1313
return goanalysis.NewLinter(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package containedctx
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}

pkg/golinters/contextcheck.go pkg/golinters/contextcheck/contextcheck.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package contextcheck
22

33
import (
44
"github.com/kkHAIKE/contextcheck"
@@ -8,7 +8,7 @@ import (
88
"github.com/golangci/golangci-lint/pkg/lint/linter"
99
)
1010

11-
func NewContextCheck() *goanalysis.Linter {
11+
func New() *goanalysis.Linter {
1212
analyzer := contextcheck.NewAnalyzer(contextcheck.Configuration{})
1313

1414
return goanalysis.NewLinter(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package contextcheck
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}

pkg/golinters/copyloopvar.go pkg/golinters/copyloopvar/copyloopvar.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package copyloopvar
22

33
import (
44
"github.com/karamaru-alpha/copyloopvar"
@@ -8,7 +8,7 @@ import (
88
"github.com/golangci/golangci-lint/pkg/goanalysis"
99
)
1010

11-
func NewCopyLoopVar(settings *config.CopyLoopVarSettings) *goanalysis.Linter {
11+
func New(settings *config.CopyLoopVarSettings) *goanalysis.Linter {
1212
a := copyloopvar.NewAnalyzer()
1313

1414
var cfg map[string]map[string]any
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package copyloopvar
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
File renamed without changes.

test/testdata/copyloopvar_custom.go pkg/golinters/copyloopvar/testdata/copyloopvar_custom.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build go1.22
22

33
//golangcitest:args -Ecopyloopvar
4-
//golangcitest:config_path testdata/configs/copyloopvar.yml
4+
//golangcitest:config_path testdata/copyloopvar.yml
55
package testdata
66

77
import "fmt"

pkg/golinters/cyclop.go pkg/golinters/cyclop/cyclop.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package cyclop
22

33
import (
44
"github.com/bkielbasa/cyclop/pkg/analyzer"
@@ -8,7 +8,7 @@ import (
88
"github.com/golangci/golangci-lint/pkg/goanalysis"
99
)
1010

11-
func NewCyclop(settings *config.Cyclop) *goanalysis.Linter {
11+
func New(settings *config.Cyclop) *goanalysis.Linter {
1212
a := analyzer.NewAnalyzer()
1313

1414
var cfg map[string]map[string]any
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cyclop
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}

test/testdata/cyclop.go pkg/golinters/cyclop/testdata/cyclop.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//golangcitest:args -Ecyclop
2-
//golangcitest:config_path testdata/configs/cyclop.yml
2+
//golangcitest:config_path testdata/cyclop.yml
33
package testdata
44

55
func cyclopComplexFunc(s string) { // want "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
File renamed without changes.

pkg/golinters/decorder.go pkg/golinters/decorder/decorder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package decorder
22

33
import (
44
"strings"
@@ -10,7 +10,7 @@ import (
1010
"github.com/golangci/golangci-lint/pkg/goanalysis"
1111
)
1212

13-
func NewDecorder(settings *config.DecorderSettings) *goanalysis.Linter {
13+
func New(settings *config.DecorderSettings) *goanalysis.Linter {
1414
a := decorder.Analyzer
1515

1616
// disable all rules/checks by default
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package decorder
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
File renamed without changes.

test/testdata/decorder_custom.go pkg/golinters/decorder/testdata/decorder_custom.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//golangcitest:args -Edecorder
2-
//golangcitest:config_path testdata/configs/decorder_custom.yml
2+
//golangcitest:config_path testdata/decorder_custom.yml
33
package testdata
44

55
import "math"

pkg/golinters/depguard.go pkg/golinters/depguard/depguard.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package golinters
1+
package depguard
22

33
import (
44
"github.com/OpenPeeDeeP/depguard/v2"
@@ -9,7 +9,7 @@ import (
99
"github.com/golangci/golangci-lint/pkg/lint/linter"
1010
)
1111

12-
func NewDepguard(settings *config.DepGuardSettings) *goanalysis.Linter {
12+
func New(settings *config.DepGuardSettings) *goanalysis.Linter {
1313
conf := depguard.LinterSettings{}
1414

1515
if settings != nil {

0 commit comments

Comments
 (0)