-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathfilefilter_test.go
125 lines (115 loc) · 2.82 KB
/
filefilter_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
package lint_test
import (
"testing"
"github.com/mgechev/revive/lint"
)
func TestFileFilter(t *testing.T) {
t.Run("whole file name", func(t *testing.T) {
ff, err := lint.ParseFileFilter("a/b/c.go")
if err != nil {
t.Fatal(err)
}
if !ff.MatchFileName("a/b/c.go") {
t.Fatal("should match a/b/c.go")
}
if ff.MatchFileName("a/b/d.go") {
t.Fatal("should not match")
}
})
t.Run("regex", func(t *testing.T) {
ff, err := lint.ParseFileFilter("~b/[cd].go$")
if err != nil {
t.Fatal(err)
}
if !ff.MatchFileName("a/b/c.go") {
t.Fatal("should match a/b/c.go")
}
if !ff.MatchFileName("b/d.go") {
t.Fatal("should match b/d.go")
}
if ff.MatchFileName("b/x.go") {
t.Fatal("should not match b/x.go")
}
})
t.Run("TEST well-known", func(t *testing.T) {
ff, err := lint.ParseFileFilter("TEST")
if err != nil {
t.Fatal(err)
}
if !ff.MatchFileName("a/b/c_test.go") {
t.Fatal("should match a/b/c_test.go")
}
if ff.MatchFileName("a/b/c_test_no.go") {
t.Fatal("should not match a/b/c_test_no.go")
}
})
t.Run("glob *", func(t *testing.T) {
ff, err := lint.ParseFileFilter("a/b/*.pb.go")
if err != nil {
t.Fatal(err)
}
if !ff.MatchFileName("a/b/xxx.pb.go") {
t.Fatal("should match a/b/xxx.pb.go")
}
if !ff.MatchFileName("a/b/yyy.pb.go") {
t.Fatal("should match a/b/yyy.pb.go")
}
if ff.MatchFileName("a/b/xxx.nopb.go") {
t.Fatal("should not match a/b/xxx.nopb.go")
}
})
t.Run("glob **", func(t *testing.T) {
ff, err := lint.ParseFileFilter("a/**/*.pb.go")
if err != nil {
t.Fatal(err)
}
if !ff.MatchFileName("a/x/xxx.pb.go") {
t.Fatal("should match a/x/xxx.pb.go")
}
if !ff.MatchFileName("a/xxx.pb.go") {
t.Fatal("should match a/xxx.pb.go")
}
if !ff.MatchFileName("a/x/y/z/yyy.pb.go") {
t.Fatal("should match a/x/y/z/yyy.pb.go")
}
if ff.MatchFileName("a/b/xxx.nopb.go") {
t.Fatal("should not match a/b/xxx.nopb.go")
}
})
t.Run("empty", func(t *testing.T) {
ff, err := lint.ParseFileFilter("")
if err != nil {
t.Fatal(err)
}
fileNames := []string{"pb.go", "a/pb.go", "a/x/xxx.pb.go", "a/x/xxx.pb_test.go"}
for _, fn := range fileNames {
if ff.MatchFileName(fn) {
t.Fatalf("should not match %s", fn)
}
}
})
t.Run("just *", func(t *testing.T) {
ff, err := lint.ParseFileFilter("*")
if err != nil {
t.Fatal(err)
}
fileNames := []string{"pb.go", "a/pb.go", "a/x/xxx.pb.go", "a/x/xxx.pb_test.go"}
for _, fn := range fileNames {
if !ff.MatchFileName(fn) {
t.Fatalf("should match %s", fn)
}
}
})
t.Run("just ~", func(t *testing.T) {
ff, err := lint.ParseFileFilter("~")
if err != nil {
t.Fatal(err)
}
fileNames := []string{"pb.go", "a/pb.go", "a/x/xxx.pb.go", "a/x/xxx.pb_test.go"}
for _, fn := range fileNames {
if !ff.MatchFileName(fn) {
t.Fatalf("should match %s", fn)
}
}
})
}