dev: replace golangcitest:config by golangcitest:config_path (#3099)

This commit is contained in:
Ludovic Fernandez 2022-08-15 18:23:07 +02:00 committed by GitHub
parent 9da04f5070
commit f7fba37cc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
59 changed files with 126 additions and 118 deletions

View File

@ -7,7 +7,6 @@ import (
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/golangci/golangci-lint/pkg/exitcodes" "github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/test/testshared" "github.com/golangci/golangci-lint/test/testshared"
@ -56,15 +55,12 @@ func TestFix(t *testing.T) {
args = append(args, rc.args...) args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config)
require.NoError(t, err)
var runResult *testshared.RunResult var runResult *testshared.RunResult
if rc.configPath != "" { if rc.configPath != "" {
args = append(args, "-c", rc.configPath) args = append(args, "-c", rc.configPath)
runResult = testshared.NewLintRunner(t).RunCommand("run", args...) runResult = testshared.NewLintRunner(t).RunCommand("run", args...)
} else { } else {
runResult = testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...) runResult = testshared.NewLintRunner(t).RunWithYamlConfig("", args...)
} }
// nolintlint test uses non existing linters (bob, alice) // nolintlint test uses non existing linters (bob, alice)

View File

@ -14,7 +14,6 @@ import (
hcversion "github.com/hashicorp/go-version" hcversion "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/golangci/golangci-lint/pkg/exitcodes" "github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/test/testshared" "github.com/golangci/golangci-lint/test/testshared"
@ -82,7 +81,7 @@ func TestGoimportsLocal(t *testing.T) {
args = append(args, rc.args...) args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config) cfg, err := os.ReadFile(rc.configPath)
require.NoError(t, err) require.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...). testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
@ -175,25 +174,6 @@ func TestFileOutput(t *testing.T) {
require.Contains(t, string(b), `"Issues":[`) require.Contains(t, string(b), `"Issues":[`)
} }
func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
f, err := os.CreateTemp("", "golangci_lint_test")
require.NoError(t, err)
cfgPath = f.Name() + ".yml"
err = os.Rename(f.Name(), cfgPath)
require.NoError(t, err)
err = yaml.NewEncoder(f).Encode(cfg)
require.NoError(t, err)
return cfgPath, func() {
require.NoError(t, f.Close())
if os.Getenv("GL_KEEP_TEMP_FILES") != "1" {
require.NoError(t, os.Remove(cfgPath))
}
}
}
func testOneSource(t *testing.T, sourcePath string) { func testOneSource(t *testing.T, sourcePath string) {
args := []string{ args := []string{
"run", "run",
@ -210,25 +190,16 @@ func testOneSource(t *testing.T, sourcePath string) {
t.Skipf("Skipped: %s", sourcePath) t.Skipf("Skipped: %s", sourcePath)
} }
var cfgPath string
if rc.config != nil {
p, finish := saveConfig(t, rc.config)
defer finish()
cfgPath = p
} else if rc.configPath != "" {
cfgPath = rc.configPath
}
for _, addArg := range []string{"", "-Etypecheck"} { for _, addArg := range []string{"", "-Etypecheck"} {
caseArgs := append([]string{}, args...) caseArgs := append([]string{}, args...)
caseArgs = append(caseArgs, rc.args...) caseArgs = append(caseArgs, rc.args...)
if addArg != "" { if addArg != "" {
caseArgs = append(caseArgs, addArg) caseArgs = append(caseArgs, addArg)
} }
if cfgPath == "" { if rc.configPath == "" {
caseArgs = append(caseArgs, "--no-config") caseArgs = append(caseArgs, "--no-config")
} else { } else {
caseArgs = append(caseArgs, "-c", cfgPath) caseArgs = append(caseArgs, "-c", rc.configPath)
} }
caseArgs = append(caseArgs, sourcePath) caseArgs = append(caseArgs, sourcePath)
@ -241,41 +212,17 @@ func testOneSource(t *testing.T, sourcePath string) {
type runContext struct { type runContext struct {
args []string args []string
config map[string]interface{}
configPath string configPath string
expectedLinter string expectedLinter string
} }
func buildConfigFromShortRepr(t *testing.T, repr string, config map[string]interface{}) {
kv := strings.Split(repr, "=")
require.Len(t, kv, 2, "repr: %s", repr)
keyParts := strings.Split(kv[0], ".")
require.True(t, len(keyParts) >= 2, len(keyParts))
lastObj := config
for _, k := range keyParts[:len(keyParts)-1] {
var v map[string]interface{}
if lastObj[k] == nil {
v = map[string]interface{}{}
} else {
v = lastObj[k].(map[string]interface{})
}
lastObj[k] = v
lastObj = v
}
lastObj[keyParts[len(keyParts)-1]] = kv[1]
}
func skipMultilineComment(scanner *bufio.Scanner) { func skipMultilineComment(scanner *bufio.Scanner) {
for line := scanner.Text(); !strings.Contains(line, "*/") && scanner.Scan(); { for line := scanner.Text(); !strings.Contains(line, "*/") && scanner.Scan(); {
line = scanner.Text() line = scanner.Text()
} }
} }
//nolint:gocyclo,funlen //nolint:gocyclo
func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext { func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext {
f, err := os.Open(sourcePath) f, err := os.Open(sourcePath)
require.NoError(t, err) require.NoError(t, err)
@ -324,14 +271,6 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
rc.args = strings.Split(after, " ") rc.args = strings.Split(after, " ")
continue continue
case "//golangcitest:config":
require.NotEmpty(t, after)
if rc.config == nil {
rc.config = map[string]interface{}{}
}
buildConfigFromShortRepr(t, after, rc.config)
continue
case "//golangcitest:config_path": case "//golangcitest:config_path":
require.NotEmpty(t, after) require.NotEmpty(t, after)
rc.configPath = after rc.configPath = after
@ -394,10 +333,7 @@ func TestTparallel(t *testing.T) {
args = append(args, rc.args...) args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config) testshared.NewLintRunner(t).RunWithYamlConfig("", args...).
require.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue( ExpectHasIssue(
"testdata/tparallel/missing_toplevel_test.go:7:6: TestTopLevel should call t.Parallel on the top level as well as its subtests\n", "testdata/tparallel/missing_toplevel_test.go:7:6: TestTopLevel should call t.Parallel on the top level as well as its subtests\n",
) )
@ -416,10 +352,7 @@ func TestTparallel(t *testing.T) {
args = append(args, rc.args...) args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config) testshared.NewLintRunner(t).RunWithYamlConfig("", args...).
require.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue( ExpectHasIssue(
"testdata/tparallel/missing_subtest_test.go:7:6: TestSubtests's subtests should call t.Parallel\n", "testdata/tparallel/missing_subtest_test.go:7:6: TestSubtests's subtests should call t.Parallel\n",
) )
@ -438,9 +371,6 @@ func TestTparallel(t *testing.T) {
args = append(args, rc.args...) args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config) testshared.NewLintRunner(t).RunWithYamlConfig("", args...).ExpectNoIssues()
require.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).ExpectNoIssues()
}) })
} }

3
test/testdata/configs/cyclop.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
cyclop:
max-complexity: 15

3
test/testdata/configs/dupl.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
dupl:
threshold: 20

View File

@ -0,0 +1,4 @@
linters-settings:
errcheck:
check-blank: true
exclude: testdata/errcheck/exclude.txt

View File

@ -0,0 +1,3 @@
linters-settings:
errcheck:
check-blank: true

View File

@ -0,0 +1,3 @@
linters-settings:
errcheck:
check-type-assertions: true

View File

@ -0,0 +1,4 @@
linters-settings:
forbidigo:
exclude-godoc-examples: false

4
test/testdata/configs/funlen.yml vendored Normal file
View File

@ -0,0 +1,4 @@
linters-settings:
funlen:
lines: 20
statements: 10

3
test/testdata/configs/gocognit.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
gocognit:
min-complexity: 2

View File

@ -0,0 +1,3 @@
linters-settings:
goconst:
ignore-calls: false

View File

@ -0,0 +1,3 @@
linters-settings:
goconst:
ignore-tests: false

View File

@ -0,0 +1,3 @@
linters-settings:
goconst:
ignore-tests: true

3
test/testdata/configs/gocyclo.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
gocyclo:
min-complexity: 20

5
test/testdata/configs/godox.yml vendored Normal file
View File

@ -0,0 +1,5 @@
linters-settings:
godox:
keywords:
- FIXME
- TODO

View File

@ -0,0 +1,3 @@
linters-settings:
gofmt:
simplify: false

3
test/testdata/configs/gofumpt-fix.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
gofumpt:
extra-rules: true

View File

@ -0,0 +1,3 @@
linters-settings:
gofumpt:
extra-rules: true

3
test/testdata/configs/goimports.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
goimports:
local-prefixes: github.com/golangci/golangci-lint

3
test/testdata/configs/govet.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
govet:
check-shadowing: true

View File

@ -0,0 +1,3 @@
linters-settings:
govet:
enable: fieldalignment

View File

@ -0,0 +1,3 @@
linters-settings:
govet:
enable: ifaceassert

3
test/testdata/configs/lll.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
lll:
tab-width: 4

View File

@ -0,0 +1,3 @@
linters-settings:
makezero:
always: true

3
test/testdata/configs/nestif.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
nestif:
min-complexity: 1

4
test/testdata/configs/nolintlint.yml vendored Normal file
View File

@ -0,0 +1,4 @@
linters-settings:
nolintlint:
require-explanation: true
require-specific: true

View File

@ -0,0 +1,3 @@
linters-settings:
nolintlint:
allow-unused: false

View File

@ -0,0 +1,4 @@
linters-settings:
whitespace:
multi-if: true
multi-func: true

3
test/testdata/configs/wsl.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
wsl:
allow-cuddle-declarations: false

View File

@ -1,5 +1,5 @@
//golangcitest:args -Ecyclop //golangcitest:args -Ecyclop
//golangcitest:config linters-settings.cyclop.max-complexity=15 //golangcitest:config_path testdata/configs/cyclop.yml
package testdata package testdata
func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15" func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Edupl //golangcitest:args -Edupl
//golangcitest:config linters-settings.dupl.threshold=20 //golangcitest:config_path testdata/configs/dupl.yml
package testdata package testdata
type DuplLogger struct{} type DuplLogger struct{}

View File

@ -1,6 +1,5 @@
//golangcitest:args -Eerrcheck //golangcitest:args -Eerrcheck
//golangcitest:config linters-settings.errcheck.check-blank=true //golangcitest:config_path testdata/configs/errcheck_exclude.yml
//golangcitest:config linters-settings.errcheck.exclude=testdata/errcheck/exclude.txt
package testdata package testdata
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Eerrcheck //golangcitest:args -Eerrcheck
//golangcitest:config linters-settings.errcheck.check-blank=true //golangcitest:config_path testdata/configs/errcheck_ignore_default.yml
package testdata package testdata
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Eerrcheck //golangcitest:args -Eerrcheck
//golangcitest:config linters-settings.errcheck.check-type-assertions=true //golangcitest:config_path testdata/configs/errcheck_type_assertions.yml
package testdata package testdata
func ErrorTypeAssertion(filter map[string]interface{}) bool { func ErrorTypeAssertion(filter map[string]interface{}) bool {

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egofumpt //golangcitest:args -Egofumpt
//golangcitest:config linters-settings.gofumpt.extra-rules=true //golangcitest:config_path testdata/configs/gofumpt-fix.yml
package p package p
import "fmt" import "fmt"

View File

@ -1,6 +1,5 @@
//golangcitest:args -Ewhitespace //golangcitest:args -Ewhitespace
//golangcitest:config linters-settings.whitespace.multi-if=true //golangcitest:config_path testdata/configs/whitespace-fix.yml
//golangcitest:config linters-settings.whitespace.multi-func=true
package p package p
import "fmt" import "fmt"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egofumpt //golangcitest:args -Egofumpt
//golangcitest:config linters-settings.gofumpt.extra-rules=true //golangcitest:config_path testdata/configs/gofumpt-fix.yml
package p package p
import "fmt" import "fmt"

View File

@ -1,6 +1,5 @@
//golangcitest:args -Ewhitespace //golangcitest:args -Ewhitespace
//golangcitest:config linters-settings.whitespace.multi-if=true //golangcitest:config_path testdata/configs/whitespace-fix.yml
//golangcitest:config linters-settings.whitespace.multi-func=true
package p package p
import "fmt" import "fmt"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Eforbidigo //golangcitest:args -Eforbidigo
//golangcitest:config linters-settings.forbidigo.exclude-godoc-examples=false //golangcitest:config_path testdata/configs/forbidigo_include_godoc_examples.yml
package testdata package testdata
import "fmt" import "fmt"

View File

@ -1,6 +1,5 @@
//golangcitest:args -Efunlen //golangcitest:args -Efunlen
//golangcitest:config linters-settings.funlen.lines=20 //golangcitest:config_path testdata/configs/funlen.yml
//golangcitest:config linters-settings.funlen.statements=10
package testdata package testdata
func TooManyLines() { // ERROR `Function 'TooManyLines' is too long \(22 > 20\)` func TooManyLines() { // ERROR `Function 'TooManyLines' is too long \(22 > 20\)`

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egocognit //golangcitest:args -Egocognit
//golangcitest:config linters-settings.gocognit.min-complexity=2 //golangcitest:config_path testdata/configs/gocognit.yml
package testdata package testdata
func GoCognit_CC4_GetWords(number int) string { // ERROR "cognitive complexity 4 of func .* is high .*" func GoCognit_CC4_GetWords(number int) string { // ERROR "cognitive complexity 4 of func .* is high .*"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egoconst //golangcitest:args -Egoconst
//golangcitest:config linters-settings.goconst.ignore-calls=false //golangcitest:config_path testdata/configs/goconst_calls_enabled.yml
package testdata package testdata
import "fmt" import "fmt"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egoconst //golangcitest:args -Egoconst
//golangcitest:config linters-settings.goconst.ignore-tests=false //golangcitest:config_path testdata/configs/goconst_dont_ignore.yml
package testdata package testdata
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egoconst //golangcitest:args -Egoconst
//golangcitest:config linters-settings.goconst.ignore-tests=true //golangcitest:config_path testdata/configs/goconst_ignore.yml
package testdata package testdata
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egocyclo //golangcitest:args -Egocyclo
//golangcitest:config linters-settings.gocyclo.min-complexity=20 //golangcitest:config_path testdata/configs/gocyclo.yml
package testdata package testdata
import "net/http" import "net/http"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egodox //golangcitest:args -Egodox
//golangcitest:config linters-settings.godox.keywords=FIXME,TODO //golangcitest:config_path testdata/configs/godox.yml
package testdata package testdata
func todoLeftInCode() { func todoLeftInCode() {

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egofmt //golangcitest:args -Egofmt
//golangcitest:config linters-settings.gofmt.simplify=false //golangcitest:config_path testdata/configs/gofmt_no_simplify.yml
package testdata package testdata
import "fmt" import "fmt"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egofumpt //golangcitest:args -Egofumpt
//golangcitest:config linters-settings.gofumpt.extra-rules=true //golangcitest:config_path testdata/configs/gofumpt_with_extra.yml
package testdata package testdata
import "fmt" import "fmt"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egoimports //golangcitest:args -Egoimports
//golangcitest:config linters-settings.goimports.local-prefixes=github.com/golangci/golangci-lint //golangcitest:config_path testdata/configs/goimports.yml
package goimports package goimports
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egovet //golangcitest:args -Egovet
//golangcitest:config linters-settings.govet.check-shadowing=true //golangcitest:config_path testdata/configs/govet.yml
package testdata package testdata
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egovet //golangcitest:args -Egovet
//golangcitest:config linters-settings.govet.enable=fieldalignment //golangcitest:config_path testdata/configs/govet_fieldalignment.yml
package testdata package testdata
type gvfaGood struct { type gvfaGood struct {

View File

@ -1,5 +1,5 @@
//golangcitest:args -Egovet //golangcitest:args -Egovet
//golangcitest:config linters-settings.govet.enable=ifaceassert //golangcitest:config_path testdata/configs/govet_ifaceassert.yml
package testdata package testdata
import ( import (

View File

@ -1,5 +1,5 @@
//golangcitest:args -Elll //golangcitest:args -Elll
//golangcitest:config linters-settings.lll.tab-width=4 //golangcitest:config_path testdata/configs/lll.yml
package testdata package testdata
func Lll() { func Lll() {

View File

@ -1,5 +1,5 @@
//golangcitest:args -Emakezero //golangcitest:args -Emakezero
//golangcitest:config linters-settings.makezero.always=true //golangcitest:config_path testdata/configs/makezero_always.yml
package testdata package testdata
import "math" import "math"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Enestif //golangcitest:args -Enestif
//golangcitest:config linters-settings.nestif.min-complexity=1 //golangcitest:config_path testdata/configs/nestif.yml
package testdata package testdata
func _() { func _() {

View File

@ -1,7 +1,6 @@
//golangcitest:args -Enolintlint -Emisspell //golangcitest:args -Enolintlint -Emisspell
//golangcitest:expected_linter nolintlint //golangcitest:expected_linter nolintlint
//golangcitest:config linters-settings.nolintlint.require-explanation=true //golangcitest:config_path testdata/configs/nolintlint.yml
//golangcitest:config linters-settings.nolintlint.require-specific=true
package testdata package testdata
import "fmt" import "fmt"

View File

@ -1,6 +1,6 @@
//golangcitest:args -Enolintlint -Evarcheck //golangcitest:args -Enolintlint -Evarcheck
//golangcitest:config linters-settings.nolintlint.allow-unused=false
//golangcitest:expected_linter nolintlint //golangcitest:expected_linter nolintlint
//golangcitest:config_path testdata/configs/nolintlint_unused.yml
package testdata package testdata
import "fmt" import "fmt"

View File

@ -1,5 +1,5 @@
//golangcitest:args -Ewsl //golangcitest:args -Ewsl
//golangcitest:config linters-settings.wsl.tests=1 //golangcitest:config_path testdata/configs/wsl.yml
package testdata package testdata
import ( import (

View File

@ -153,8 +153,7 @@ func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...strin
defer os.Remove(cfgPath) defer os.Remove(cfgPath)
} }
cfg = strings.TrimSpace(cfg) cfg = strings.ReplaceAll(strings.TrimSpace(cfg), "\t", " ")
cfg = strings.ReplaceAll(cfg, "\t", " ")
err = os.WriteFile(cfgPath, []byte(cfg), os.ModePerm) err = os.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
assert.NoError(r.t, err) assert.NoError(r.t, err)