Skip to content

Commit ebce0fc

Browse files
committed
Tests for filters
1 parent 8e7df42 commit ebce0fc

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ require (
88
github.com/fatih/color v1.18.0
99
github.com/go-git/go-billy/v5 v5.6.2
1010
github.com/go-git/go-git/v5 v5.13.2
11+
github.com/gobwas/glob v0.2.3
1112
github.com/kelseyhightower/envconfig v1.4.0
1213
github.com/mitchellh/go-homedir v1.1.0
1314
github.com/openshift-knative/hack v0.0.0-20250530124021-bed35e443a23
1415
github.com/spf13/cobra v1.9.1
16+
github.com/stretchr/testify v1.10.0
1517
github.com/wavesoftware/go-commandline v1.3.0
1618
github.com/xanzy/ssh-agent v0.3.3
1719
gotest.tools/v3 v3.5.2
@@ -236,7 +238,6 @@ require (
236238
github.com/spf13/viper v1.18.2 // indirect
237239
github.com/stoewer/go-strcase v1.3.0 // indirect
238240
github.com/stretchr/objx v0.5.2 // indirect
239-
github.com/stretchr/testify v1.10.0 // indirect
240241
github.com/subosito/gotenv v1.6.0 // indirect
241242
github.com/tektoncd/pipeline v0.61.0 // indirect
242243
github.com/theupdateframework/go-tuf v0.7.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
390390
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
391391
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
392392
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
393+
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
394+
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
393395
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
394396
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
395397
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

pkg/files/filters.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
package files
22

3-
import "path"
3+
import "github.com/gobwas/glob"
44

55
// Filters represents what files to include, and which to exclude from copying operations.
66
type Filters struct {
77
Include []string `json:"include"`
88
Exclude []string `json:"exclude"`
99
}
1010

11-
func (f Filters) Matches(pth string) bool {
11+
func (f Filters) Matcher() Matcher {
12+
m := Matcher{
13+
Include: make([]glob.Glob, 0, len(f.Include)),
14+
Exclude: make([]glob.Glob, 0, len(f.Exclude)),
15+
}
16+
separators := []rune{'/'}
17+
for _, p := range f.Include {
18+
g := glob.MustCompile(p, separators...)
19+
m.Include = append(m.Include, g)
20+
}
21+
for _, p := range f.Exclude {
22+
g := glob.MustCompile(p, separators...)
23+
m.Exclude = append(m.Exclude, g)
24+
}
25+
return m
26+
}
27+
28+
type Matcher struct {
29+
Include []glob.Glob
30+
Exclude []glob.Glob
31+
}
32+
33+
func (m Matcher) Matches(pth string) bool {
1234
result := false
13-
for _, pattern := range f.Include {
14-
matched, _ := path.Match(pattern, pth)
15-
if matched {
35+
for _, pattern := range m.Include {
36+
if pattern.Match(pth) {
1637
result = true
1738
}
1839
}
1940
if !result {
2041
return false
2142
}
22-
for _, pattern := range f.Exclude {
23-
matched, _ := path.Match(pattern, pth)
24-
if matched {
43+
for _, pattern := range m.Exclude {
44+
if pattern.Match(pth) {
2545
return false
2646
}
2747
}

pkg/files/filters_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package files_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/openshift-knative/deviate/pkg/files"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestFilters_Match(t *testing.T) {
11+
filelist := []string{
12+
"a/b/c.txt",
13+
"a/d.txt",
14+
"a/b/d.md",
15+
"a/b/d.txt",
16+
"a.txt",
17+
"b.md",
18+
}
19+
tcs := []testFiltersMatchCases{{
20+
name: "all md files",
21+
Filters: files.Filters{
22+
Include: []string{"**/*.md", "*.md"},
23+
},
24+
want: []string{
25+
"a/b/d.md",
26+
"b.md",
27+
},
28+
}, {
29+
name: "root level md files",
30+
Filters: files.Filters{
31+
Include: []string{"*.md"},
32+
},
33+
want: []string{
34+
"b.md",
35+
},
36+
}, {
37+
"just one txt",
38+
files.Filters{
39+
Include: []string{"**.txt"},
40+
Exclude: []string{"a/b**", "a.txt"},
41+
},
42+
[]string{
43+
"a/d.txt",
44+
},
45+
}}
46+
for _, tc := range tcs {
47+
t.Run(tc.name, func(t *testing.T) {
48+
matcher := tc.Matcher()
49+
got := make([]string, 0, len(tc.want))
50+
for _, f := range filelist {
51+
if matcher.Matches(f) {
52+
got = append(got, f)
53+
}
54+
}
55+
56+
assert.Equal(t, tc.want, got)
57+
})
58+
}
59+
}
60+
61+
type testFiltersMatchCases struct {
62+
name string
63+
files.Filters
64+
want []string
65+
}

pkg/git/checkout.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,22 @@ func (o onGoingCheckout) OntoWorkspace(filters files.Filters) error {
9999
if err != nil {
100100
return errors.Wrap(err, ErrLocalOperationFailed)
101101
}
102-
return o.applyTree(wt, "/", filters)
102+
matcher := filters.Matcher()
103+
return o.applyTree(wt, "/", matcher)
103104
}
104105

105-
func (o onGoingCheckout) applyTree(fs billy.Filesystem, dir string, filters files.Filters) error {
106+
func (o onGoingCheckout) applyTree(fs billy.Filesystem, dir string, matcher files.Matcher) error {
106107
infos, err := fs.ReadDir(dir)
107108
if err != nil {
108109
return errors.Wrap(err, ErrLocalOperationFailed)
109110
}
110111
for _, f := range infos {
111112
fp := path.Join(dir, f.Name())
112-
if !filters.Matches(fp) {
113+
if !matcher.Matches(fp) {
113114
continue
114115
}
115116
if f.IsDir() {
116-
err = o.applyTree(fs, fp, filters)
117+
err = o.applyTree(fs, fp, matcher)
117118
if err != nil {
118119
return err
119120
}

pkg/sync/remove_unwanted_upstream_files.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
func (o Operation) removeUnwantedUpstreamFiles() error {
1313
o.Println("- Remove unwanted upstream files")
14+
matcher := o.Config.DeleteFromUpstream.Matcher()
1415

1516
return errors.Wrap(filepath.WalkDir(o.State.Project.Path, func(pth string, _ fs.DirEntry, err error) error {
1617
if err != nil {
1718
return err
1819
}
19-
if o.Config.DeleteFromUpstream.Matches(pth) {
20+
if matcher.Matches(pth) {
2021
fp := path.Join(o.State.Project.Path, pth)
2122
return os.RemoveAll(fp)
2223
}

0 commit comments

Comments
 (0)