Skip to content

Commit 27f921f

Browse files
authored
dev: use directives instead of comments for tests (#2978)
1 parent 0abb298 commit 27f921f

File tree

166 files changed

+291
-278
lines changed

Some content is hidden

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

166 files changed

+291
-278
lines changed

Diff for: pkg/config/linters_settings.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,12 @@ type WSLSettings struct {
629629
// CustomLinterSettings encapsulates the meta-data of a private linter.
630630
// For example, a private linter may be added to the golangci config file as shown below.
631631
//
632-
// linters-settings:
633-
// custom:
634-
// example:
635-
// path: /example.so
636-
// description: The description of the linter
637-
// original-url: github.com/golangci/example-linter
632+
// linters-settings:
633+
// custom:
634+
// example:
635+
// path: /example.so
636+
// description: The description of the linter
637+
// original-url: github.com/golangci/example-linter
638638
type CustomLinterSettings struct {
639639
// Path to a plugin *.so file that implements the private linter.
640640
Path string

Diff for: pkg/golinters/gofmt_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ index 2c9f78d..c0d5791 100644
310310
--- a/gofmt.go
311311
+++ b/gofmt.go
312312
@@ -1,9 +1,9 @@
313-
//args: -Egofmt
313+
//golangcitest:args -Egofmt
314314
package p
315315
316316
- func gofmt(a, b int) int {

Diff for: pkg/golinters/revive.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type jsonObject struct {
3434
}
3535

3636
// NewRevive returns a new Revive linter.
37+
//
3738
//nolint:dupl
3839
func NewRevive(settings *config.ReviveSettings) *goanalysis.Linter {
3940
var mu sync.Mutex

Diff for: pkg/golinters/scopelint.go

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type Node struct {
8484
// Visit method is invoked for each node encountered by Walk.
8585
// If the result visitor w is not nil, Walk visits each of the children
8686
// of node with the visitor w, followed by a call of w.Visit(nil).
87+
//
8788
//nolint:gocyclo,gocritic
8889
func (f *Node) Visit(node ast.Node) ast.Visitor {
8990
switch typedNode := node.(type) {
@@ -173,6 +174,7 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
173174

174175
// The variadic arguments may start with link and category types,
175176
// and must end with a format string and any arguments.
177+
//
176178
//nolint:interfacer
177179
func (f *Node) errorf(n ast.Node, format string, args ...interface{}) {
178180
pos := f.fset.Position(n.Pos())

Diff for: test/errchk.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var errorLineRx = regexp.MustCompile(`^\S+?: (.*)\((\S+?)\)$`)
2424
//
2525
// Sources files are supplied as fullshort slice.
2626
// It consists of pairs: full path to source file and its base name.
27+
//
2728
//nolint:gocyclo,funlen
2829
func errorCheck(outStr string, wantAuto bool, defaultWantedLinter string, fullshort ...string) (err error) {
2930
var errs []error
@@ -179,6 +180,7 @@ var (
179180
)
180181

181182
// wantedErrors parses expected errors from comments in a file.
183+
//
182184
//nolint:nakedret
183185
func wantedErrors(file, short, defaultLinter string) (errs []wantedError) {
184186
cache := make(map[string]*regexp.Regexp)

Diff for: test/linters_test.go

+32-24
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ type runContext struct {
248248

249249
func buildConfigFromShortRepr(t *testing.T, repr string, config map[string]interface{}) {
250250
kv := strings.Split(repr, "=")
251-
require.Len(t, kv, 2)
251+
require.Len(t, kv, 2, "repr: %s", repr)
252252

253253
keyParts := strings.Split(kv[0], ".")
254254
require.True(t, len(keyParts) >= 2, len(keyParts))
@@ -308,47 +308,55 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
308308
continue
309309
}
310310

311-
line = strings.TrimLeft(strings.TrimPrefix(line, "//"), " ")
312-
if strings.HasPrefix(line, "args: ") {
311+
if !strings.HasPrefix(line, "//golangcitest:") {
312+
require.Failf(t, "invalid prefix of comment line %s", line)
313+
}
314+
315+
// TODO(ldez) replace that by strings.Cut when we will drop go1.17
316+
var before string
317+
var after string
318+
if i := strings.Index(line, " "); i >= 0 {
319+
before = line[:i]
320+
after = strings.TrimSpace(line[i+len(" "):])
321+
} else {
322+
require.Failf(t, "invalid prefix of comment line %s", line)
323+
}
324+
325+
switch before {
326+
case "//golangcitest:args":
313327
require.Nil(t, rc.args)
314-
args := strings.TrimPrefix(line, "args: ")
315-
require.NotEmpty(t, args)
316-
rc.args = strings.Split(args, " ")
328+
require.NotEmpty(t, after)
329+
rc.args = strings.Split(after, " ")
317330
continue
318-
}
319331

320-
if strings.HasPrefix(line, "config: ") {
321-
repr := strings.TrimPrefix(line, "config: ")
322-
require.NotEmpty(t, repr)
332+
case "//golangcitest:config":
333+
require.NotEmpty(t, after)
323334
if rc.config == nil {
324335
rc.config = map[string]interface{}{}
325336
}
326-
buildConfigFromShortRepr(t, repr, rc.config)
337+
buildConfigFromShortRepr(t, after, rc.config)
327338
continue
328-
}
329339

330-
if strings.HasPrefix(line, "config_path: ") {
331-
configPath := strings.TrimPrefix(line, "config_path: ")
332-
require.NotEmpty(t, configPath)
333-
rc.configPath = configPath
340+
case "//golangcitest:config_path":
341+
require.NotEmpty(t, after)
342+
rc.configPath = after
334343
continue
335-
}
336344

337-
if strings.HasPrefix(line, "expected_linter: ") {
338-
expectedLinter := strings.TrimPrefix(line, "expected_linter: ")
339-
require.NotEmpty(t, expectedLinter)
340-
rc.expectedLinter = expectedLinter
345+
case "//golangcitest:expected_linter":
346+
require.NotEmpty(t, after)
347+
rc.expectedLinter = after
341348
continue
342-
}
343349

344-
require.Fail(t, "invalid prefix of comment line %s", line)
350+
default:
351+
require.Failf(t, "invalid prefix of comment line %s", line)
352+
}
345353
}
346354

347355
// guess the expected linter if none is specified
348356
if rc.expectedLinter == "" {
349357
for _, arg := range rc.args {
350358
if strings.HasPrefix(arg, "-E") && !strings.Contains(arg, ",") {
351-
require.Empty(t, rc.expectedLinter, "could not infer expected linter for errors because multiple linters are enabled. Please use the `expected_linter: ` directive in your test to indicate the linter-under-test.") //nolint:lll
359+
require.Empty(t, rc.expectedLinter, "could not infer expected linter for errors because multiple linters are enabled. Please use the `//golangcitest:expected_linter ` directive in your test to indicate the linter-under-test.") //nolint:lll
352360
rc.expectedLinter = arg[2:]
353361
}
354362
}

Diff for: test/testdata/asciicheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Easciicheck
1+
//golangcitest:args -Easciicheck
22
package testdata
33

44
import (

Diff for: test/testdata/bidichk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Ebidichk
1+
//golangcitest:args -Ebidichk
22
package testdata
33

44
import "fmt"

Diff for: test/testdata/bodyclose.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Ebodyclose
1+
//golangcitest:args -Ebodyclose
22
package testdata
33

44
import (

Diff for: test/testdata/containedctx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// args: -Econtainedctx
1+
//golangcitest:args -Econtainedctx
22
package testdata
33

44
import "context"

Diff for: test/testdata/contextcheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Econtextcheck
1+
//golangcitest:args -Econtextcheck
22
package testdata
33

44
import "context"

Diff for: test/testdata/cyclop.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Ecyclop
2-
//config: linters-settings.cyclop.max-complexity=15
1+
//golangcitest:args -Ecyclop
2+
//golangcitest:config linters-settings.cyclop.max-complexity=15
33
package testdata
44

55
func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"

Diff for: test/testdata/deadcode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Edeadcode
1+
//golangcitest:args -Edeadcode
22
package testdata
33

44
var y int

Diff for: test/testdata/decorder.go

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

55
import "math"

Diff for: test/testdata/decorder_default.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// args: -Edecorder
1+
//golangcitest:args -Edecorder
22
package testdata
33

44
import "math"

Diff for: test/testdata/default_exclude.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Estylecheck,golint --internal-cmd-test
2-
//config_path: testdata/configs/default_exclude.yml
1+
//golangcitest:args -Estylecheck,golint --internal-cmd-test
2+
//golangcitest:config_path testdata/configs/default_exclude.yml
33

44
/*Package testdata ...*/
55
package testdata

Diff for: test/testdata/depguard.go

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

55
import (

Diff for: test/testdata/depguard_additional_guards.go

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

55
import (

Diff for: test/testdata/depguard_ignore_file_rules.go

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

55
// NOTE - No lint errors becuase this file is ignored

Diff for: test/testdata/dogsled.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Edogsled
1+
//golangcitest:args -Edogsled
22
package testdata
33

44
func Dogsled() {

Diff for: test/testdata/dupl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Edupl
2-
//config: linters-settings.dupl.threshold=20
1+
//golangcitest:args -Edupl
2+
//golangcitest:config linters-settings.dupl.threshold=20
33
package testdata
44

55
type DuplLogger struct{}

Diff for: test/testdata/durationcheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Edurationcheck
1+
//golangcitest:args -Edurationcheck
22
package testdata
33

44
import (

Diff for: test/testdata/errcheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Eerrcheck
1+
//golangcitest:args -Eerrcheck
22
package testdata
33

44
import (

Diff for: test/testdata/errcheck_exclude.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//args: -Eerrcheck
2-
//config: linters-settings.errcheck.check-blank=true
3-
//config: linters-settings.errcheck.exclude=testdata/errcheck/exclude.txt
1+
//golangcitest:args -Eerrcheck
2+
//golangcitest:config linters-settings.errcheck.check-blank=true
3+
//golangcitest:config linters-settings.errcheck.exclude=testdata/errcheck/exclude.txt
44
package testdata
55

66
import (

Diff for: test/testdata/errcheck_exclude_functions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Eerrcheck
2-
//config_path: testdata/errcheck/exclude_functions.yml
1+
//golangcitest:args -Eerrcheck
2+
//golangcitest:config_path testdata/errcheck/exclude_functions.yml
33
package testdata
44

55
import (

Diff for: test/testdata/errcheck_ignore.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Eerrcheck
2-
//config_path: testdata/errcheck/ignore_config.yml
1+
//golangcitest:args -Eerrcheck
2+
//golangcitest:config_path testdata/errcheck/ignore_config.yml
33
package testdata
44

55
import (

Diff for: test/testdata/errcheck_ignore_default.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Eerrcheck
2-
//config: linters-settings.errcheck.check-blank=true
1+
//golangcitest:args -Eerrcheck
2+
//golangcitest:config linters-settings.errcheck.check-blank=true
33
package testdata
44

55
import (

Diff for: test/testdata/errcheck_type_assertions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Eerrcheck
2-
//config: linters-settings.errcheck.check-type-assertions=true
1+
//golangcitest:args -Eerrcheck
2+
//golangcitest:config linters-settings.errcheck.check-type-assertions=true
33
package testdata
44

55
func ErrorTypeAssertion(filter map[string]interface{}) bool {

Diff for: test/testdata/errchkjson.go

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

55
import (

Diff for: test/testdata/errchkjson_check_error_free_encoding.go

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

55
import (

Diff for: test/testdata/errchkjson_no_exported.go

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

55
import (

Diff for: test/testdata/errname.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Eerrname
1+
//golangcitest:args -Eerrname
22
package testdata
33

44
import (

Diff for: test/testdata/errorlint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Eerrorlint
1+
//golangcitest:args -Eerrorlint
22
package testdata
33

44
import (

Diff for: test/testdata/errorlint_asserts.go

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

55
import (

Diff for: test/testdata/errorlint_comparison.go

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

55
import (

Diff for: test/testdata/errorlint_errorf.go

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

55
import (

Diff for: test/testdata/execinquery.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// args: -Eexecinquery
1+
//golangcitest:args -Eexecinquery
22
package testdata
33

44
import (

Diff for: test/testdata/exhaustive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Eexhaustive
1+
//golangcitest:args -Eexhaustive
22
package testdata
33

44
type Direction int

Diff for: test/testdata/exhaustive_default.go

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

55
type Direction int

Diff for: test/testdata/exhaustive_generated.go

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

0 commit comments

Comments
 (0)