1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
package testshared
import (
"bufio"
"go/build/constraint"
"os"
"runtime"
"strconv"
"strings"
"testing"
hcversion "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
"github.com/golangci/golangci-lint/pkg/exitcodes"
)
// RunContext the information extracted from directives.
type RunContext struct {
Args []string
ConfigPath string
ExpectedLinter string
ExitCode int
}
// ParseTestDirectives parses test directives from sources files.
//
//nolint:gocyclo,funlen
func ParseTestDirectives(tb testing.TB, sourcePath string) *RunContext {
tb.Helper()
f, err := os.Open(sourcePath)
require.NoError(tb, err)
tb.Cleanup(func() { _ = f.Close() })
rc := &RunContext{
ExitCode: exitcodes.IssuesFound,
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "/*") {
skipMultilineComment(scanner)
continue
}
if strings.TrimSpace(line) == "" {
continue
}
if !strings.HasPrefix(line, "//") {
break
}
if constraint.IsGoBuild(line) {
if !evaluateBuildTags(tb, line) {
return nil
}
continue
}
if !strings.HasPrefix(line, "//golangcitest:") {
require.Failf(tb, "invalid prefix of comment line %s", line)
}
before, after, found := strings.Cut(line, " ")
require.Truef(tb, found, "invalid prefix of comment line %s", line)
after = strings.TrimSpace(after)
switch before {
case "//golangcitest:args":
require.Nil(tb, rc.Args)
require.NotEmpty(tb, after)
rc.Args = strings.Split(after, " ")
continue
case "//golangcitest:config_path":
require.NotEmpty(tb, after)
rc.ConfigPath = after
continue
case "//golangcitest:expected_linter":
require.NotEmpty(tb, after)
rc.ExpectedLinter = after
continue
case "//golangcitest:expected_exitcode":
require.NotEmpty(tb, after)
val, err := strconv.Atoi(after)
require.NoError(tb, err)
rc.ExitCode = val
continue
default:
require.Failf(tb, "invalid prefix of comment line %s", line)
}
}
// guess the expected linter if none is specified
if rc.ExpectedLinter == "" {
for _, arg := range rc.Args {
if strings.HasPrefix(arg, "-E") && !strings.Contains(arg, ",") {
require.Empty(tb, 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
rc.ExpectedLinter = arg[2:]
}
}
}
return rc
}
func skipMultilineComment(scanner *bufio.Scanner) {
for line := scanner.Text(); !strings.Contains(line, "*/") && scanner.Scan(); {
line = scanner.Text()
}
}
// evaluateBuildTags Naive implementation of the evaluation of the build tags.
// Inspired by https://github.com/golang/go/blob/1dcef7b3bdcea4a829ea22c821e6a9484c325d61/src/cmd/go/internal/modindex/build.go#L914-L972
func evaluateBuildTags(tb testing.TB, line string) bool {
parse, err := constraint.Parse(line)
require.NoError(tb, err)
return parse.Eval(func(tag string) bool {
if tag == runtime.GOOS {
return true
}
if buildTagGoVersion(tag) {
return true
}
return false
})
}
func buildTagGoVersion(tag string) bool {
vRuntime, err := hcversion.NewVersion(strings.TrimPrefix(runtime.Version(), "go"))
if err != nil {
return false
}
vTag, err := hcversion.NewVersion(strings.TrimPrefix(tag, "go"))
if err != nil {
return false
}
return vRuntime.GreaterThanOrEqual(vTag)
}
|