-
Notifications
You must be signed in to change notification settings - Fork 113
/
checkers_test.go
206 lines (186 loc) · 4.71 KB
/
checkers_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
package checkers
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/go-critic/go-critic/framework/linter"
"github.com/go-critic/go-critic/framework/linttest"
"github.com/google/go-cmp/cmp"
)
func TestCheckers(t *testing.T) {
allParams := map[string]map[string]interface{}{
"captLocal": {"paramsOnly": false},
}
for _, info := range linter.GetCheckersInfo() {
params := allParams[info.Name]
for key, p := range info.Params {
v, ok := params[key]
if ok {
p.Value = v
}
}
}
cfg := linttest.CheckersTest{
IgnoreErrors: []string{
"caseOrder",
},
}
cfg.Run(t)
}
func TestIntegration(t *testing.T) {
cfg := linttest.IntegrationTest{
Main: "github.com/go-critic/go-critic/cmd/gocritic",
Dir: "./testdata/_integration",
}
cfg.Run(t)
}
func TestTags(t *testing.T) {
// Verify that we're only using strict set of tags.
// This helps to avoid typos in tag names.
//
// Also check that exactly 1 category tag is used.
for _, info := range linter.GetCheckersInfo() {
categories := 0
for _, tag := range info.Tags {
switch tag {
case "diagnostic", "style", "performance":
// Category tags.
// Can only have one of them.
categories++
case "experimental", "opinionated":
// Optional tags.
default:
t.Errorf("%q checker uses unknown tag %q", info.Name, tag)
}
}
if categories != 1 {
t.Errorf("%q expected to have 1 category, found %d",
info.Name, categories)
}
}
}
func TestDocs(t *testing.T) {
for _, info := range linter.GetCheckersInfo() {
if info.Summary == "" {
t.Errorf("%q checker lacks summary", info.Name)
}
for key, p := range info.Params {
if p.Usage == "" {
t.Errorf("%q checker %q param lacks usage docs",
info.Name, key)
}
}
}
}
func TestStableList(t *testing.T) {
// Verify that new checker is not added without "experimental"
// tag by accident. When stable checker is about to be added,
// slice above should be modified to include new checker name.
// It is a good practice to keep this list sorted.
stableList := []string{
"appendAssign",
"appendCombine",
"argOrder",
"assignOp",
"badCall",
"badCond",
"builtinShadow",
"captLocal",
"caseOrder",
"codegenComment",
"commentFormatting",
"defaultCaseOrder",
"deprecatedComment",
"dupArg",
"dupBranchBody",
"dupCase",
"dupSubExpr",
"elseif",
"exitAfterDefer",
"flagDeref",
"flagName",
"hugeParam",
"ifElseChain",
"importShadow",
"indexAlloc",
"mapKey",
"newDeref",
"offBy1",
"paramTypeCombine",
"rangeExprCopy",
"rangeValCopy",
"regexpMust",
"singleCaseSwitch",
"sloppyLen",
"sloppyTypeAssert",
"stringXbytes",
"switchTrue",
"typeSwitchVar",
"typeUnparen",
"underef",
"unlambda",
"unslice",
"valSwap",
"wrapperFunc",
}
m := make(map[string]bool)
for _, name := range stableList {
m[name] = true
}
for _, info := range linter.GetCheckersInfo() {
if info.HasTag("experimental") {
continue
}
if !m[info.Name] {
t.Errorf("%q checker misses `experimental` tag", info.Name)
}
}
}
func TestExternal(t *testing.T) {
// Don't run these tests normally, unless asked to.
// Note that CI tests do enable GOCRITIC_EXTERNAL_TESTS.
if os.Getenv("GOCRITIC_EXTERNAL_TESTS") == "" {
t.Skip("GOCRITIC_EXTERNAL_TESTS is unset")
}
// Build the linter.
tmpDir := os.TempDir()
gocriticBin := filepath.Join(tmpDir, "gocritic_external_test.exe")
args := []string{"build", "-race", "-o", gocriticBin, "github.com/go-critic/go-critic/cmd/gocritic"}
out, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
t.Fatalf("%v: %s", err, out)
}
defer os.Remove(gocriticBin)
externalTests := filepath.Join(tmpDir, "extern-testdata")
out, err = exec.Command("git", "clone", "https://github.com/go-critic/extern-testdata.git", externalTests).CombinedOutput()
if err != nil {
t.Fatalf("%v: %s", err, out)
}
defer os.RemoveAll(externalTests)
projects, err := os.ReadDir(filepath.Join(externalTests, "projects"))
if err != nil {
t.Fatal(err)
}
if len(projects) == 0 {
t.Fatal("found 0 test projects")
}
for _, proj := range projects {
projectPath := filepath.Join(externalTests, "projects", proj.Name())
gocriticCmd := exec.Command(gocriticBin, "check", "--enableAll", "./...")
gocriticCmd.Dir = projectPath
out, err := gocriticCmd.CombinedOutput()
have := err.Error() + "\n" + strings.TrimSpace(string(out))
wantBytes, err := os.ReadFile(filepath.Join(projectPath, "output.golden"))
want := strings.TrimSpace(string(wantBytes))
if err != nil {
t.Errorf("read %s golden file: %v", proj.Name(), err)
continue
}
if diff := cmp.Diff(want, have); diff != "" {
t.Errorf("%s output mismatches (+have -want):\n%s", proj.Name(), diff)
continue
}
}
}