filter: fix bug, copy slice with new pattern

This commit is contained in:
Alexander Neumann 2015-07-13 23:15:57 +02:00
parent e9285539be
commit b425ea19e5
2 changed files with 23 additions and 2 deletions

View File

@ -45,7 +45,8 @@ func match(patterns, strs []string) (matched bool, err error) {
if ok, pos := hasDoubleWildcard(patterns); ok {
// gradually expand '**' into separate wildcards
for i := 0; i <= len(strs)-len(patterns)+1; i++ {
newPat := patterns[:pos]
newPat := make([]string, pos)
copy(newPat, patterns[:pos])
for k := 0; k < i; k++ {
newPat = append(newPat, "*")
}

View File

@ -69,6 +69,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},
{"user/**/important*", "/home/user/work/x/y/hidden/x", false},
{"user/**/hidden*/**/c", "/home/user/work/x/y/hidden/z/a/b/c", true},
}
func TestMatch(t *testing.T) {
@ -268,7 +270,7 @@ var filterTests = []struct {
[]string{"accounting.*", "*Partner*"},
[]string{"*.docx", "*.xlsx"},
[]test{
// {"/home/user/foo/test.c", true},
{"/home/user/foo/test.c", true},
{"/home/user/Partner/test.docx", true},
{"/home/user/bar/test.docx", false},
{"/home/user/test.xlsx", false},
@ -279,6 +281,24 @@ var filterTests = []struct {
{"/users/A/Calculation Partner.xlsx", true},
},
},
{
[]string{"accounting.*", "*Partner*", "user/**/important*"},
[]string{"*.docx", "*.xlsx", "user/**/*hidden*"},
[]test{
{"/home/user/foo/test.c", true},
{"/home/user/Partner/test.docx", true},
{"/home/user/bar/test.docx", false},
{"/home/user/test.xlsx", false},
{"/home/foo/test.doc", true},
{"/x", true},
{"main.go", true},
{"/users/A/accounting.xlsx", true},
{"/users/A/Calculation Partner.xlsx", true},
{"/home/user/work/shared/test/important Calculations/help.txt", true},
{"/home/user/work/shared/test/important.xlsx", true},
{"/home/user/work/x/y/hidden/x", false},
},
},
}
func TestFilter(t *testing.T) {