-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexclude_test.go
More file actions
190 lines (168 loc) · 4.48 KB
/
exclude_test.go
File metadata and controls
190 lines (168 loc) · 4.48 KB
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
package exclude_test
import (
"bytes"
"flag"
"fmt"
"html/template"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
"github.com/gostaticanalysis/exclude"
)
func TestGeneratedFile(t *testing.T) {
t.Parallel()
cases := map[string]struct {
src string
want bool // is reported?
}{
"autogen": {"// Code generated by test; DO NOT EDIT.", false},
"notautogen": {"//Code generated by test; DO NOT EDIT.", true},
"empty": {"", true},
"blank": {"// Code generated by test; DO NOT EDIT.\n", false},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(name, name+".go")
dir := writeFiles(t, map[string]string{
path: fmt.Sprintf("%s\npackage %s", tt.src, name),
})
var rec reportRecoder
analysistest.Run(t, dir, rec.new(exclude.GeneratedFile, -1), name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
})
}
}
func TestTestFile(t *testing.T) {
t.Parallel()
cases := map[string]struct {
file string
want bool // is reported?
}{
"testfile": {"a_test.go", false},
"not": {"a.go", true},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(name, tt.file)
dir := writeFiles(t, map[string]string{
path: fmt.Sprintf("package %s", name),
})
var rec reportRecoder
analysistest.Run(t, dir, rec.new(exclude.TestFile, 1), name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
})
}
}
func TestFileWithPattern(t *testing.T) {
t.Parallel()
cases := map[string]struct {
pattern string
file string
want bool // is reported?
}{
"match": {`^.+\.pb\.go$`, "a.pb.go", false},
"unmatch": {`^.+\.pb\.go$`, "a.go", true},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(name, tt.file)
dir := writeFiles(t, map[string]string{
path: fmt.Sprintf("package %s", name),
})
var rec reportRecoder
a := rec.new(exclude.FileWithPattern, 1)
a.Flags.Set("exclude-file", tt.pattern)
analysistest.Run(t, dir, a, name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
})
}
}
func TestFlags(t *testing.T) {
t.Parallel()
cases := map[string]struct {
args string
flags string
remains string
}{
"set": {"-all-exclude-testfile=false mypkg", "exclude-testfile=false", "mypkg"},
"onlyargs": {"mypkg", "", "mypkg"},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
a := exclude.By(new(analysis.Analyzer), exclude.AllFuncs...)
{ // check remains
args := strings.Split(tt.args, " ")
got, err := exclude.Flags(args, a)
if err != nil {
t.Fatal("unexpected error:", err)
}
want := strings.Split(tt.remains, " ")
if diff := cmp.Diff(want, got); diff != "" {
t.Error("remains:", diff)
}
}
if tt.flags != "" { // check flags
flags := strings.Split(tt.flags, " ")
want := make(map[string]string, len(flags))
for _, f := range flags {
kv := strings.Split(f, "=")
if len(kv) != 2 {
t.Fatal("invalid flags", f)
}
want[kv[0]] = kv[1]
}
got := map[string]string{}
a.Flags.Visit(func(f *flag.Flag) {
got[f.Name] = f.Value.String()
})
if diff := cmp.Diff(want, got); diff != "" {
t.Error("flags:", diff)
}
}
})
}
}
func TestLintIgnoreComment(t *testing.T) {
t.Parallel()
cases := map[string]struct {
comment string
want bool // is reported?
}{
"nocomment": {"", true},
"ignore": {"//lint:ignore {{.}} reason", false},
"missname": {"//lint:ignore missname reason", true},
"noreason": {"//lint:ignore {{.}}", true},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
path := filepath.Join(name, name+".go")
var rec reportRecoder
a := rec.new(exclude.LintIgnoreComment, 3)
var comment bytes.Buffer
if err := template.Must(template.New(name).Parse(tt.comment)).Execute(&comment, a.Name); err != nil {
t.Fatal("unexpected error")
}
dir := writeFiles(t, map[string]string{
path: fmt.Sprintf("package %s\n%s\nvar _ struct{}", name, &comment),
})
analysistest.Run(t, dir, a, name)
if got := rec.isReported(); got != tt.want {
t.Errorf("want %v but got %v", tt.want, got)
}
})
}
}