Skip to content

Commit 1a481fa

Browse files
author
David Lawrence
committed
adding support for arbitrary paths with ...
1 parent 3911321 commit 1a481fa

File tree

8 files changed

+490
-83
lines changed

8 files changed

+490
-83
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ _testmain.go
2626
*.prof
2727

2828
.DS_Store
29+
30+
.vscode

filelist.go

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,58 @@
1515
package main
1616

1717
import (
18-
"fmt"
19-
"os"
20-
"path/filepath"
18+
"log"
2119
"strings"
20+
21+
"github.com/ryanuber/go-glob"
2222
)
2323

24-
type filelist struct {
25-
paths map[string]bool
26-
globs []string
24+
// fileList uses a map for patterns to ensure each pattern only
25+
// appears once
26+
type fileList struct {
27+
patterns map[string]struct{}
2728
}
2829

29-
func newFileList(paths ...string) *filelist {
30-
31-
f := &filelist{
32-
make(map[string]bool),
33-
make([]string, 0),
30+
func newFileList(paths ...string) *fileList {
31+
f := &fileList{
32+
patterns: make(map[string]struct{}),
3433
}
35-
36-
for _, path := range paths {
37-
if e := f.Set(path); e != nil {
38-
// #nosec
39-
fmt.Fprintf(os.Stderr, "Unable to add %s to filelist: %s\n", path, e)
40-
}
34+
for _, p := range paths {
35+
f.patterns[p] = struct{}{}
4136
}
4237
return f
4338
}
4439

45-
func (f *filelist) String() string {
46-
return strings.Join(f.globs, ", ")
40+
func (f *fileList) String() string {
41+
ps := make([]string, 0, len(f.patterns))
42+
for p := range f.patterns {
43+
ps = append(ps, p)
44+
}
45+
return strings.Join(ps, ", ")
4746
}
4847

49-
func (f *filelist) Set(path string) error {
50-
f.globs = append(f.globs, path)
51-
matches, e := filepath.Glob(path)
52-
if e != nil {
53-
return e
54-
}
55-
for _, each := range matches {
56-
abs, e := filepath.Abs(each)
57-
if e != nil {
58-
return e
59-
}
60-
f.paths[abs] = true
48+
func (f *fileList) Set(path string) error {
49+
if path == "" {
50+
// don't bother adding the empty path
51+
return nil
6152
}
53+
f.patterns[path] = struct{}{}
6254
return nil
6355
}
6456

65-
func (f filelist) Contains(path string) bool {
66-
_, present := f.paths[path]
67-
return present
57+
func (f fileList) Contains(path string) bool {
58+
for p := range f.patterns {
59+
if glob.Glob(p, path) {
60+
log.Printf("excluding: %s\n", path)
61+
return true
62+
}
63+
}
64+
log.Printf("including: %s\n", path)
65+
return false
6866
}
6967

7068
/*
71-
func (f filelist) Dump() {
69+
func (f fileList) Dump() {
7270
for k, _ := range f.paths {
7371
println(k)
7472
}

filelist_test.go

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_newFileList(t *testing.T) {
9+
type args struct {
10+
paths []string
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want *fileList
16+
}{
17+
{
18+
name: "nil paths",
19+
args: args{paths: nil},
20+
want: &fileList{patterns: map[string]struct{}{}},
21+
},
22+
{
23+
name: "empty paths",
24+
args: args{paths: []string{}},
25+
want: &fileList{patterns: map[string]struct{}{}},
26+
},
27+
{
28+
name: "have paths",
29+
args: args{paths: []string{"*_test.go"}},
30+
want: &fileList{patterns: map[string]struct{}{
31+
"*_test.go": struct{}{},
32+
}},
33+
},
34+
}
35+
for _, tt := range tests {
36+
if got := newFileList(tt.args.paths...); !reflect.DeepEqual(got, tt.want) {
37+
t.Errorf("%q. newFileList() = %v, want %v", tt.name, got, tt.want)
38+
}
39+
}
40+
}
41+
42+
func Test_fileList_String(t *testing.T) {
43+
type fields struct {
44+
patterns []string
45+
}
46+
tests := []struct {
47+
name string
48+
fields fields
49+
want string
50+
}{
51+
{
52+
name: "nil patterns",
53+
fields: fields{patterns: nil},
54+
want: "",
55+
},
56+
{
57+
name: "empty patterns",
58+
fields: fields{patterns: []string{}},
59+
want: "",
60+
},
61+
{
62+
name: "one pattern",
63+
fields: fields{patterns: []string{"foo"}},
64+
want: "foo",
65+
},
66+
{
67+
name: "two patterns",
68+
fields: fields{patterns: []string{"foo", "bar"}},
69+
want: "foo, bar",
70+
},
71+
}
72+
for _, tt := range tests {
73+
f := newFileList(tt.fields.patterns...)
74+
if got := f.String(); got != tt.want {
75+
t.Errorf("%q. fileList.String() = %v, want %v", tt.name, got, tt.want)
76+
}
77+
}
78+
}
79+
80+
func Test_fileList_Set(t *testing.T) {
81+
type fields struct {
82+
patterns []string
83+
}
84+
type args struct {
85+
path string
86+
}
87+
tests := []struct {
88+
name string
89+
fields fields
90+
args args
91+
want map[string]struct{}
92+
wantErr bool
93+
}{
94+
{
95+
name: "add empty path",
96+
fields: fields{patterns: nil},
97+
args: args{path: ""},
98+
want: map[string]struct{}{},
99+
wantErr: false,
100+
},
101+
{
102+
name: "add path to nil patterns",
103+
fields: fields{patterns: nil},
104+
args: args{path: "foo"},
105+
want: map[string]struct{}{
106+
"foo": struct{}{},
107+
},
108+
wantErr: false,
109+
},
110+
{
111+
name: "add path to empty patterns",
112+
fields: fields{patterns: []string{}},
113+
args: args{path: "foo"},
114+
want: map[string]struct{}{
115+
"foo": struct{}{},
116+
},
117+
wantErr: false,
118+
},
119+
{
120+
name: "add path to populated patterns",
121+
fields: fields{patterns: []string{"foo"}},
122+
args: args{path: "bar"},
123+
want: map[string]struct{}{
124+
"foo": struct{}{},
125+
"bar": struct{}{},
126+
},
127+
wantErr: false,
128+
},
129+
}
130+
for _, tt := range tests {
131+
f := newFileList(tt.fields.patterns...)
132+
if err := f.Set(tt.args.path); (err != nil) != tt.wantErr {
133+
t.Errorf("%q. fileList.Set() error = %v, wantErr %v", tt.name, err, tt.wantErr)
134+
}
135+
if !reflect.DeepEqual(f.patterns, tt.want) {
136+
t.Errorf("%q. got state fileList.patterns = %v, want state %v", tt.name, f.patterns, tt.want)
137+
}
138+
}
139+
}
140+
141+
func Test_fileList_Contains(t *testing.T) {
142+
type fields struct {
143+
patterns []string
144+
}
145+
type args struct {
146+
path string
147+
}
148+
tests := []struct {
149+
name string
150+
fields fields
151+
args args
152+
want bool
153+
}{
154+
{
155+
name: "nil patterns",
156+
fields: fields{patterns: nil},
157+
args: args{path: "foo"},
158+
want: false,
159+
},
160+
{
161+
name: "empty patterns",
162+
fields: fields{patterns: nil},
163+
args: args{path: "foo"},
164+
want: false,
165+
},
166+
{
167+
name: "one pattern, no wildcard, no match",
168+
fields: fields{patterns: []string{"foo"}},
169+
args: args{path: "bar"},
170+
want: false,
171+
},
172+
{
173+
name: "one pattern, no wildcard, match",
174+
fields: fields{patterns: []string{"foo"}},
175+
args: args{path: "foo"},
176+
want: true,
177+
},
178+
{
179+
name: "one pattern, wildcard prefix, match",
180+
fields: fields{patterns: []string{"*foo"}},
181+
args: args{path: "foo"},
182+
want: true,
183+
},
184+
{
185+
name: "one pattern, wildcard suffix, match",
186+
fields: fields{patterns: []string{"foo*"}},
187+
args: args{path: "foo"},
188+
want: true,
189+
},
190+
{
191+
name: "one pattern, wildcard both ends, match",
192+
fields: fields{patterns: []string{"*foo*"}},
193+
args: args{path: "foo"},
194+
want: true,
195+
},
196+
{
197+
name: "default test match 1",
198+
fields: fields{patterns: []string{"*_test.go"}},
199+
args: args{path: "foo_test.go"},
200+
want: true,
201+
},
202+
{
203+
name: "default test match 2",
204+
fields: fields{patterns: []string{"*_test.go"}},
205+
args: args{path: "bar/foo_test.go"},
206+
want: true,
207+
},
208+
{
209+
name: "default test match 3",
210+
fields: fields{patterns: []string{"*_test.go"}},
211+
args: args{path: "/bar/foo_test.go"},
212+
want: true,
213+
},
214+
{
215+
name: "default test match 4",
216+
fields: fields{patterns: []string{"*_test.go"}},
217+
args: args{path: "baz/bar/foo_test.go"},
218+
want: true,
219+
},
220+
{
221+
name: "default test match 5",
222+
fields: fields{patterns: []string{"*_test.go"}},
223+
args: args{path: "/baz/bar/foo_test.go"},
224+
want: true,
225+
},
226+
{
227+
name: "many patterns, no match",
228+
fields: fields{patterns: []string{"*_one.go", "*_two.go"}},
229+
args: args{path: "/baz/bar/foo_test.go"},
230+
want: false,
231+
},
232+
{
233+
name: "many patterns, match",
234+
fields: fields{patterns: []string{"*_one.go", "*_two.go", "*_test.go"}},
235+
args: args{path: "/baz/bar/foo_test.go"},
236+
want: true,
237+
},
238+
}
239+
for _, tt := range tests {
240+
f := newFileList(tt.fields.patterns...)
241+
if got := f.Contains(tt.args.path); got != tt.want {
242+
t.Errorf("%q. fileList.Contains() = %v, want %v", tt.name, got, tt.want)
243+
}
244+
}
245+
}

0 commit comments

Comments
 (0)