filter: Allow double wildcard in ChildMatch

This commit is contained in:
Alexander Neumann 2018-06-09 23:12:51 +02:00
parent ce01ca30d6
commit 14aead94b3
2 changed files with 48 additions and 14 deletions

View File

@ -83,6 +83,12 @@ func childMatch(patterns, strs []string) (matched bool, err error) {
return true, nil return true, nil
} }
ok, pos := hasDoubleWildcard(patterns)
if ok && len(strs) >= pos {
// cut off at the double wildcard
strs = strs[:pos]
}
// match path against absolute pattern prefix // match path against absolute pattern prefix
l := 0 l := 0
if len(strs) > len(patterns) { if len(strs) > len(patterns) {

View File

@ -83,6 +83,8 @@ var matchTests = []struct {
{"foo/**/bar/*.go", "bar/main.go", false}, {"foo/**/bar/*.go", "bar/main.go", false},
{"foo/**/bar", "/home/user/foo/x/y/bar", true}, {"foo/**/bar", "/home/user/foo/x/y/bar", true},
{"foo/**/bar", "/home/user/foo/x/y/bar/main.go", true}, {"foo/**/bar", "/home/user/foo/x/y/bar/main.go", true},
{"foo/**/bar/**/x", "/home/user/foo/bar/x", true},
{"foo/**/bar/**/x", "/home/user/foo/blaaa/blaz/bar/shared/work/x", true},
{"user/**/important*", "/home/user/work/x/y/hidden/x", false}, {"user/**/important*", "/home/user/work/x/y/hidden/x", false},
{"user/**/hidden*/**/c", "/home/user/work/x/y/hidden/z/a/b/c", true}, {"user/**/hidden*/**/c", "/home/user/work/x/y/hidden/z/a/b/c", true},
{"c:/foo/*test.*", "c:/foo/bar/test.go", false}, {"c:/foo/*test.*", "c:/foo/bar/test.go", false},
@ -107,20 +109,28 @@ func testpattern(t *testing.T, pattern, path string, shouldMatch bool) {
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {
for _, test := range matchTests { for _, test := range matchTests {
testpattern(t, test.pattern, test.path, test.match) t.Run("", func(t *testing.T) {
testpattern(t, test.pattern, test.path, test.match)
})
// Test with native path separator // Test with native path separator
if filepath.Separator != '/' { if filepath.Separator != '/' {
// Test with pattern as native
pattern := strings.Replace(test.pattern, "/", string(filepath.Separator), -1) pattern := strings.Replace(test.pattern, "/", string(filepath.Separator), -1)
testpattern(t, pattern, test.path, test.match) // Test with pattern as native
t.Run("pattern-native", func(t *testing.T) {
testpattern(t, pattern, test.path, test.match)
})
// Test with path as native
path := strings.Replace(test.path, "/", string(filepath.Separator), -1) path := strings.Replace(test.path, "/", string(filepath.Separator), -1)
testpattern(t, test.pattern, path, test.match) t.Run("path-native", func(t *testing.T) {
// Test with path as native
testpattern(t, test.pattern, path, test.match)
})
// Test with both pattern and path as native t.Run("both-native", func(t *testing.T) {
testpattern(t, pattern, path, test.match) // Test with both pattern and path as native
testpattern(t, pattern, path, test.match)
})
} }
} }
} }
@ -147,6 +157,16 @@ var childMatchTests = []struct {
{"/foo/**/baz", "/foo/bar/baz", true}, {"/foo/**/baz", "/foo/bar/baz", true},
{"/foo/**/baz", "/foo/bar/baz/blah", true}, {"/foo/**/baz", "/foo/bar/baz/blah", true},
{"/foo/**/qux", "/foo/bar/baz/qux", true}, {"/foo/**/qux", "/foo/bar/baz/qux", true},
{"/foo/**/qux", "/foo/bar/baz", true},
{"/foo/**/qux", "/foo/bar/baz/boo", true},
{"/foo/**", "/foo/bar/baz", true},
{"/foo/**", "/foo/bar", true},
{"foo/**/bar/**/x", "/home/user/foo", true},
{"foo/**/bar/**/x", "/home/user/foo/bar", true},
{"foo/**/bar/**/x", "/home/user/foo/blaaa/blaz/bar/shared/work/x", true},
{"/foo/*/qux", "/foo/bar", true},
{"/foo/*/qux", "/foo/bar/boo", false},
{"/foo/*/qux", "/foo/bar/boo/xx", false},
{"/baz/bar", "/foo", false}, {"/baz/bar", "/foo", false},
{"/foo", "/foo/bar", true}, {"/foo", "/foo/bar", true},
{"/*", "/foo", true}, {"/*", "/foo", true},
@ -179,20 +199,28 @@ func testchildpattern(t *testing.T, pattern, path string, shouldMatch bool) {
func TestChildMatch(t *testing.T) { func TestChildMatch(t *testing.T) {
for _, test := range childMatchTests { for _, test := range childMatchTests {
testchildpattern(t, test.pattern, test.path, test.match) t.Run("", func(t *testing.T) {
testchildpattern(t, test.pattern, test.path, test.match)
})
// Test with native path separator // Test with native path separator
if filepath.Separator != '/' { if filepath.Separator != '/' {
// Test with pattern as native
pattern := strings.Replace(test.pattern, "/", string(filepath.Separator), -1) pattern := strings.Replace(test.pattern, "/", string(filepath.Separator), -1)
testchildpattern(t, pattern, test.path, test.match) // Test with pattern as native
t.Run("pattern-native", func(t *testing.T) {
testchildpattern(t, pattern, test.path, test.match)
})
// Test with path as native
path := strings.Replace(test.path, "/", string(filepath.Separator), -1) path := strings.Replace(test.path, "/", string(filepath.Separator), -1)
testchildpattern(t, test.pattern, path, test.match) t.Run("path-native", func(t *testing.T) {
// Test with path as native
testchildpattern(t, test.pattern, path, test.match)
})
// Test with both pattern and path as native t.Run("both-native", func(t *testing.T) {
testchildpattern(t, pattern, path, test.match) // Test with both pattern and path as native
testchildpattern(t, pattern, path, test.match)
})
} }
} }
} }