-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrunner.go
335 lines (246 loc) · 6.69 KB
/
runner.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package testshared
import (
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/golangci/golangci-lint/v2/pkg/exitcodes"
"github.com/golangci/golangci-lint/v2/pkg/fsutils"
"github.com/golangci/golangci-lint/v2/pkg/logutils"
)
// value: "1"
const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
type RunnerBuilder struct {
tb testing.TB
log logutils.Log
binPath string
command string
env []string
configPath string
noConfig bool
allowParallelRunners bool
args []string
target string
}
func NewRunnerBuilder(tb testing.TB) *RunnerBuilder {
tb.Helper()
log := logutils.NewStderrLog(logutils.DebugKeyTest)
log.SetLevel(logutils.LogLevelInfo)
return &RunnerBuilder{
tb: tb,
log: log,
command: "run",
allowParallelRunners: true,
}
}
func (b *RunnerBuilder) WithBinPath(binPath string) *RunnerBuilder {
b.binPath = binPath
return b
}
func (b *RunnerBuilder) WithCommand(command string) *RunnerBuilder {
b.command = command
return b
}
func (b *RunnerBuilder) WithNoConfig() *RunnerBuilder {
b.noConfig = true
return b
}
func (b *RunnerBuilder) WithConfigFile(cfgPath string) *RunnerBuilder {
if cfgPath != "" {
b.configPath = filepath.FromSlash(cfgPath)
}
b.noConfig = cfgPath == ""
return b
}
func (b *RunnerBuilder) WithConfig(cfg string) *RunnerBuilder {
b.tb.Helper()
content := strings.ReplaceAll(strings.TrimSpace(cfg), "\t", " ")
if content == "" {
return b.WithNoConfig()
}
cfgFile, err := os.CreateTemp("", "golangci_lint_test*.yml")
require.NoError(b.tb, err)
cfgPath := cfgFile.Name()
b.tb.Cleanup(func() {
if os.Getenv(envKeepTempFiles) != "1" {
_ = os.Remove(cfgPath)
}
})
_, err = cfgFile.WriteString(content)
require.NoError(b.tb, err)
return b.WithConfigFile(cfgPath)
}
func (b *RunnerBuilder) WithRunContext(rc *RunContext) *RunnerBuilder {
if rc == nil {
return b
}
dir, err := os.Getwd()
require.NoError(b.tb, err)
configPath := filepath.FromSlash(rc.ConfigPath)
base := filepath.Base(dir)
if strings.HasPrefix(configPath, base) {
configPath = strings.TrimPrefix(configPath, base+string(filepath.Separator))
}
return b.WithConfigFile(configPath).WithArgs(rc.Args...)
}
func (b *RunnerBuilder) WithDirectives(sourcePath string) *RunnerBuilder {
b.tb.Helper()
return b.WithRunContext(ParseTestDirectives(b.tb, sourcePath))
}
func (b *RunnerBuilder) WithEnviron(environ ...string) *RunnerBuilder {
b.env = environ
return b
}
func (b *RunnerBuilder) WithNoParallelRunners() *RunnerBuilder {
b.allowParallelRunners = false
return b
}
func (b *RunnerBuilder) WithArgs(args ...string) *RunnerBuilder {
b.args = append(b.args, args...)
return b
}
func (b *RunnerBuilder) WithTargetPath(targets ...string) *RunnerBuilder {
b.target = filepath.Join(targets...)
return b
}
func (b *RunnerBuilder) Runner() *Runner {
b.tb.Helper()
if b.noConfig && b.configPath != "" {
b.tb.Fatal("--no-config and -c cannot be used at the same time")
}
var arguments []string
if b.command == "run" {
arguments = append(arguments, "--internal-cmd-test")
if b.allowParallelRunners {
arguments = append(arguments, "--allow-parallel-runners")
}
}
if b.noConfig {
arguments = append(arguments, "--no-config")
}
if b.configPath != "" {
arguments = append(arguments, "-c", b.configPath)
}
if len(b.args) != 0 {
arguments = append(arguments, b.args...)
}
if b.target != "" {
arguments = append(arguments, b.target)
}
return &Runner{
binPath: b.binPath,
log: b.log,
tb: b.tb,
env: b.env,
command: b.command,
args: arguments,
}
}
type Runner struct {
log logutils.Log
tb testing.TB
binPath string
env []string
command string
args []string
installOnce sync.Once
}
func (r *Runner) Install() *Runner {
r.tb.Helper()
r.installOnce.Do(func() {
r.binPath = InstallGolangciLint(r.tb)
})
return r
}
func (r *Runner) Run() *RunnerResult {
r.tb.Helper()
runArgs := append([]string{r.command}, r.args...)
defer func(startedAt time.Time) {
r.log.Infof("ran [%s %s] in %s", r.binPath, strings.Join(runArgs, " "), time.Since(startedAt))
}(time.Now())
cmd := r.Command()
out, err := cmd.CombinedOutput()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
if len(exitError.Stderr) != 0 {
r.log.Infof("stderr: %s", exitError.Stderr)
}
ws := exitError.Sys().(syscall.WaitStatus)
return &RunnerResult{
tb: r.tb,
output: string(out),
exitCode: ws.ExitStatus(),
}
}
r.tb.Errorf("can't get error code from %s", err)
return nil
}
// success, exitCode should be 0 if go is ok
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
return &RunnerResult{
tb: r.tb,
output: string(out),
exitCode: ws.ExitStatus(),
}
}
func (r *Runner) Command() *exec.Cmd {
r.tb.Helper()
runArgs := append([]string{r.command}, r.args...)
//nolint:gosec // we don't use user input here
cmd := exec.Command(r.binPath, runArgs...)
cmd.Env = append(os.Environ(), r.env...)
return cmd
}
type RunnerResult struct {
tb testing.TB
output string
exitCode int
}
func (r *RunnerResult) ExpectNoIssues() {
r.tb.Helper()
assert.Empty(r.tb, r.output, "exit code is %d", r.exitCode)
assert.Equal(r.tb, exitcodes.Success, r.exitCode, "output is %s", r.output)
}
func (r *RunnerResult) ExpectExitCode(possibleCodes ...int) *RunnerResult {
r.tb.Helper()
for _, pc := range possibleCodes {
if pc == r.exitCode {
return r
}
}
assert.Fail(r.tb, "invalid exit code", "exit code (%d) must be one of %v: %s", r.exitCode, possibleCodes, r.output)
return r
}
// ExpectOutputRegexp can be called with either a string or compiled regexp
func (r *RunnerResult) ExpectOutputRegexp(s string) *RunnerResult {
r.tb.Helper()
assert.Regexp(r.tb, fsutils.NormalizePathInRegex(s), r.output, "exit code is %d", r.exitCode)
return r
}
func (r *RunnerResult) ExpectOutputContains(s ...string) *RunnerResult {
r.tb.Helper()
for _, expected := range s {
assert.Contains(r.tb, r.output, normalizeFilePath(expected), "exit code is %d", r.exitCode)
}
return r
}
func (r *RunnerResult) ExpectOutputNotContains(s string) *RunnerResult {
r.tb.Helper()
assert.NotContains(r.tb, r.output, s, "exit code is %d", r.exitCode)
return r
}
func (r *RunnerResult) ExpectOutputEq(s string) *RunnerResult {
r.tb.Helper()
assert.Equal(r.tb, normalizeFilePath(s), r.output, "exit code is %d", r.exitCode)
return r
}
func (r *RunnerResult) ExpectHasIssue(issueText string) *RunnerResult {
r.tb.Helper()
return r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputContains(issueText)
}