Skip to content

Commit 6a1e223

Browse files
committed
tests: use directives instead of comments
1 parent 0abb298 commit 6a1e223

File tree

162 files changed

+280
-272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+280
-272
lines changed

pkg/golinters/gofmt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ index 2c9f78d..c0d5791 100644
310310
--- a/gofmt.go
311311
+++ b/gofmt.go
312312
@@ -1,9 +1,9 @@
313-
//args: -Egofmt
313+
//golangcitest:args -Egofmt
314314
package p
315315
316316
- func gofmt(a, b int) int {

test/linters_test.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ type runContext struct {
248248

249249
func buildConfigFromShortRepr(t *testing.T, repr string, config map[string]interface{}) {
250250
kv := strings.Split(repr, "=")
251-
require.Len(t, kv, 2)
251+
require.Len(t, kv, 2, "repr: %s", repr)
252252

253253
keyParts := strings.Split(kv[0], ".")
254254
require.True(t, len(keyParts) >= 2, len(keyParts))
@@ -308,47 +308,55 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
308308
continue
309309
}
310310

311-
line = strings.TrimLeft(strings.TrimPrefix(line, "//"), " ")
312-
if strings.HasPrefix(line, "args: ") {
311+
if !strings.HasPrefix(line, "//golangcitest:") {
312+
require.Failf(t, "invalid prefix of comment line %s", line)
313+
}
314+
315+
// TODO(ldez) replace that by strings.Cut when we will drop go1.17
316+
var before string
317+
var after string
318+
if i := strings.Index(line, " "); i >= 0 {
319+
before = line[:i]
320+
after = strings.TrimSpace(line[i+len(" "):])
321+
} else {
322+
require.Failf(t, "invalid prefix of comment line %s", line)
323+
}
324+
325+
switch before {
326+
case "//golangcitest:args":
313327
require.Nil(t, rc.args)
314-
args := strings.TrimPrefix(line, "args: ")
315-
require.NotEmpty(t, args)
316-
rc.args = strings.Split(args, " ")
328+
require.NotEmpty(t, after)
329+
rc.args = strings.Split(after, " ")
317330
continue
318-
}
319331

320-
if strings.HasPrefix(line, "config: ") {
321-
repr := strings.TrimPrefix(line, "config: ")
322-
require.NotEmpty(t, repr)
332+
case "//golangcitest:config":
333+
require.NotEmpty(t, after)
323334
if rc.config == nil {
324335
rc.config = map[string]interface{}{}
325336
}
326-
buildConfigFromShortRepr(t, repr, rc.config)
337+
buildConfigFromShortRepr(t, after, rc.config)
327338
continue
328-
}
329339

330-
if strings.HasPrefix(line, "config_path: ") {
331-
configPath := strings.TrimPrefix(line, "config_path: ")
332-
require.NotEmpty(t, configPath)
333-
rc.configPath = configPath
340+
case "//golangcitest:config_path":
341+
require.NotEmpty(t, after)
342+
rc.configPath = after
334343
continue
335-
}
336344

337-
if strings.HasPrefix(line, "expected_linter: ") {
338-
expectedLinter := strings.TrimPrefix(line, "expected_linter: ")
339-
require.NotEmpty(t, expectedLinter)
340-
rc.expectedLinter = expectedLinter
345+
case "//golangcitest:expected_linter":
346+
require.NotEmpty(t, after)
347+
rc.expectedLinter = after
341348
continue
342-
}
343349

344-
require.Fail(t, "invalid prefix of comment line %s", line)
350+
default:
351+
require.Failf(t, "invalid prefix of comment line %s", line)
352+
}
345353
}
346354

347355
// guess the expected linter if none is specified
348356
if rc.expectedLinter == "" {
349357
for _, arg := range rc.args {
350358
if strings.HasPrefix(arg, "-E") && !strings.Contains(arg, ",") {
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
359+
require.Empty(t, rc.expectedLinter, "could not infer expected linter for errors because multiple linters are enabled. Please use the `//golangcitest:expected_linter ` directive in your test to indicate the linter-under-test.") //nolint:lll
352360
rc.expectedLinter = arg[2:]
353361
}
354362
}

test/testdata/asciicheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Easciicheck
1+
//golangcitest:args -Easciicheck
22
package testdata
33

44
import (

test/testdata/bidichk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Ebidichk
1+
//golangcitest:args -Ebidichk
22
package testdata
33

44
import "fmt"

test/testdata/bodyclose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Ebodyclose
1+
//golangcitest:args -Ebodyclose
22
package testdata
33

44
import (

test/testdata/containedctx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// args: -Econtainedctx
1+
//golangcitest:args -Econtainedctx
22
package testdata
33

44
import "context"

test/testdata/contextcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Econtextcheck
1+
//golangcitest:args -Econtextcheck
22
package testdata
33

44
import "context"

test/testdata/cyclop.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//args: -Ecyclop
2-
//config: linters-settings.cyclop.max-complexity=15
1+
//golangcitest:args -Ecyclop
2+
//golangcitest:config linters-settings.cyclop.max-complexity=15
33
package testdata
44

55
func cyclopComplexFunc(s string) { // ERROR "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"

test/testdata/deadcode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Edeadcode
1+
//golangcitest:args -Edeadcode
22
package testdata
33

44
var y int

test/testdata/decorder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// args: -Edecorder
2-
// config_path: testdata/configs/decorder.yml
1+
//golangcitest:args -Edecorder
2+
//golangcitest:config_path testdata/configs/decorder.yml
33
package testdata
44

55
import "math"

0 commit comments

Comments
 (0)