Skip to content

Commit c77ca92

Browse files
committed
passes/commentmap: use txtar for testdata
1 parent 81e1806 commit c77ca92

File tree

6 files changed

+150
-36
lines changed

6 files changed

+150
-36
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ go 1.15
44

55
require (
66
github.com/google/go-cmp v0.5.4
7+
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4
78
golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d
89
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
12
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
23
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4+
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4 h1:d2/eIbH9XjD1fFwD5SHv8x168fjbQ9PB8hvs8DSEC08=
5+
github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4/go.mod h1:D+FIZ+7OahH3ePw/izIEeH5I06eKs1IKI4Xr64/Am3M=
6+
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
7+
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
8+
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
9+
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
10+
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
11+
github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI=
12+
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
13+
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
14+
github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc=
15+
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
16+
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=
17+
github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0=
18+
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag=
19+
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
320
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
421
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
522
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -17,6 +34,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
1734
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
1835
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1936
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
37+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
38+
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
2039
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
2140
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2241
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,105 @@
11
package commentmap_test
22

33
import (
4+
"fmt"
45
"go/ast"
6+
"os"
7+
"path/filepath"
8+
"strings"
59
"testing"
610

711
"golang.org/x/tools/go/analysis"
812
"golang.org/x/tools/go/analysis/analysistest"
913
"golang.org/x/tools/go/analysis/passes/inspect"
1014
"golang.org/x/tools/go/ast/inspector"
15+
"golang.org/x/tools/txtar"
1116

1217
"github.com/gostaticanalysis/comment"
1318
"github.com/gostaticanalysis/comment/passes/commentmap"
19+
"github.com/gostaticanalysis/testutil"
1420
)
1521

1622
func Test_Maps_Ignore(t *testing.T) {
17-
testdata := analysistest.TestData()
18-
analyzer := &analysis.Analyzer{
19-
Requires: []*analysis.Analyzer{
20-
inspect.Analyzer,
21-
commentmap.Analyzer,
23+
t.Parallel()
24+
25+
tests := map[string]struct {
26+
path string
27+
found bool
28+
}{
29+
"ignore": {
30+
path: "ignore",
31+
found: true,
2232
},
23-
Run: func(pass *analysis.Pass) (interface{}, error) {
24-
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
25-
cmaps := pass.ResultOf[commentmap.Analyzer].(comment.Maps)
26-
inspect.Preorder(nil, func(n ast.Node) {
27-
if cmaps.Ignore(n, "check") {
28-
pass.Reportf(n.Pos(), "ignore")
29-
}
30-
})
31-
return nil, nil
33+
"notignore": {
34+
path: "notignore",
35+
found: false,
3236
},
3337
}
34-
analysistest.Run(t, testdata, analyzer, "ignore")
38+
for name, tt := range tests {
39+
name := name
40+
tt := tt
41+
t.Run(name, func(t *testing.T) {
42+
t.Parallel()
43+
44+
testdata := parseTestdata(t, tt.path)
45+
analyzer := &analysis.Analyzer{
46+
Requires: []*analysis.Analyzer{
47+
inspect.Analyzer,
48+
commentmap.Analyzer,
49+
},
50+
Run: func(pass *analysis.Pass) (interface{}, error) {
51+
var found bool
52+
53+
ins := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
54+
cmaps := pass.ResultOf[commentmap.Analyzer].(comment.Maps)
55+
ins.Preorder(nil, func(n ast.Node) {
56+
if cmaps.Ignore(n, "check") {
57+
found = true
58+
}
59+
})
60+
61+
if found != tt.found {
62+
return nil, fmt.Errorf("%q not found", name)
63+
}
64+
65+
return nil, nil
66+
},
67+
}
68+
69+
analysistest.Run(t, testdata, analyzer, tt.path)
70+
})
71+
}
72+
}
73+
74+
func parseTestdata(t *testing.T, name string) string {
75+
t.Helper()
76+
77+
filemap := make(map[string]string)
78+
walkFn := func(path string, info os.FileInfo, err error) error {
79+
if err != nil {
80+
return err
81+
}
82+
if info.IsDir() {
83+
return nil
84+
}
85+
86+
if !strings.Contains(path, name) {
87+
return filepath.SkipDir
88+
}
89+
90+
ar, err := txtar.ParseFile(path)
91+
if err != nil {
92+
return err
93+
}
94+
for _, file := range ar.Files {
95+
filemap[filepath.Join(name, file.Name)] = string(file.Data)
96+
}
97+
98+
return nil
99+
}
100+
if err := filepath.Walk(analysistest.TestData(), walkFn); err != nil {
101+
t.Fatal(err)
102+
}
103+
104+
return testutil.WriteFiles(t, filemap)
35105
}

passes/commentmap/testdata/src/ignore/a.go

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- a.go --
2+
package ignore
3+
4+
func _() {
5+
//lint:ignore check test
6+
var _ = ""
7+
}
8+
9+
-- b.go --
10+
package ignore
11+
12+
func _() {
13+
//lint:ignore check1,check,check2 test
14+
var _ = ""
15+
}
16+
17+
-- c.go --
18+
package ignore
19+
20+
func _() {
21+
// lint:ignore check test
22+
var _ = ""
23+
}
24+
25+
-- d.go --
26+
package ignore
27+
28+
func _() {
29+
//lint:ignore check multiple words in reason
30+
var _ = ""
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- a.go --
2+
package ignore
3+
4+
func _() {
5+
//lint:ignore check1 test
6+
var _ = ""
7+
}
8+
-- b.go --
9+
package ignore
10+
11+
func _() {
12+
//lint:ignore check1
13+
var _ = ""
14+
}

0 commit comments

Comments
 (0)