@@ -9,9 +9,10 @@ import (
9
9
"strings"
10
10
"testing"
11
11
12
- assert "github.com/stretchr/testify/require"
13
- yaml "gopkg.in/yaml.v2"
12
+ "github.com/stretchr/testify/require"
13
+ "gopkg.in/yaml.v2"
14
14
15
+ "github.com/golangci/golangci-lint/pkg/exitcodes"
15
16
"github.com/golangci/golangci-lint/test/testshared"
16
17
)
17
18
@@ -21,29 +22,27 @@ func runGoErrchk(c *exec.Cmd, defaultExpectedLinter string, files []string, t *t
21
22
// and thus the linter exits with exit code 0. So perform the additional
22
23
// assertions only if the error is non-nil.
23
24
if err != nil {
24
- _ , ok := err .(* exec.ExitError )
25
- assert .True (t , ok , err )
25
+ var exitErr * exec.ExitError
26
+ require .ErrorAs (t , err , & exitErr )
27
+ require .Equal (t , exitcodes .IssuesFound , exitErr .ExitCode ())
26
28
}
27
29
28
- // TODO: uncomment after deprecating go1.11
29
- // assert.Equal(t, exitcodes.IssuesFound, exitErr.ExitCode())
30
-
31
30
fullshort := make ([]string , 0 , len (files )* 2 )
32
31
for _ , f := range files {
33
32
fullshort = append (fullshort , f , filepath .Base (f ))
34
33
}
35
34
36
35
err = errorCheck (string (output ), false , defaultExpectedLinter , fullshort ... )
37
- assert .NoError (t , err )
36
+ require .NoError (t , err )
38
37
}
39
38
40
39
func testSourcesFromDir (t * testing.T , dir string ) {
41
40
t .Log (filepath .Join (dir , "*.go" ))
42
41
43
42
findSources := func (pathPatterns ... string ) []string {
44
43
sources , err := filepath .Glob (filepath .Join (pathPatterns ... ))
45
- assert .NoError (t , err )
46
- assert .NotEmpty (t , sources )
44
+ require .NoError (t , err )
45
+ require .NotEmpty (t , sources )
47
46
return sources
48
47
}
49
48
sources := findSources (dir , "*.go" )
@@ -77,7 +76,7 @@ func TestGoimportsLocal(t *testing.T) {
77
76
args = append (args , rc .args ... )
78
77
79
78
cfg , err := yaml .Marshal (rc .config )
80
- assert .NoError (t , err )
79
+ require .NoError (t , err )
81
80
82
81
testshared .NewLintRunner (t ).RunWithYamlConfig (string (cfg ), args ... ).
83
82
ExpectHasIssue ("testdata/goimports/goimports.go:8: File is not `goimports`-ed" )
@@ -93,27 +92,27 @@ func TestGciLocal(t *testing.T) {
93
92
args = append (args , rc .args ... )
94
93
95
94
cfg , err := yaml .Marshal (rc .config )
96
- assert .NoError (t , err )
95
+ require .NoError (t , err )
97
96
98
97
testshared .NewLintRunner (t ).RunWithYamlConfig (string (cfg ), args ... ).
99
98
ExpectHasIssue ("testdata/gci/gci.go:7: File is not `gci`-ed" )
100
99
}
101
100
102
101
func saveConfig (t * testing.T , cfg map [string ]interface {}) (cfgPath string , finishFunc func ()) {
103
102
f , err := ioutil .TempFile ("" , "golangci_lint_test" )
104
- assert .NoError (t , err )
103
+ require .NoError (t , err )
105
104
106
105
cfgPath = f .Name () + ".yml"
107
106
err = os .Rename (f .Name (), cfgPath )
108
- assert .NoError (t , err )
107
+ require .NoError (t , err )
109
108
110
109
err = yaml .NewEncoder (f ).Encode (cfg )
111
- assert .NoError (t , err )
110
+ require .NoError (t , err )
112
111
113
112
return cfgPath , func () {
114
- assert .NoError (t , f .Close ())
113
+ require .NoError (t , f .Close ())
115
114
if os .Getenv ("GL_KEEP_TEMP_FILES" ) != "1" {
116
- assert .NoError (t , os .Remove (cfgPath ))
115
+ require .NoError (t , os .Remove (cfgPath ))
117
116
}
118
117
}
119
118
}
@@ -168,10 +167,10 @@ type runContext struct {
168
167
169
168
func buildConfigFromShortRepr (t * testing.T , repr string , config map [string ]interface {}) {
170
169
kv := strings .Split (repr , "=" )
171
- assert .Len (t , kv , 2 )
170
+ require .Len (t , kv , 2 )
172
171
173
172
keyParts := strings .Split (kv [0 ], "." )
174
- assert .True (t , len (keyParts ) >= 2 , len (keyParts ))
173
+ require .True (t , len (keyParts ) >= 2 , len (keyParts ))
175
174
176
175
lastObj := config
177
176
for _ , k := range keyParts [:len (keyParts )- 1 ] {
@@ -197,7 +196,7 @@ func skipMultilineComment(scanner *bufio.Scanner) {
197
196
198
197
func extractRunContextFromComments (t * testing.T , sourcePath string ) * runContext {
199
198
f , err := os .Open (sourcePath )
200
- assert .NoError (t , err )
199
+ require .NoError (t , err )
201
200
defer f .Close ()
202
201
203
202
rc := & runContext {}
@@ -218,16 +217,16 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
218
217
219
218
line = strings .TrimLeft (strings .TrimPrefix (line , "//" ), " " )
220
219
if strings .HasPrefix (line , "args: " ) {
221
- assert .Nil (t , rc .args )
220
+ require .Nil (t , rc .args )
222
221
args := strings .TrimPrefix (line , "args: " )
223
- assert .NotEmpty (t , args )
222
+ require .NotEmpty (t , args )
224
223
rc .args = strings .Split (args , " " )
225
224
continue
226
225
}
227
226
228
227
if strings .HasPrefix (line , "config: " ) {
229
228
repr := strings .TrimPrefix (line , "config: " )
230
- assert .NotEmpty (t , repr )
229
+ require .NotEmpty (t , repr )
231
230
if rc .config == nil {
232
231
rc .config = map [string ]interface {}{}
233
232
}
@@ -237,27 +236,27 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
237
236
238
237
if strings .HasPrefix (line , "config_path: " ) {
239
238
configPath := strings .TrimPrefix (line , "config_path: " )
240
- assert .NotEmpty (t , configPath )
239
+ require .NotEmpty (t , configPath )
241
240
rc .configPath = configPath
242
241
continue
243
242
}
244
243
245
244
if strings .HasPrefix (line , "expected_linter: " ) {
246
245
expectedLinter := strings .TrimPrefix (line , "expected_linter: " )
247
- assert .NotEmpty (t , expectedLinter )
246
+ require .NotEmpty (t , expectedLinter )
248
247
rc .expectedLinter = expectedLinter
249
248
continue
250
249
}
251
250
252
- assert .Fail (t , "invalid prefix of comment line %s" , line )
251
+ require .Fail (t , "invalid prefix of comment line %s" , line )
253
252
}
254
253
255
254
// guess the expected linter if none is specified
256
255
if rc .expectedLinter == "" {
257
256
for _ , arg := range rc .args {
258
257
if strings .HasPrefix (arg , "-E" ) && ! strings .Contains (arg , "," ) {
259
258
if rc .expectedLinter != "" {
260
- assert .Fail (t , "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
259
+ require .Fail (t , "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
261
260
break
262
261
}
263
262
rc .expectedLinter = arg [2 :]
@@ -270,7 +269,7 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
270
269
271
270
func TestExtractRunContextFromComments (t * testing.T ) {
272
271
rc := extractRunContextFromComments (t , filepath .Join (testdataDir , "goimports" , "goimports.go" ))
273
- assert .Equal (t , []string {"-Egoimports" }, rc .args )
272
+ require .Equal (t , []string {"-Egoimports" }, rc .args )
274
273
}
275
274
276
275
func TestTparallel (t * testing.T ) {
@@ -284,7 +283,7 @@ func TestTparallel(t *testing.T) {
284
283
args = append (args , rc .args ... )
285
284
286
285
cfg , err := yaml .Marshal (rc .config )
287
- assert .NoError (t , err )
286
+ require .NoError (t , err )
288
287
289
288
testshared .NewLintRunner (t ).RunWithYamlConfig (string (cfg ), args ... ).
290
289
ExpectHasIssue (
@@ -302,7 +301,7 @@ func TestTparallel(t *testing.T) {
302
301
args = append (args , rc .args ... )
303
302
304
303
cfg , err := yaml .Marshal (rc .config )
305
- assert .NoError (t , err )
304
+ require .NoError (t , err )
306
305
307
306
testshared .NewLintRunner (t ).RunWithYamlConfig (string (cfg ), args ... ).
308
307
ExpectHasIssue (
@@ -320,7 +319,7 @@ func TestTparallel(t *testing.T) {
320
319
args = append (args , rc .args ... )
321
320
322
321
cfg , err := yaml .Marshal (rc .config )
323
- assert .NoError (t , err )
322
+ require .NoError (t , err )
324
323
325
324
testshared .NewLintRunner (t ).RunWithYamlConfig (string (cfg ), args ... ).ExpectNoIssues ()
326
325
})
0 commit comments