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