Skip to content

dev: rewrite the tests framework #3100

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
Aug 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 test/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func runOne(b *testing.B, run func(*testing.B), progName string) *runResult {
}

func BenchmarkGolangciLint(b *testing.B) {
testshared.NewLintRunner(b).Install()
testshared.InstallGolangciLint(b)

type bcase struct {
name string
Expand Down
1 change: 0 additions & 1 deletion test/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

const testdataDir = "testdata"
const binName = "../golangci-lint"

var minimalPkg = getTestDataDir("minimalpkg")

Expand Down
74 changes: 39 additions & 35 deletions test/enabled_linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func inSlice(s []string, v string) bool {
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
m := lintersdb.NewManager(nil, nil)
ebdl := m.GetAllEnabledByDefaultLinters()
ret := []string{}
var ret []string
for _, lc := range ebdl {
if lc.IsSlowLinter() {
continue
Expand Down Expand Up @@ -52,7 +52,7 @@ func getAllFastLintersWith(with ...string) []string {

func getEnabledByDefaultLinters() []string {
ebdl := lintersdb.NewManager(nil, nil).GetAllEnabledByDefaultLinters()
ret := []string{}
var ret []string
for _, lc := range ebdl {
ret = append(ret, lc.Name())
}
Expand All @@ -76,23 +76,21 @@ func getEnabledByDefaultFastLintersWith(with ...string) []string {

//nolint:funlen
func TestEnabledLinters(t *testing.T) {
type tc struct {
cases := []struct {
name string
cfg string
el []string
args string
enabledLinters []string
args []string
noImplicitFast bool
}

cases := []tc{
}{
{
name: "disable govet in config",
cfg: `
linters:
disable:
- govet
`,
el: getEnabledByDefaultFastLintersExcept("govet"),
enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable golint in config",
Expand All @@ -101,30 +99,30 @@ func TestEnabledLinters(t *testing.T) {
enable:
- golint
`,
el: getEnabledByDefaultFastLintersWith("golint"),
enabledLinters: getEnabledByDefaultFastLintersWith("golint"),
},
{
name: "disable govet in cmd",
args: "-Dgovet",
el: getEnabledByDefaultFastLintersExcept("govet"),
name: "disable govet in cmd",
args: []string{"-Dgovet"},
enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable gofmt in cmd and enable golint in config",
args: "-Egofmt",
args: []string{"-Egofmt"},
cfg: `
linters:
enable:
- golint
`,
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
enabledLinters: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
},
{
name: "fast option in config",
cfg: `
linters:
fast: true
`,
el: getEnabledByDefaultFastLintersWith(),
enabledLinters: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
Expand All @@ -133,13 +131,13 @@ func TestEnabledLinters(t *testing.T) {
linters:
fast: false
`,
el: getEnabledByDefaultLinters(),
enabledLinters: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "set fast option in command-line",
args: "--fast",
el: getEnabledByDefaultFastLintersWith(),
args: []string{"--fast"},
enabledLinters: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
Expand All @@ -148,8 +146,8 @@ func TestEnabledLinters(t *testing.T) {
linters:
fast: false
`,
args: "--fast",
el: getEnabledByDefaultFastLintersWith(),
args: []string{"--fast"},
enabledLinters: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
Expand All @@ -158,36 +156,42 @@ func TestEnabledLinters(t *testing.T) {
linters:
fast: true
`,
args: "--fast=false",
el: getEnabledByDefaultLinters(),
args: []string{"--fast=false"},
enabledLinters: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "fast option combined with enable and enable-all",
args: "--enable-all --fast --enable=unused",
el: getAllFastLintersWith("unused"),
args: []string{"--enable-all", "--fast", "--enable=unused"},
enabledLinters: getAllFastLintersWith("unused"),
noImplicitFast: true,
},
}

runner := testshared.NewLintRunner(t)
testshared.InstallGolangciLint(t)

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

runArgs := []string{"--verbose"}
args := []string{"--verbose"}
if !c.noImplicitFast {
runArgs = append(runArgs, "--fast")
args = append(args, "--fast")
}
if c.args != "" {
runArgs = append(runArgs, strings.Split(c.args, " ")...)
}
r := runner.RunCommandWithYamlConfig(c.cfg, "linters", runArgs...)
sort.StringSlice(c.el).Sort()

expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(c.el), strings.Join(c.el, " "))
r.ExpectOutputContains(expectedLine)
r := testshared.NewRunnerBuilder(t).
WithCommand("linters").
WithArgs(args...).
WithArgs(c.args...).
WithConfig(c.cfg).
Runner().
Run()

sort.StringSlice(c.enabledLinters).Sort()

r.ExpectOutputContains(fmt.Sprintf("Active %d linters: [%s]",
len(c.enabledLinters), strings.Join(c.enabledLinters, " ")))
})
}
}
32 changes: 15 additions & 17 deletions test/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,34 @@ func TestFix(t *testing.T) {
err := exec.Command("cp", "-R", fixDir, tmpDir).Run()
require.NoError(t, err)

testshared.InstallGolangciLint(t)

inputs := findSources(tmpDir, "in", "*.go")
for _, input := range inputs {
input := input
t.Run(filepath.Base(input), func(t *testing.T) {
t.Parallel()

args := []string{
"--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests.
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
"--allow-parallel-runners", "--fix",
input,
}
rc := extractRunContextFromComments(t, input)
rc := testshared.ParseTestDirectives(t, input)
if rc == nil {
t.Logf("Skipped: %s", input)
return
}

args = append(args, rc.args...)

var runResult *testshared.RunResult
if rc.configPath != "" {
args = append(args, "-c", rc.configPath)
runResult = testshared.NewLintRunner(t).RunCommand("run", args...)
} else {
runResult = testshared.NewLintRunner(t).RunWithYamlConfig("", args...)
}
runResult := testshared.NewRunnerBuilder(t).
WithRunContext(rc).
WithTargetPath(input).
WithArgs(
"--disable-all",
"--print-issued-lines=false",
"--print-linter-name=false",
"--out-format=line-number",
"--fix").
Runner().
Run()

// nolintlint test uses non existing linters (bob, alice)
if rc.expectedLinter != "nolintlint" {
if rc.ExpectedLinter != "nolintlint" {
runResult.ExpectExitCode(exitcodes.Success)
}

Expand Down
Loading