From 14aead94b390c31aeb53cccc8be3c493451c180a Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 9 Jun 2018 23:12:51 +0200 Subject: [PATCH] filter: Allow double wildcard in ChildMatch --- internal/filter/filter.go | 6 ++++ internal/filter/filter_test.go | 56 +++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/internal/filter/filter.go b/internal/filter/filter.go index 0cc9512d4..74deddb03 100644 --- a/internal/filter/filter.go +++ b/internal/filter/filter.go @@ -83,6 +83,12 @@ func childMatch(patterns, strs []string) (matched bool, err error) { 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 l := 0 if len(strs) > len(patterns) { diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index bd2070e56..97df452fb 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -83,6 +83,8 @@ var matchTests = []struct { {"foo/**/bar/*.go", "bar/main.go", false}, {"foo/**/bar", "/home/user/foo/x/y/bar", 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/**/hidden*/**/c", "/home/user/work/x/y/hidden/z/a/b/c", true}, {"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) { 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 if filepath.Separator != '/' { - // Test with pattern as native 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) - 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 - testpattern(t, pattern, path, test.match) + t.Run("both-native", func(t *testing.T) { + // 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/blah", 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}, {"/foo", "/foo/bar", true}, {"/*", "/foo", true}, @@ -179,20 +199,28 @@ func testchildpattern(t *testing.T, pattern, path string, shouldMatch bool) { func TestChildMatch(t *testing.T) { 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 if filepath.Separator != '/' { - // Test with pattern as native 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) - 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 - testchildpattern(t, pattern, path, test.match) + t.Run("both-native", func(t *testing.T) { + // Test with both pattern and path as native + testchildpattern(t, pattern, path, test.match) + }) } } }