-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathlinters_test.go
134 lines (100 loc) · 2.88 KB
/
linters_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/test/testshared"
)
const testdataDir = "testdata"
func TestSourcesFromTestdata(t *testing.T) {
testSourcesFromDir(t, testdataDir)
}
func TestTypecheck(t *testing.T) {
testshared.SkipOnWindows(t)
testSourcesFromDir(t, filepath.Join(testdataDir, "notcompiles"))
}
func TestSourcesFromTestdataSubDir(t *testing.T) {
subDirs := []string{
"loggercheck",
"uncalled",
}
for _, dir := range subDirs {
t.Run(dir, func(t *testing.T) {
testSourcesFromDir(t, filepath.Join(testdataDir, dir))
})
}
}
func testSourcesFromDir(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 {
source := source
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{
"--allow-parallel-runners",
"--disable-all",
"--out-format=json",
"--max-same-issues=100",
}
for _, addArg := range []string{"", "-Etypecheck"} {
caseArgs := append([]string{}, args...)
if addArg != "" {
caseArgs = append(caseArgs, addArg)
}
cmd := testshared.NewRunnerBuilder(t).
WithBinPath(binPath).
WithNoParallelRunners().
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)
}
assert.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
}