forked from golangci/revgrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevgrep_test.go
317 lines (274 loc) · 7.79 KB
/
revgrep_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
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
package revgrep
import (
"bufio"
"bytes"
"io"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"
"testing"
)
func setup(t *testing.T, stage, subdir string) (string, []byte) {
t.Helper()
wd, err := os.Getwd()
if err != nil {
t.Fatalf("could not get working dir: %s", err)
}
testDataDir := filepath.Join(wd, "testdata")
// Execute make
cmd := exec.Command("bash", "./make.sh", stage)
cmd.Dir = testDataDir
gitOutput, err := cmd.CombinedOutput()
if err != nil {
t.Logf("%s: git setup: %s", stage, string(gitOutput))
t.Fatalf("could not run make.sh: %v", err)
}
gitDir := filepath.Join(testDataDir, "git")
t.Cleanup(func() {
_ = os.RemoveAll(gitDir)
})
cmd = exec.Command("go", "vet", "./...")
cmd.Dir = gitDir
goVetOutput, err := cmd.CombinedOutput()
if cmd.ProcessState.ExitCode() != 2 {
t.Logf("%s: go vet: %s", stage, string(goVetOutput))
t.Fatalf("could not run go vet: %v", err)
}
// chdir so the vcs exec commands read the correct testdata
err = os.Chdir(filepath.Join(gitDir, subdir))
if err != nil {
t.Fatalf("could not chdir: %v", err)
}
if stage == "11-abs-path" {
goVetOutput = regexp.MustCompile(`(.+\.go)`).
ReplaceAll(goVetOutput, []byte(filepath.Join(gitDir, "$1")))
}
// clean go vet output
goVetOutput = bytes.ReplaceAll(goVetOutput, []byte("."+string(filepath.Separator)), []byte(""))
t.Logf("%s: go vet clean: %s", stage, string(goVetOutput))
return wd, goVetOutput
}
func teardown(t *testing.T, wd string) {
t.Helper()
err := os.Chdir(wd)
if err != nil {
t.Fatalf("could not chdir: %v", err)
}
}
// TestCheckerRegexp tests line matching and extraction of issue.
func TestCheckerRegexp(t *testing.T) {
tests := []struct {
regexp string
line string
want Issue
}{
{
line: "file.go:1:issue",
want: Issue{File: "file.go", LineNo: 1, HunkPos: 2, Issue: "file.go:1:issue", Message: "issue"},
},
{
line: "file.go:1:5:issue",
want: Issue{File: "file.go", LineNo: 1, ColNo: 5, HunkPos: 2, Issue: "file.go:1:5:issue", Message: "issue"},
},
{
line: "file.go:1: issue",
want: Issue{File: "file.go", LineNo: 1, HunkPos: 2, Issue: "file.go:1: issue", Message: "issue"},
},
{
regexp: `.*?:(.*?\.go):([0-9]+):()(.*)`,
line: "prefix:file.go:1:issue",
want: Issue{File: "file.go", LineNo: 1, HunkPos: 2, Issue: "prefix:file.go:1:issue", Message: "issue"},
},
}
diff := []byte(`--- a/file.go
+++ b/file.go
@@ -1,1 +1,1 @@
-func Line() {}
+func NewLine() {}`)
for _, test := range tests {
checker := Checker{
Patch: bytes.NewReader(diff),
Regexp: test.regexp,
}
issues, err := checker.Check(bytes.NewReader([]byte(test.line)), io.Discard)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := []Issue{test.want}
if !reflect.DeepEqual(issues, want) {
t.Errorf("unexpected issues for line: %q\nhave: %#v\nwant: %#v", test.line, issues, want)
}
}
}
// TestWholeFile tests Checker.WholeFiles will report any issues in files that have changes, even if
// they are outside the diff.
func TestWholeFiles(t *testing.T) {
tests := []struct {
name string
line string
matches bool
}{
{
name: "inside diff",
line: "file.go:1:issue",
matches: true,
},
{
name: "outside diff",
line: "file.go:10:5:issue",
matches: true,
},
{
name: "different file",
line: "file2.go:1:issue",
},
}
diff := []byte(`--- a/file.go
+++ b/file.go
@@ -1,1 +1,1 @@
-func Line() {}
+func NewLine() {}`)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
checker := Checker{
Patch: bytes.NewReader(diff),
WholeFiles: true,
}
issues, err := checker.Check(bytes.NewReader([]byte(test.line)), io.Discard)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if test.matches && len(issues) != 1 {
t.Fatalf("expected one issue to be returned, but got %#v", issues)
}
if !test.matches && len(issues) != 0 {
t.Fatalf("expected no issues to be returned, but got %#v", issues)
}
})
}
}
// Tests the writer in the argument to the Changes function
// and generally tests the entire program functionality.
func TestChecker_Check_changesWriter(t *testing.T) {
tests := map[string]struct {
subdir string
exp []string // file:linenumber including trailing colon
revFrom string
revTo string
}{
"2-untracked": {exp: []string{"main.go:3:"}},
"3-untracked-subdir": {exp: []string{"main.go:3:", "subdir/main.go:3:"}},
"3-untracked-subdir-cwd": {subdir: "subdir", exp: []string{"main.go:3:"}},
"4-commit": {exp: []string{"main.go:3:", "subdir/main.go:3:"}},
"5-unstaged-no-warning": {},
"6-unstaged": {exp: []string{"main.go:6:"}},
// From a commit, all changes should be shown
"7-commit": {exp: []string{"main.go:6:"}, revFrom: "HEAD~1"},
// From a commit+unstaged, all changes should be shown
"8-unstaged": {exp: []string{"main.go:6:", "main.go:7:"}, revFrom: "HEAD~1"},
// From a commit+unstaged+untracked, all changes should be shown
"9-untracked": {exp: []string{"main.go:6:", "main.go:7:", "main2.go:3:"}, revFrom: "HEAD~1"},
// From a commit to last commit, all changes should be shown except recent unstaged, untracked
"10-committed": {exp: []string{"main.go:6:"}, revFrom: "HEAD~1", revTo: "HEAD~0"},
// static analysis tools with absolute paths should be handled
"11-abs-path": {exp: []string{"main.go:6:"}, revFrom: "HEAD~1", revTo: "HEAD~0"},
// Removing a single line shouldn't raise any issues.
"12-removed-lines": {},
}
for stage, test := range tests {
t.Run(stage, func(t *testing.T) {
prevwd, goVetOutput := setup(t, stage, test.subdir)
var out bytes.Buffer
c := Checker{
RevisionFrom: test.revFrom,
RevisionTo: test.revTo,
}
_, err := c.Check(bytes.NewBuffer(goVetOutput), &out)
if err != nil {
t.Errorf("%s: unexpected error: %v", stage, err)
}
var lines []string
scanner := bufio.NewScanner(&out)
for scanner.Scan() {
// Rewrite abs paths to for simpler matching
line := rewriteAbs(scanner.Text())
lines = append(lines, strings.TrimPrefix(line, "./"))
}
sort.Slice(lines, func(i, j int) bool {
return lines[i] <= lines[j]
})
var count int
for i, line := range lines {
count++
if i > len(test.exp)-1 {
t.Errorf("%s: unexpected line: %q", stage, line)
} else if !strings.HasPrefix(line, filepath.FromSlash(test.exp[i])) {
t.Errorf("%s: line %q does not have prefix %q", stage, line, filepath.FromSlash(test.exp[i]))
}
}
if count != len(test.exp) {
t.Errorf("%s: got %d, expected %d", stage, count, len(test.exp))
}
teardown(t, prevwd)
})
}
}
func rewriteAbs(line string) string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
return strings.TrimPrefix(line, cwd+string(filepath.Separator))
}
func TestGitPatchNonGitDir(t *testing.T) {
// Change to non-git dir
err := os.Chdir("/")
if err != nil {
t.Fatalf("could not chdir: %v", err)
}
patch, newfiles, err := GitPatch("", "")
if err != nil {
t.Errorf("error expected nil, got: %v", err)
}
if patch != nil {
t.Errorf("patch expected nil, got: %v", patch)
}
if newfiles != nil {
t.Errorf("newFiles expected nil, got: %v", newfiles)
}
}
func TestLinesChanged(t *testing.T) {
diff := []byte(`--- a/file.go
+++ b/file.go
@@ -1,1 +1,1 @@
// comment
-func Line() {}
+func NewLine() {}
@@ -20,1 +20,1 @@
// comment
-func Line() {}
+func NewLine() {}
// comment
@@ -3,1 +30,1 @@
-func Line() {}
+func NewLine() {}
// comment`)
checker := Checker{
Patch: bytes.NewReader(diff),
}
have := checker.linesChanged()
want := map[string][]pos{
"file.go": {
{lineNo: 2, hunkPos: 3},
{lineNo: 21, hunkPos: 7},
{lineNo: 30, hunkPos: 11},
},
}
if !reflect.DeepEqual(have, want) {
t.Errorf("unexpected pos:\nhave: %#v\nwant: %#v", have, want)
}
}