Skip to content

dev: use directives instead of comments for tests #2978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ index 2c9f78d..c0d5791 100644
--- a/gofmt.go
+++ b/gofmt.go
@@ -1,9 +1,9 @@
//args: -Egofmt
//golangcitest:args -Egofmt
package p

- func gofmt(a, b int) int {
Expand Down
56 changes: 32 additions & 24 deletions test/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ type runContext struct {

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

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

line = strings.TrimLeft(strings.TrimPrefix(line, "//"), " ")
if strings.HasPrefix(line, "args: ") {
if !strings.HasPrefix(line, "//golangcitest:") {
require.Failf(t, "invalid prefix of comment line %s", line)
}

// TODO(ldez) replace that by strings.Cut when we will drop go1.17
var before string
var after string
if i := strings.Index(line, " "); i >= 0 {
before = line[:i]
after = strings.TrimSpace(line[i+len(" "):])
} else {
require.Failf(t, "invalid prefix of comment line %s", line)
}

switch before {
case "//golangcitest:args":
require.Nil(t, rc.args)
args := strings.TrimPrefix(line, "args: ")
require.NotEmpty(t, args)
rc.args = strings.Split(args, " ")
require.NotEmpty(t, after)
rc.args = strings.Split(after, " ")
continue
}

if strings.HasPrefix(line, "config: ") {
repr := strings.TrimPrefix(line, "config: ")
require.NotEmpty(t, repr)
case "//golangcitest:config":
require.NotEmpty(t, after)
if rc.config == nil {
rc.config = map[string]interface{}{}
}
buildConfigFromShortRepr(t, repr, rc.config)
buildConfigFromShortRepr(t, after, rc.config)
continue
}

if strings.HasPrefix(line, "config_path: ") {
configPath := strings.TrimPrefix(line, "config_path: ")
require.NotEmpty(t, configPath)
rc.configPath = configPath
case "//golangcitest:config_path":
require.NotEmpty(t, after)
rc.configPath = after
continue
}

if strings.HasPrefix(line, "expected_linter: ") {
expectedLinter := strings.TrimPrefix(line, "expected_linter: ")
require.NotEmpty(t, expectedLinter)
rc.expectedLinter = expectedLinter
case "//golangcitest:expected_linter":
require.NotEmpty(t, after)
rc.expectedLinter = after
continue
}

require.Fail(t, "invalid prefix of comment line %s", line)
default:
require.Failf(t, "invalid prefix of comment line %s", line)
}
}

// guess the expected linter if none is specified
if rc.expectedLinter == "" {
for _, arg := range rc.args {
if strings.HasPrefix(arg, "-E") && !strings.Contains(arg, ",") {
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
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
rc.expectedLinter = arg[2:]
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/asciicheck.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Easciicheck
//golangcitest:args -Easciicheck
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/bidichk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Ebidichk
//golangcitest:args -Ebidichk
package testdata

import "fmt"
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/bodyclose.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Ebodyclose
//golangcitest:args -Ebodyclose
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/containedctx.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// args: -Econtainedctx
//golangcitest:args -Econtainedctx
package testdata

import "context"
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/contextcheck.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Econtextcheck
//golangcitest:args -Econtextcheck
package testdata

import "context"
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/cyclop.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Ecyclop
//config: linters-settings.cyclop.max-complexity=15
//golangcitest:args -Ecyclop
//golangcitest:config linters-settings.cyclop.max-complexity=15
package testdata

func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/deadcode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Edeadcode
//golangcitest:args -Edeadcode
package testdata

var y int
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/decorder.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Edecorder
// config_path: testdata/configs/decorder.yml
//golangcitest:args -Edecorder
//golangcitest:config_path testdata/configs/decorder.yml
package testdata

import "math"
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/decorder_default.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// args: -Edecorder
//golangcitest:args -Edecorder
package testdata

import "math"
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/default_exclude.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Estylecheck,golint --internal-cmd-test
//config_path: testdata/configs/default_exclude.yml
//golangcitest:args -Estylecheck,golint --internal-cmd-test
//golangcitest:config_path testdata/configs/default_exclude.yml

/*Package testdata ...*/
package testdata
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/depguard.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Edepguard
//config_path: testdata/configs/depguard.yml
//golangcitest:args -Edepguard
//golangcitest:config_path testdata/configs/depguard.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/depguard_additional_guards.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Edepguard
//config_path: testdata/configs/depguard_additional_guards.yml
//golangcitest:args -Edepguard
//golangcitest:config_path testdata/configs/depguard_additional_guards.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/depguard_ignore_file_rules.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Edepguard
//config_path: testdata/configs/depguard_ignore_file_rules.yml
//golangcitest:args -Edepguard
//golangcitest:config_path testdata/configs/depguard_ignore_file_rules.yml
package testdata

// NOTE - No lint errors becuase this file is ignored
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/dogsled.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Edogsled
//golangcitest:args -Edogsled
package testdata

func Dogsled() {
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/dupl.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Edupl
//config: linters-settings.dupl.threshold=20
//golangcitest:args -Edupl
//golangcitest:config linters-settings.dupl.threshold=20
package testdata

type DuplLogger struct{}
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/durationcheck.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Edurationcheck
//golangcitest:args -Edurationcheck
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/errcheck.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Eerrcheck
//golangcitest:args -Eerrcheck
package testdata

import (
Expand Down
6 changes: 3 additions & 3 deletions test/testdata/errcheck_exclude.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//args: -Eerrcheck
//config: linters-settings.errcheck.check-blank=true
//config: linters-settings.errcheck.exclude=testdata/errcheck/exclude.txt
//golangcitest:args -Eerrcheck
//golangcitest:config linters-settings.errcheck.check-blank=true
//golangcitest:config linters-settings.errcheck.exclude=testdata/errcheck/exclude.txt
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errcheck_exclude_functions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrcheck
//config_path: testdata/errcheck/exclude_functions.yml
//golangcitest:args -Eerrcheck
//golangcitest:config_path testdata/errcheck/exclude_functions.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errcheck_ignore.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrcheck
//config_path: testdata/errcheck/ignore_config.yml
//golangcitest:args -Eerrcheck
//golangcitest:config_path testdata/errcheck/ignore_config.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errcheck_ignore_default.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrcheck
//config: linters-settings.errcheck.check-blank=true
//golangcitest:args -Eerrcheck
//golangcitest:config linters-settings.errcheck.check-blank=true
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errcheck_type_assertions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrcheck
//config: linters-settings.errcheck.check-type-assertions=true
//golangcitest:args -Eerrcheck
//golangcitest:config linters-settings.errcheck.check-type-assertions=true
package testdata

func ErrorTypeAssertion(filter map[string]interface{}) bool {
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errchkjson.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Eerrchkjson
// config_path: testdata/configs/errchkjson.yml
//golangcitest:args -Eerrchkjson
//golangcitest:config_path testdata/configs/errchkjson.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errchkjson_check_error_free_encoding.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Eerrchkjson
// config_path: testdata/configs/errchkjson_check_error_free_encoding.yml
//golangcitest:args -Eerrchkjson
//golangcitest:config_path testdata/configs/errchkjson_check_error_free_encoding.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errchkjson_no_exported.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Eerrchkjson
// config_path: testdata/configs/errchkjson_no_exported.yml
//golangcitest:args -Eerrchkjson
//golangcitest:config_path testdata/configs/errchkjson_no_exported.yml
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/errname.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Eerrname
//golangcitest:args -Eerrname
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/errorlint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Eerrorlint
//golangcitest:args -Eerrorlint
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errorlint_asserts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrorlint
//config_path: testdata/configs/errorlint_asserts.yml
//golangcitest:args -Eerrorlint
//golangcitest:config_path testdata/configs/errorlint_asserts.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errorlint_comparison.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrorlint
//config_path: testdata/configs/errorlint_comparison.yml
//golangcitest:args -Eerrorlint
//golangcitest:config_path testdata/configs/errorlint_comparison.yml
package testdata

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/errorlint_errorf.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eerrorlint
//config_path: testdata/configs/errorlint_errorf.yml
//golangcitest:args -Eerrorlint
//golangcitest:config_path testdata/configs/errorlint_errorf.yml
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/execinquery.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// args: -Eexecinquery
//golangcitest:args -Eexecinquery
package testdata

import (
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/exhaustive.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Eexhaustive
//golangcitest:args -Eexhaustive
package testdata

type Direction int
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/exhaustive_default.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eexhaustive
//config_path: testdata/configs/exhaustive_default.yml
//golangcitest:args -Eexhaustive
//golangcitest:config_path testdata/configs/exhaustive_default.yml
package testdata

type Direction int
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/exhaustive_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/testdata/exhaustive_ignore_enum_members.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Eexhaustive
//config_path: testdata/configs/exhaustive_ignore_enum_members.yml
//golangcitest:args -Eexhaustive
//golangcitest:config_path testdata/configs/exhaustive_ignore_enum_members.yml
package testdata

type Direction int
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/exhaustivestruct.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// args: -Eexhaustivestruct --internal-cmd-test
//golangcitest:args -Eexhaustivestruct --internal-cmd-test
package testdata

import "time"
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/exhaustivestruct_custom.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Eexhaustivestruct --internal-cmd-test
// config_path: testdata/configs/exhaustivestruct.yml
//golangcitest:args -Eexhaustivestruct --internal-cmd-test
//golangcitest:config_path testdata/configs/exhaustivestruct.yml
package testdata

import "time"
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/exhaustruct.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// args: -Eexhaustruct
//golangcitest:args -Eexhaustruct
package testdata

import "time"
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/exhaustruct_custom.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Eexhaustruct
// config_path: testdata/configs/exhaustruct.yml
//golangcitest:args -Eexhaustruct
//golangcitest:config_path testdata/configs/exhaustruct.yml
package testdata

import "time"
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/exportloopref.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//args: -Eexportloopref
//golangcitest:args -Eexportloopref
package testdata

import "fmt"
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/fix/in/gci.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//args: -Egci
//config_path: testdata/configs/gci.yml
//golangcitest:args -Egci
//golangcitest:config_path testdata/configs/gci.yml
package gci

import (
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/fix/in/gocritic.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// args: -Egocritic
// config_path: testdata/configs/gocritic-fix.yml
//golangcitest:args -Egocritic
//golangcitest:config_path testdata/configs/gocritic-fix.yml
package p

import (
Expand Down
Loading