Skip to content

Commit

Permalink
dev: group linter implementation and integration tests (#4603)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Apr 9, 2024
1 parent 93d3a38 commit 2c666ed
Show file tree
Hide file tree
Showing 597 changed files with 2,788 additions and 1,353 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/*.pdf
/*.pprof
/*.txt
/test.lock
/.idea/
/.vscode/
/dist/
Expand Down
16 changes: 8 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,27 @@ issues:
text: "SA1019: c.cfg.Run.ShowStats is deprecated: use Output.ShowStats instead."

# Deprecated linter options.
- path: pkg/golinters/errcheck.go
- path: pkg/golinters/errcheck/errcheck.go
linters: [staticcheck]
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
- path: pkg/golinters/govet.go
- path: pkg/golinters/govet/govet.go
linters: [staticcheck]
text: "SA1019: cfg.CheckShadowing is deprecated: the linter should be enabled inside Enable."
- path: pkg/golinters/godot.go
- path: pkg/golinters/godot/godot.go
linters: [staticcheck]
text: "SA1019: settings.CheckAll is deprecated: use Scope instead"
- path: pkg/golinters/gci.go
- path: pkg/golinters/gci/gci.go
linters: [staticcheck]
text: "SA1019: settings.LocalPrefixes is deprecated: use Sections instead."
- path: pkg/golinters/mnd.go
- path: pkg/golinters/mnd/mnd.go
linters: [staticcheck]
text: "SA1019: settings.Settings is deprecated: use root level settings instead."
- path: pkg/golinters/mnd.go
- path: pkg/golinters/mnd/mnd.go
linters: [staticcheck]
text: "SA1019: config.GoMndSettings is deprecated: use MndSettings."

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

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

Expand Down
20 changes: 9 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,21 @@ test: build
GL_TEST_RUN=1 go test -v -parallel 2 ./...
.PHONY: test

# ex: T=gofmt.go make test_fix
# the value of `T` is the name of a file from `test/testdata/fix`
test_fix: build
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestFix/$T
.PHONY: test_fix

test_race: build_race
GL_TEST_RUN=1 ./$(BINARY) run -v --timeout=5m
.PHONY: test_race

test_linters:
# ex: T=output.go make test_integration
# the value of `T` is the name of a file from `test/testdata`
test_integration:
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdata/$T
.PHONY: test_linters
.PHONY: test_integration

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

# Maintenance

Expand Down
13 changes: 5 additions & 8 deletions docs/src/docs/contributing/new-linters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ We don't accept non `go/analysis` linters.
After that:

1. Implement functional tests for the linter:
- Add one file into directory [`test/testdata`](https://github.com/golangci/golangci-lint/tree/master/test/testdata).
- Run the test to ensure that test fails:
```bash
T=yourlintername.go make test_linters
```
- Add one file into directory `pkg/golinters/{yourlintername}/testdata/`.
- Run the test to ensure that test fails.
- Run:
```bash
go run ./cmd/golangci-lint/ run --no-config --disable-all --enable=yourlintername ./test/testdata/yourlintername.go
go run ./cmd/golangci-lint/ run --no-config --disable-all --enable={yourlintername} ./pkg/golinters/{yourlintername}/testdata/{yourlintername}.go
```
2. Add a new file `pkg/golinters/{yourlintername}.go`.
2. Add a new file `pkg/golinters/{yourlintername}/{yourlintername}.go`.
Look at other linters in this directory.
Implement linter integration and check that test passes.
3. Add the new struct for the linter (which you've implemented in `pkg/golinters/{yourlintername}.go`) to the
3. Add the new struct for the linter (which you've implemented in `pkg/golinters/{yourlintername}/{yourlintername}.go`) to the
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)
to the method `LinterBuilder.Build`.
- 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package asasalint

import (
"github.com/alingse/asasalint"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/internal"
)

func NewAsasalint(setting *config.AsasalintSettings) *goanalysis.Linter {
func New(setting *config.AsasalintSettings) *goanalysis.Linter {
cfg := asasalint.LinterSetting{}
if setting != nil {
cfg.Exclude = setting.Exclude
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/asasalint/asasalint_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asasalint

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package asciicheck

import (
"github.com/tdakkota/asciicheck"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewAsciicheck() *goanalysis.Linter {
func New() *goanalysis.Linter {
a := asciicheck.NewAnalyzer()

return goanalysis.NewLinter(
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/asciicheck/asciicheck_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asciicheck

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
4 changes: 2 additions & 2 deletions pkg/golinters/bidichk.go → pkg/golinters/bidichk/bidichk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package bidichk

import (
"strings"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewBiDiChk(cfg *config.BiDiChkSettings) *goanalysis.Linter {
func New(cfg *config.BiDiChkSettings) *goanalysis.Linter {
a := bidichk.NewAnalyzer()

cfgMap := map[string]map[string]any{}
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/bidichk/bidichk_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bidichk

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package bodyclose

import (
"github.com/timakin/bodyclose/passes/bodyclose"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewBodyclose() *goanalysis.Linter {
func New() *goanalysis.Linter {
a := bodyclose.Analyzer

return goanalysis.NewLinter(
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/bodyclose/bodyclose_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bodyclose

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package containedctx

import (
"github.com/sivchari/containedctx"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewContainedCtx() *goanalysis.Linter {
func New() *goanalysis.Linter {
a := containedctx.Analyzer

return goanalysis.NewLinter(
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/containedctx/containedctx_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package containedctx

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package contextcheck

import (
"github.com/kkHAIKE/contextcheck"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/golangci/golangci-lint/pkg/lint/linter"
)

func NewContextCheck() *goanalysis.Linter {
func New() *goanalysis.Linter {
analyzer := contextcheck.NewAnalyzer(contextcheck.Configuration{})

return goanalysis.NewLinter(
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/contextcheck/contextcheck_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package contextcheck

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package copyloopvar

import (
"github.com/karamaru-alpha/copyloopvar"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewCopyLoopVar(settings *config.CopyLoopVarSettings) *goanalysis.Linter {
func New(settings *config.CopyLoopVarSettings) *goanalysis.Linter {
a := copyloopvar.NewAnalyzer()

var cfg map[string]map[string]any
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/copyloopvar/copyloopvar_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package copyloopvar

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build go1.22

//golangcitest:args -Ecopyloopvar
//golangcitest:config_path testdata/configs/copyloopvar.yml
//golangcitest:config_path testdata/copyloopvar.yml
package testdata

import "fmt"
Expand Down
4 changes: 2 additions & 2 deletions pkg/golinters/cyclop.go → pkg/golinters/cyclop/cyclop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package cyclop

import (
"github.com/bkielbasa/cyclop/pkg/analyzer"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewCyclop(settings *config.Cyclop) *goanalysis.Linter {
func New(settings *config.Cyclop) *goanalysis.Linter {
a := analyzer.NewAnalyzer()

var cfg map[string]map[string]any
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/cyclop/cyclop_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cyclop

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//golangcitest:args -Ecyclop
//golangcitest:config_path testdata/configs/cyclop.yml
//golangcitest:config_path testdata/cyclop.yml
package testdata

func cyclopComplexFunc(s string) { // want "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package decorder

import (
"strings"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewDecorder(settings *config.DecorderSettings) *goanalysis.Linter {
func New(settings *config.DecorderSettings) *goanalysis.Linter {
a := decorder.Analyzer

// disable all rules/checks by default
Expand Down
11 changes: 11 additions & 0 deletions pkg/golinters/decorder/decorder_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package decorder

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//golangcitest:args -Edecorder
//golangcitest:config_path testdata/configs/decorder_custom.yml
//golangcitest:config_path testdata/decorder_custom.yml
package testdata

import "math"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golinters
package depguard

import (
"github.com/OpenPeeDeeP/depguard/v2"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/golangci/golangci-lint/pkg/lint/linter"
)

func NewDepguard(settings *config.DepGuardSettings) *goanalysis.Linter {
func New(settings *config.DepGuardSettings) *goanalysis.Linter {
conf := depguard.LinterSettings{}

if settings != nil {
Expand Down
Loading

0 comments on commit 2c666ed

Please sign in to comment.