forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestshared.go
155 lines (126 loc) · 3.85 KB
/
testshared.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
package testshared
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
"github.com/stretchr/testify/assert"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type LintRunner struct {
t assert.TestingT
log logutils.Log
env []string
installOnce sync.Once
}
func NewLintRunner(t assert.TestingT, environ ...string) *LintRunner {
log := logutils.NewStderrLog("test")
log.SetLevel(logutils.LogLevelInfo)
return &LintRunner{
t: t,
log: log,
env: environ,
}
}
func (r *LintRunner) Install() {
r.installOnce.Do(func() {
if os.Getenv("GOLANGCI_LINT_INSTALLED") == "true" {
return
}
cmd := exec.Command("make", "-C", "..", "build")
assert.NoError(r.t, cmd.Run(), "Can't go install golangci-lint")
})
}
type RunResult struct {
t assert.TestingT
output string
exitCode int
}
func (r *RunResult) ExpectNoIssues() {
assert.Equal(r.t, "", r.output, "exit code is %d", r.exitCode)
assert.Equal(r.t, exitcodes.Success, r.exitCode, "output is %s", r.output)
}
func (r *RunResult) ExpectExitCode(possibleCodes ...int) *RunResult {
for _, pc := range possibleCodes {
if pc == r.exitCode {
return r
}
}
assert.Fail(r.t, "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 *RunResult) ExpectOutputRegexp(s interface{}) *RunResult {
assert.Regexp(r.t, s, r.output, "exit code is %d", r.exitCode)
return r
}
func (r *RunResult) ExpectOutputContains(s string) *RunResult {
assert.Contains(r.t, r.output, s, "exit code is %d", r.exitCode)
return r
}
func (r *RunResult) ExpectOutputEq(s string) *RunResult {
assert.Equal(r.t, s, r.output, "exit code is %d", r.exitCode)
return r
}
func (r *RunResult) ExpectHasIssue(issueText string) *RunResult {
return r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputContains(issueText)
}
func (r *LintRunner) Run(args ...string) *RunResult {
newArgs := append([]string{"--allow-parallel-runners"}, args...)
return r.RunCommand("run", newArgs...)
}
func (r *LintRunner) RunCommand(command string, args ...string) *RunResult {
r.Install()
runArgs := append([]string{command}, args...)
defer func(startedAt time.Time) {
r.log.Infof("ran [../golangci-lint %s] in %s", strings.Join(runArgs, " "), time.Since(startedAt))
}(time.Now())
cmd := exec.Command("../golangci-lint", runArgs...)
cmd.Env = append(os.Environ(), r.env...)
out, err := cmd.CombinedOutput()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
r.log.Infof("stderr: %s", exitError.Stderr)
ws := exitError.Sys().(syscall.WaitStatus)
return &RunResult{
t: r.t,
output: string(out),
exitCode: ws.ExitStatus(),
}
}
r.t.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 &RunResult{
t: r.t,
output: string(out),
exitCode: ws.ExitStatus(),
}
}
func (r *LintRunner) RunWithYamlConfig(cfg string, args ...string) *RunResult {
newArgs := append([]string{"--allow-parallel-runners"}, args...)
return r.RunCommandWithYamlConfig(cfg, "run", newArgs...)
}
func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...string) *RunResult {
f, err := ioutil.TempFile("", "golangci_lint_test")
assert.NoError(r.t, err)
f.Close()
cfgPath := f.Name() + ".yml"
err = os.Rename(f.Name(), cfgPath)
assert.NoError(r.t, err)
if os.Getenv("GL_KEEP_TEMP_FILES") != "1" {
defer os.Remove(cfgPath)
}
cfg = strings.TrimSpace(cfg)
cfg = strings.Replace(cfg, "\t", " ", -1)
err = ioutil.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
assert.NoError(r.t, err)
pargs := append([]string{"-c", cfgPath}, args...)
return r.RunCommand(command, pargs...)
}