forked from TrueCloudLab/restic
Allow filter patterns/paths to be both '/' and os.PathSeparator
This converts filepath.Separator to '/', if it isn't already. This allows both native paths and patterns, as well as a mix of them. Added tests.
This commit is contained in:
parent
a3570af500
commit
21ab5a488d
2 changed files with 40 additions and 12 deletions
|
@ -25,8 +25,14 @@ func Match(pattern, str string) (matched bool, err error) {
|
||||||
return false, ErrBadString
|
return false, ErrBadString
|
||||||
}
|
}
|
||||||
|
|
||||||
patterns := strings.Split(pattern, string(filepath.Separator))
|
// convert file path separator to '/'
|
||||||
strs := strings.Split(str, string(filepath.Separator))
|
if filepath.Separator != '/' {
|
||||||
|
pattern = strings.Replace(pattern, string(filepath.Separator), "/", -1)
|
||||||
|
str = strings.Replace(str, string(filepath.Separator), "/", -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
patterns := strings.Split(pattern, "/")
|
||||||
|
strs := strings.Split(str, "/")
|
||||||
|
|
||||||
return match(patterns, strs)
|
return match(patterns, strs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"compress/bzip2"
|
"compress/bzip2"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/restic/filter"
|
"github.com/restic/restic/filter"
|
||||||
|
@ -71,20 +73,40 @@ var matchTests = []struct {
|
||||||
{"foo/**/bar", "/home/user/foo/x/y/bar/main.go", true},
|
{"foo/**/bar", "/home/user/foo/x/y/bar/main.go", 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", true},
|
||||||
|
{"c:/foo/*/bar/test.*", "c:/foo/bar/test.go", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
func testpattern(t *testing.T, pattern, path string, shouldMatch bool) {
|
||||||
|
match, err := filter.Match(pattern, path)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("test pattern %q failed: expected no error for path %q, but error returned: %v",
|
||||||
|
pattern, path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if match != shouldMatch {
|
||||||
|
t.Errorf("test: filter.Match(%q, %q): expected %v, got %v",
|
||||||
|
pattern, path, shouldMatch, match)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMatch(t *testing.T) {
|
func TestMatch(t *testing.T) {
|
||||||
for i, test := range matchTests {
|
for _, test := range matchTests {
|
||||||
match, err := filter.Match(test.pattern, test.path)
|
testpattern(t, test.pattern, test.path, test.match)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("test %d failed: expected no error for pattern %q, but error returned: %v",
|
|
||||||
i, test.pattern, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if match != test.match {
|
// Test with native path separator
|
||||||
t.Errorf("test %d: filter.Match(%q, %q): expected %v, got %v",
|
if filepath.Separator != '/' {
|
||||||
i, test.pattern, test.path, test.match, match)
|
// Test with pattern as native
|
||||||
|
pattern := strings.Replace(test.pattern, "/", string(filepath.Separator), -1)
|
||||||
|
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)
|
||||||
|
|
||||||
|
// Test with both pattern and path as native
|
||||||
|
testpattern(t, pattern, path, test.match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue