@@ -3,13 +3,16 @@ package test
3
3
import (
4
4
"bufio"
5
5
"fmt"
6
+ "go/build/constraint"
6
7
"os"
7
8
"os/exec"
8
9
"path"
9
10
"path/filepath"
11
+ "runtime"
10
12
"strings"
11
13
"testing"
12
14
15
+ hcversion "github.com/hashicorp/go-version"
13
16
"github.com/stretchr/testify/require"
14
17
"gopkg.in/yaml.v3"
15
18
@@ -73,7 +76,10 @@ func TestGoimportsLocal(t *testing.T) {
73
76
"--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" ,
74
77
sourcePath ,
75
78
}
79
+
76
80
rc := extractRunContextFromComments (t , sourcePath )
81
+ require .NotNil (t , rc )
82
+
77
83
args = append (args , rc .args ... )
78
84
79
85
cfg , err := yaml .Marshal (rc .config )
@@ -89,7 +95,10 @@ func TestGciLocal(t *testing.T) {
89
95
"--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" ,
90
96
sourcePath ,
91
97
}
98
+
92
99
rc := extractRunContextFromComments (t , sourcePath )
100
+ require .NotNil (t , rc )
101
+
93
102
args = append (args , rc .args ... )
94
103
95
104
cfg , err := os .ReadFile (rc .configPath )
@@ -105,7 +114,10 @@ func TestMultipleOutputs(t *testing.T) {
105
114
"--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number,json:stdout" ,
106
115
sourcePath ,
107
116
}
117
+
108
118
rc := extractRunContextFromComments (t , sourcePath )
119
+ require .NotNil (t , rc )
120
+
109
121
args = append (args , rc .args ... )
110
122
111
123
cfg , err := os .ReadFile (rc .configPath )
@@ -122,7 +134,10 @@ func TestStderrOutput(t *testing.T) {
122
134
"--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number,json:stderr" ,
123
135
sourcePath ,
124
136
}
137
+
125
138
rc := extractRunContextFromComments (t , sourcePath )
139
+ require .NotNil (t , rc )
140
+
126
141
args = append (args , rc .args ... )
127
142
128
143
cfg , err := os .ReadFile (rc .configPath )
@@ -142,7 +157,10 @@ func TestFileOutput(t *testing.T) {
142
157
fmt .Sprintf ("--out-format=json:%s,line-number" , resultPath ),
143
158
sourcePath ,
144
159
}
160
+
145
161
rc := extractRunContextFromComments (t , sourcePath )
162
+ require .NotNil (t , rc )
163
+
146
164
args = append (args , rc .args ... )
147
165
148
166
cfg , err := os .ReadFile (rc .configPath )
@@ -188,8 +206,11 @@ func testOneSource(t *testing.T, sourcePath string) {
188
206
}
189
207
190
208
rc := extractRunContextFromComments (t , sourcePath )
191
- var cfgPath string
209
+ if rc == nil {
210
+ t .Skipf ("Skipped: %s" , sourcePath )
211
+ }
192
212
213
+ var cfgPath string
193
214
if rc .config != nil {
194
215
p , finish := saveConfig (t , rc .config )
195
216
defer finish ()
@@ -254,6 +275,7 @@ func skipMultilineComment(scanner *bufio.Scanner) {
254
275
}
255
276
}
256
277
278
+ //nolint:gocyclo,funlen
257
279
func extractRunContextFromComments (t * testing.T , sourcePath string ) * runContext {
258
280
f , err := os .Open (sourcePath )
259
281
require .NoError (t , err )
@@ -275,6 +297,17 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
275
297
break
276
298
}
277
299
300
+ if strings .HasPrefix (line , "//go:build" ) || strings .HasPrefix (line , "// +build" ) {
301
+ parse , err := constraint .Parse (line )
302
+ require .NoError (t , err )
303
+
304
+ if ! parse .Eval (buildTagGoVersion ) {
305
+ return nil
306
+ }
307
+
308
+ continue
309
+ }
310
+
278
311
line = strings .TrimLeft (strings .TrimPrefix (line , "//" ), " " )
279
312
if strings .HasPrefix (line , "args: " ) {
280
313
require .Nil (t , rc .args )
@@ -315,10 +348,7 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
315
348
if rc .expectedLinter == "" {
316
349
for _ , arg := range rc .args {
317
350
if strings .HasPrefix (arg , "-E" ) && ! strings .Contains (arg , "," ) {
318
- if rc .expectedLinter != "" {
319
- 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
320
- break
321
- }
351
+ require .Empty (t , rc .expectedLinter , "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
322
352
rc .expectedLinter = arg [2 :]
323
353
}
324
354
}
@@ -327,19 +357,38 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
327
357
return rc
328
358
}
329
359
360
+ func buildTagGoVersion (tag string ) bool {
361
+ vRuntime , err := hcversion .NewVersion (strings .TrimPrefix (runtime .Version (), "go" ))
362
+ if err != nil {
363
+ return false
364
+ }
365
+
366
+ vTag , err := hcversion .NewVersion (strings .TrimPrefix (tag , "go" ))
367
+ if err != nil {
368
+ return false
369
+ }
370
+
371
+ return vRuntime .GreaterThanOrEqual (vTag )
372
+ }
373
+
330
374
func TestExtractRunContextFromComments (t * testing.T ) {
331
375
rc := extractRunContextFromComments (t , filepath .Join (testdataDir , "goimports" , "goimports.go" ))
376
+ require .NotNil (t , rc )
332
377
require .Equal (t , []string {"-Egoimports" }, rc .args )
333
378
}
334
379
335
380
func TestTparallel (t * testing.T ) {
336
381
t .Run ("should fail on missing top-level Parallel()" , func (t * testing.T ) {
337
382
sourcePath := filepath .Join (testdataDir , "tparallel" , "missing_toplevel_test.go" )
338
383
args := []string {
339
- "--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" , "--enable" , "tparallel" ,
384
+ "--disable-all" , "--enable" , "tparallel" ,
385
+ "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" ,
340
386
sourcePath ,
341
387
}
388
+
342
389
rc := extractRunContextFromComments (t , sourcePath )
390
+ require .NotNil (t , rc )
391
+
343
392
args = append (args , rc .args ... )
344
393
345
394
cfg , err := yaml .Marshal (rc .config )
@@ -354,10 +403,14 @@ func TestTparallel(t *testing.T) {
354
403
t .Run ("should fail on missing subtest Parallel()" , func (t * testing.T ) {
355
404
sourcePath := filepath .Join (testdataDir , "tparallel" , "missing_subtest_test.go" )
356
405
args := []string {
357
- "--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" , "--enable" , "tparallel" ,
406
+ "--disable-all" , "--enable" , "tparallel" ,
407
+ "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" ,
358
408
sourcePath ,
359
409
}
410
+
360
411
rc := extractRunContextFromComments (t , sourcePath )
412
+ require .NotNil (t , rc )
413
+
361
414
args = append (args , rc .args ... )
362
415
363
416
cfg , err := yaml .Marshal (rc .config )
@@ -372,10 +425,14 @@ func TestTparallel(t *testing.T) {
372
425
t .Run ("should pass on parallel test with no subtests" , func (t * testing.T ) {
373
426
sourcePath := filepath .Join (testdataDir , "tparallel" , "happy_path_test.go" )
374
427
args := []string {
375
- "--disable-all" , "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" , "--enable" , "tparallel" ,
428
+ "--disable-all" , "--enable" , "tparallel" ,
429
+ "--print-issued-lines=false" , "--print-linter-name=false" , "--out-format=line-number" ,
376
430
sourcePath ,
377
431
}
432
+
378
433
rc := extractRunContextFromComments (t , sourcePath )
434
+ require .NotNil (t , rc )
435
+
379
436
args = append (args , rc .args ... )
380
437
381
438
cfg , err := yaml .Marshal (rc .config )
0 commit comments