forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.go
115 lines (84 loc) · 2.48 KB
/
run.go
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
package integration
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/test/testshared"
)
const testdataDir = "testdata"
func RunTestdata(t *testing.T) {
t.Helper()
RunTestSourcesFromDir(t, testdataDir)
}
func RunTestSourcesFromDir(t *testing.T, dir string) {
t.Helper()
t.Log(filepath.Join(dir, "*.go"))
sources := findSources(t, dir, "*.go")
binPath := testshared.InstallGolangciLint(t)
cwd, err := os.Getwd()
require.NoError(t, err)
t.Cleanup(func() { _ = os.Chdir(cwd) })
err = os.Chdir(dir)
require.NoError(t, err)
log := logutils.NewStderrLog(logutils.DebugKeyTest)
log.SetLevel(logutils.LogLevelInfo)
for _, source := range sources {
t.Run(filepath.Base(source), func(subTest *testing.T) {
subTest.Parallel()
rel, err := filepath.Rel(dir, source)
require.NoError(t, err)
testOneSource(subTest, log, binPath, rel)
})
}
}
func testOneSource(t *testing.T, log *logutils.StderrLog, binPath, sourcePath string) {
t.Helper()
rc := testshared.ParseTestDirectives(t, sourcePath)
if rc == nil {
t.Skipf("Skipped: %s", sourcePath)
}
args := []string{
"--disable-all",
"--show-stats=false",
"--output.json.path=stdout",
"--max-same-issues=100",
"--max-issues-per-linter=100",
}
for _, addArg := range []string{"", "-Etypecheck"} {
caseArgs := append([]string{}, args...)
if addArg != "" {
caseArgs = append(caseArgs, addArg)
}
cmd := testshared.NewRunnerBuilder(t).
WithBinPath(binPath).
WithArgs(caseArgs...).
WithRunContext(rc).
WithTargetPath(sourcePath).
Runner().
Command()
startedAt := time.Now()
output, err := cmd.CombinedOutput()
log.Infof("ran [%s] in %s", strings.Join(cmd.Args, " "), time.Since(startedAt))
// The returned error will be nil if the test file does not have any issues
// and thus the linter exits with exit code 0.
// So perform the additional assertions only if the error is non-nil.
if err != nil {
var exitErr *exec.ExitError
require.ErrorAs(t, err, &exitErr)
}
require.Equal(t, rc.ExitCode, cmd.ProcessState.ExitCode(), "Unexpected exit code: %s", string(output))
testshared.Analyze(t, sourcePath, output)
}
}
func findSources(t *testing.T, pathPatterns ...string) []string {
t.Helper()
sources, err := filepath.Glob(filepath.Join(pathPatterns...))
require.NoError(t, err)
require.NotEmpty(t, sources)
return sources
}