Skip to content

Commit dddfb55

Browse files
committed
dev: rewrite tests framework
1 parent f7fba37 commit dddfb55

13 files changed

+1338
-612
lines changed

test/bench/bench_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func runOne(b *testing.B, run func(*testing.B), progName string) *runResult {
201201
}
202202

203203
func BenchmarkGolangciLint(b *testing.B) {
204-
testshared.NewLintRunner(b).Install()
204+
testshared.InstallGolangciLint(b)
205205

206206
type bcase struct {
207207
name string

test/data.go

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
)
66

77
const testdataDir = "testdata"
8-
const binName = "../golangci-lint"
98

109
var minimalPkg = getTestDataDir("minimalpkg")
1110

test/enabled_linters_test.go

+39-35
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func inSlice(s []string, v string) bool {
2323
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
2424
m := lintersdb.NewManager(nil, nil)
2525
ebdl := m.GetAllEnabledByDefaultLinters()
26-
ret := []string{}
26+
var ret []string
2727
for _, lc := range ebdl {
2828
if lc.IsSlowLinter() {
2929
continue
@@ -52,7 +52,7 @@ func getAllFastLintersWith(with ...string) []string {
5252

5353
func getEnabledByDefaultLinters() []string {
5454
ebdl := lintersdb.NewManager(nil, nil).GetAllEnabledByDefaultLinters()
55-
ret := []string{}
55+
var ret []string
5656
for _, lc := range ebdl {
5757
ret = append(ret, lc.Name())
5858
}
@@ -76,23 +76,21 @@ func getEnabledByDefaultFastLintersWith(with ...string) []string {
7676

7777
//nolint:funlen
7878
func TestEnabledLinters(t *testing.T) {
79-
type tc struct {
79+
cases := []struct {
8080
name string
8181
cfg string
82-
el []string
83-
args string
82+
enabledLinters []string
83+
args []string
8484
noImplicitFast bool
85-
}
86-
87-
cases := []tc{
85+
}{
8886
{
8987
name: "disable govet in config",
9088
cfg: `
9189
linters:
9290
disable:
9391
- govet
9492
`,
95-
el: getEnabledByDefaultFastLintersExcept("govet"),
93+
enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
9694
},
9795
{
9896
name: "enable golint in config",
@@ -101,30 +99,30 @@ func TestEnabledLinters(t *testing.T) {
10199
enable:
102100
- golint
103101
`,
104-
el: getEnabledByDefaultFastLintersWith("golint"),
102+
enabledLinters: getEnabledByDefaultFastLintersWith("golint"),
105103
},
106104
{
107-
name: "disable govet in cmd",
108-
args: "-Dgovet",
109-
el: getEnabledByDefaultFastLintersExcept("govet"),
105+
name: "disable govet in cmd",
106+
args: []string{"-Dgovet"},
107+
enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
110108
},
111109
{
112110
name: "enable gofmt in cmd and enable golint in config",
113-
args: "-Egofmt",
111+
args: []string{"-Egofmt"},
114112
cfg: `
115113
linters:
116114
enable:
117115
- golint
118116
`,
119-
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
117+
enabledLinters: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
120118
},
121119
{
122120
name: "fast option in config",
123121
cfg: `
124122
linters:
125123
fast: true
126124
`,
127-
el: getEnabledByDefaultFastLintersWith(),
125+
enabledLinters: getEnabledByDefaultFastLintersWith(),
128126
noImplicitFast: true,
129127
},
130128
{
@@ -133,13 +131,13 @@ func TestEnabledLinters(t *testing.T) {
133131
linters:
134132
fast: false
135133
`,
136-
el: getEnabledByDefaultLinters(),
134+
enabledLinters: getEnabledByDefaultLinters(),
137135
noImplicitFast: true,
138136
},
139137
{
140138
name: "set fast option in command-line",
141-
args: "--fast",
142-
el: getEnabledByDefaultFastLintersWith(),
139+
args: []string{"--fast"},
140+
enabledLinters: getEnabledByDefaultFastLintersWith(),
143141
noImplicitFast: true,
144142
},
145143
{
@@ -148,8 +146,8 @@ func TestEnabledLinters(t *testing.T) {
148146
linters:
149147
fast: false
150148
`,
151-
args: "--fast",
152-
el: getEnabledByDefaultFastLintersWith(),
149+
args: []string{"--fast"},
150+
enabledLinters: getEnabledByDefaultFastLintersWith(),
153151
noImplicitFast: true,
154152
},
155153
{
@@ -158,36 +156,42 @@ func TestEnabledLinters(t *testing.T) {
158156
linters:
159157
fast: true
160158
`,
161-
args: "--fast=false",
162-
el: getEnabledByDefaultLinters(),
159+
args: []string{"--fast=false"},
160+
enabledLinters: getEnabledByDefaultLinters(),
163161
noImplicitFast: true,
164162
},
165163
{
166164
name: "fast option combined with enable and enable-all",
167-
args: "--enable-all --fast --enable=unused",
168-
el: getAllFastLintersWith("unused"),
165+
args: []string{"--enable-all", "--fast", "--enable=unused"},
166+
enabledLinters: getAllFastLintersWith("unused"),
169167
noImplicitFast: true,
170168
},
171169
}
172170

173-
runner := testshared.NewLintRunner(t)
171+
testshared.InstallGolangciLint(t)
172+
174173
for _, c := range cases {
175174
c := c
176175
t.Run(c.name, func(t *testing.T) {
177176
t.Parallel()
178177

179-
runArgs := []string{"--verbose"}
178+
args := []string{"--verbose"}
180179
if !c.noImplicitFast {
181-
runArgs = append(runArgs, "--fast")
180+
args = append(args, "--fast")
182181
}
183-
if c.args != "" {
184-
runArgs = append(runArgs, strings.Split(c.args, " ")...)
185-
}
186-
r := runner.RunCommandWithYamlConfig(c.cfg, "linters", runArgs...)
187-
sort.StringSlice(c.el).Sort()
188182

189-
expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(c.el), strings.Join(c.el, " "))
190-
r.ExpectOutputContains(expectedLine)
183+
r := testshared.NewRunnerBuilder(t).
184+
WithCommand("linters").
185+
WithArgs(args...).
186+
WithArgs(c.args...).
187+
WithConfig(c.cfg).
188+
Runner().
189+
Run()
190+
191+
sort.StringSlice(c.enabledLinters).Sort()
192+
193+
r.ExpectOutputContains(fmt.Sprintf("Active %d linters: [%s]",
194+
len(c.enabledLinters), strings.Join(c.enabledLinters, " ")))
191195
})
192196
}
193197
}

test/fix_test.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,34 @@ func TestFix(t *testing.T) {
3535
err := exec.Command("cp", "-R", fixDir, tmpDir).Run()
3636
require.NoError(t, err)
3737

38+
testshared.InstallGolangciLint(t)
39+
3840
inputs := findSources(tmpDir, "in", "*.go")
3941
for _, input := range inputs {
4042
input := input
4143
t.Run(filepath.Base(input), func(t *testing.T) {
4244
t.Parallel()
4345

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

56-
args = append(args, rc.args...)
57-
58-
var runResult *testshared.RunResult
59-
if rc.configPath != "" {
60-
args = append(args, "-c", rc.configPath)
61-
runResult = testshared.NewLintRunner(t).RunCommand("run", args...)
62-
} else {
63-
runResult = testshared.NewLintRunner(t).RunWithYamlConfig("", args...)
64-
}
52+
runResult := testshared.NewRunnerBuilder(t).
53+
WithRunContext(rc).
54+
WithTargetPath(input).
55+
WithArgs(
56+
"--disable-all",
57+
"--print-issued-lines=false",
58+
"--print-linter-name=false",
59+
"--out-format=line-number",
60+
"--fix").
61+
Runner().
62+
Run()
6563

6664
// nolintlint test uses non existing linters (bob, alice)
67-
if rc.expectedLinter != "nolintlint" {
65+
if rc.ExpectedLinter != "nolintlint" {
6866
runResult.ExpectExitCode(exitcodes.Success)
6967
}
7068

0 commit comments

Comments
 (0)