forked from golangci/golangci-lint
/
run_test.go
218 lines (175 loc) · 7.32 KB
/
run_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
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
package test
import (
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/golangci/golangci-lint/test/testshared"
"github.com/golangci/golangci-lint/pkg/exitcodes"
_ "github.com/valyala/quicktemplate"
)
func getCommonRunArgs() []string {
return []string{"--skip-dirs", "testdata_etc/,pkg/golinters/goanalysis/(checker|passes)"}
}
func withCommonRunArgs(args ...string) []string {
return append(getCommonRunArgs(), args...)
}
func TestAutogeneratedNoIssues(t *testing.T) {
testshared.NewLintRunner(t).Run(getTestDataDir("autogenerated")).ExpectNoIssues()
}
func TestEmptyDirRun(t *testing.T) {
testshared.NewLintRunner(t, "GO111MODULE=off").Run(getTestDataDir("nogofiles")).
ExpectExitCode(exitcodes.NoGoFiles).
ExpectOutputContains(": no go files to analyze")
}
func TestNotExistingDirRun(t *testing.T) {
testshared.NewLintRunner(t, "GO111MODULE=off").Run(getTestDataDir("no_such_dir")).
ExpectExitCode(exitcodes.Failure).
ExpectOutputContains(`cannot find package \"./testdata/no_such_dir\"`)
}
func TestSymlinkLoop(t *testing.T) {
testshared.NewLintRunner(t).Run(getTestDataDir("symlink_loop", "...")).ExpectNoIssues()
}
func TestDeadline(t *testing.T) {
testshared.NewLintRunner(t).Run("--deadline=1ms", getProjectRoot()).
ExpectExitCode(exitcodes.Timeout).
ExpectOutputContains(`Deadline exceeded: try increase it by passing --deadline option`)
}
func TestTestsAreLintedByDefault(t *testing.T) {
testshared.NewLintRunner(t).Run(getTestDataDir("withtests")).
ExpectHasIssue("`if` block ends with a `return`")
}
func TestCgoOk(t *testing.T) {
testshared.NewLintRunner(t).Run("--enable-all", getTestDataDir("cgo")).ExpectNoIssues()
}
func TestCgoWithIssues(t *testing.T) {
testshared.NewLintRunner(t).Run("--enable-all", getTestDataDir("cgo_with_issues")).
ExpectHasIssue("Printf format %t has arg cs of wrong type")
}
func TestUnsafeOk(t *testing.T) {
testshared.NewLintRunner(t).Run("--enable-all", getTestDataDir("unsafe")).ExpectNoIssues()
}
func TestGovetCustomFormatter(t *testing.T) {
testshared.NewLintRunner(t).Run(getTestDataDir("govet_custom_formatter")).ExpectNoIssues()
}
func TestLineDirectiveProcessedFilesLiteLoading(t *testing.T) {
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config",
"--exclude-use-default=false", "-Egolint", getTestDataDir("quicktemplate"))
output := strings.Join([]string{
"testdata/quicktemplate/hello.qtpl.go:26:1: exported function `StreamHello` should have comment or be unexported (golint)",
"testdata/quicktemplate/hello.qtpl.go:50:1: exported function `Hello` should have comment or be unexported (golint)",
"testdata/quicktemplate/hello.qtpl.go:39:1: exported function `WriteHello` should have comment or be unexported (golint)",
}, "\n")
r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq(output + "\n")
}
func TestLineDirectiveProcessedFilesFullLoading(t *testing.T) {
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config",
"--exclude-use-default=false", "-Egolint,govet", getTestDataDir("quicktemplate"))
output := strings.Join([]string{
"testdata/quicktemplate/hello.qtpl.go:26:1: exported function `StreamHello` should have comment or be unexported (golint)",
"testdata/quicktemplate/hello.qtpl.go:50:1: exported function `Hello` should have comment or be unexported (golint)",
"testdata/quicktemplate/hello.qtpl.go:39:1: exported function `WriteHello` should have comment or be unexported (golint)",
}, "\n")
r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq(output + "\n")
}
func TestSkippedDirsNoMatchArg(t *testing.T) {
dir := getTestDataDir("skipdirs", "skip_me", "nested")
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "--skip-dirs", dir, "-Egolint", dir)
r.ExpectExitCode(exitcodes.IssuesFound).
ExpectOutputEq("testdata/skipdirs/skip_me/nested/with_issue.go:8:9: `if` block ends with " +
"a `return` statement, so drop this `else` and outdent its block (golint)\n")
}
func TestSkippedDirsTestdata(t *testing.T) {
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "-Egolint", getTestDataDir("skipdirs", "..."))
r.ExpectNoIssues() // all was skipped because in testdata
}
func TestDeadcodeNoFalsePositivesInMainPkg(t *testing.T) {
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Edeadcode", getTestDataDir("deadcode_main_pkg")).ExpectNoIssues()
}
func TestIdentifierUsedOnlyInTests(t *testing.T) {
testshared.NewLintRunner(t).Run("--no-config", "--disable-all", "-Eunused", getTestDataDir("used_only_in_tests")).ExpectNoIssues()
}
func TestUnusedCheckExported(t *testing.T) {
testshared.NewLintRunner(t).Run("-c", "testdata_etc/unused_exported/golangci.yml", "testdata_etc/unused_exported/...").ExpectNoIssues()
}
func TestConfigFileIsDetected(t *testing.T) {
checkGotConfig := func(r *testshared.RunResult) {
r.ExpectExitCode(exitcodes.Success).
ExpectOutputEq("test\n") // test config contains InternalTest: true, it triggers such output
}
r := testshared.NewLintRunner(t)
checkGotConfig(r.Run(getTestDataDir("withconfig", "pkg")))
checkGotConfig(r.Run(getTestDataDir("withconfig", "...")))
}
func TestEnableAllFastAndEnableCanCoexist(t *testing.T) {
r := testshared.NewLintRunner(t)
r.Run(withCommonRunArgs("--fast", "--enable-all", "--enable=typecheck")...).ExpectNoIssues()
r.Run(withCommonRunArgs("--enable-all", "--enable=typecheck")...).ExpectExitCode(exitcodes.Failure)
}
func TestEnabledPresetsAreNotDuplicated(t *testing.T) {
testshared.NewLintRunner(t).Run("--no-config", "-v", "-p", "style,bugs").
ExpectOutputContains("Active presets: [bugs style]")
}
func TestAbsPathDirAnalysis(t *testing.T) {
dir := filepath.Join("testdata_etc", "abspath") // abs paths don't work with testdata dir
absDir, err := filepath.Abs(dir)
assert.NoError(t, err)
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "-Egolint", absDir)
r.ExpectHasIssue("`if` block ends with a `return` statement")
}
func TestAbsPathFileAnalysis(t *testing.T) {
dir := filepath.Join("testdata_etc", "abspath", "with_issue.go") // abs paths don't work with testdata dir
absDir, err := filepath.Abs(dir)
assert.NoError(t, err)
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "-Egolint", absDir)
r.ExpectHasIssue("`if` block ends with a `return` statement")
}
func TestDisallowedOptionsInConfig(t *testing.T) {
type tc struct {
cfg string
option string
}
cases := []tc{
{
cfg: `
ruN:
Args:
- 1
`,
},
{
cfg: `
run:
CPUProfilePath: path
`,
option: "--cpu-profile-path=path",
},
{
cfg: `
run:
MemProfilePath: path
`,
option: "--mem-profile-path=path",
},
{
cfg: `
run:
Verbose: true
`,
option: "-v",
},
}
r := testshared.NewLintRunner(t)
for _, c := range cases {
// Run with disallowed option set only in config
r.RunWithYamlConfig(c.cfg, getCommonRunArgs()...).ExpectExitCode(exitcodes.Failure)
if c.option == "" {
continue
}
args := []string{c.option, "--fast"}
// Run with disallowed option set only in command-line
r.Run(withCommonRunArgs(args...)...).ExpectExitCode(exitcodes.Success)
// Run with disallowed option set both in command-line and in config
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(args...)...).ExpectExitCode(exitcodes.Failure)
}
}