@@ -15,7 +15,7 @@ import (
15
15
"testing"
16
16
)
17
17
18
- func setup (t * testing.T , stage , subdir string ) ( string , []byte ) {
18
+ func setup (t * testing.T , stage , subdir string ) []byte {
19
19
t .Helper ()
20
20
21
21
wd , err := os .Getwd ()
@@ -65,20 +65,18 @@ func setup(t *testing.T, stage, subdir string) (string, []byte) {
65
65
66
66
t .Logf ("%s: go vet clean: %s" , stage , string (goVetOutput ))
67
67
68
- return wd , goVetOutput
69
- }
70
-
71
- func teardown (t * testing.T , wd string ) {
72
- t .Helper ()
68
+ t .Cleanup (func () {
69
+ err = os .Chdir (wd )
70
+ if err != nil {
71
+ t .Fatalf ("could not chdir: %v" , err )
72
+ }
73
+ })
73
74
74
- err := os .Chdir (wd )
75
- if err != nil {
76
- t .Fatalf ("could not chdir: %v" , err )
77
- }
75
+ return goVetOutput
78
76
}
79
77
80
- // TestCheckerRegexp tests line matching and extraction of issue.
81
- func TestCheckerRegexp (t * testing.T ) {
78
+ // Tests line matching and extraction of issue.
79
+ func TestChecker_Check_Regexp (t * testing.T ) {
82
80
tests := []struct {
83
81
regexp string
84
82
line string
@@ -127,9 +125,9 @@ func TestCheckerRegexp(t *testing.T) {
127
125
}
128
126
}
129
127
130
- // TestWholeFile tests Checker.WholeFiles will report any issues in files that have changes, even if
131
- // they are outside the diff.
132
- func TestWholeFiles (t * testing.T ) {
128
+ // Tests [ Checker.WholeFiles] will report any issues in files that have changes,
129
+ // even if they are outside the diff.
130
+ func TestChecker_Check_WholeFiles (t * testing.T ) {
133
131
tests := []struct {
134
132
name string
135
133
line string
@@ -209,14 +207,15 @@ func TestChecker_Check_changesWriter(t *testing.T) {
209
207
210
208
for stage , test := range tests {
211
209
t .Run (stage , func (t * testing.T ) {
212
- prevwd , goVetOutput := setup (t , stage , test .subdir )
210
+ goVetOutput := setup (t , stage , test .subdir )
213
211
214
212
var out bytes.Buffer
215
213
216
214
c := Checker {
217
215
RevisionFrom : test .revFrom ,
218
216
RevisionTo : test .revTo ,
219
217
}
218
+
220
219
_ , err := c .Check (context .Background (), bytes .NewBuffer (goVetOutput ), & out )
221
220
if err != nil {
222
221
t .Errorf ("%s: unexpected error: %v" , stage , err )
@@ -227,7 +226,7 @@ func TestChecker_Check_changesWriter(t *testing.T) {
227
226
scanner := bufio .NewScanner (& out )
228
227
for scanner .Scan () {
229
228
// Rewrite abs paths to for simpler matching
230
- line := rewriteAbs (scanner .Text ())
229
+ line := rewriteAbs (t , scanner .Text ())
231
230
lines = append (lines , strings .TrimPrefix (line , "./" ))
232
231
}
233
232
@@ -248,21 +247,11 @@ func TestChecker_Check_changesWriter(t *testing.T) {
248
247
if count != len (test .exp ) {
249
248
t .Errorf ("%s: got %d, expected %d" , stage , count , len (test .exp ))
250
249
}
251
-
252
- teardown (t , prevwd )
253
250
})
254
251
}
255
252
}
256
253
257
- func rewriteAbs (line string ) string {
258
- cwd , err := os .Getwd ()
259
- if err != nil {
260
- panic (err )
261
- }
262
- return strings .TrimPrefix (line , cwd + string (filepath .Separator ))
263
- }
264
-
265
- func TestLinesChanged (t * testing.T ) {
254
+ func TestChecker_linesChanged (t * testing.T ) {
266
255
diff := []byte (`--- a/file.go
267
256
+++ b/file.go
268
257
@@ -1,1 +1,1 @@
@@ -297,3 +286,14 @@ func TestLinesChanged(t *testing.T) {
297
286
t .Errorf ("unexpected pos:\n have: %#v\n want: %#v" , have , want )
298
287
}
299
288
}
289
+
290
+ func rewriteAbs (t * testing.T , line string ) string {
291
+ t .Helper ()
292
+
293
+ cwd , err := os .Getwd ()
294
+ if err != nil {
295
+ t .Fatal (err )
296
+ }
297
+
298
+ return strings .TrimPrefix (line , cwd + string (filepath .Separator ))
299
+ }
0 commit comments