From 21ab5a488db5d4ed162d4d821d65efc9e05b7852 Mon Sep 17 00:00:00 2001 From: klauspost Date: Mon, 17 Aug 2015 11:48:24 +0200 Subject: [PATCH] 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. --- filter/filter.go | 10 ++++++++-- filter/filter_test.go | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/filter/filter.go b/filter/filter.go index f8c335e34..4092b4025 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -25,8 +25,14 @@ func Match(pattern, str string) (matched bool, err error) { return false, ErrBadString } - patterns := strings.Split(pattern, string(filepath.Separator)) - strs := strings.Split(str, string(filepath.Separator)) + // convert file path separator to '/' + 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) } diff --git a/filter/filter_test.go b/filter/filter_test.go index 78e731b68..15892e910 100644 --- a/filter/filter_test.go +++ b/filter/filter_test.go @@ -5,6 +5,8 @@ import ( "compress/bzip2" "fmt" "os" + "path/filepath" + "strings" "testing" "github.com/restic/restic/filter" @@ -71,20 +73,40 @@ var matchTests = []struct { {"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}, + {"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) { - for i, test := range matchTests { - match, err := filter.Match(test.pattern, test.path) - if err != nil { - t.Errorf("test %d failed: expected no error for pattern %q, but error returned: %v", - i, test.pattern, err) - continue - } + for _, test := range matchTests { + testpattern(t, test.pattern, test.path, test.match) - if match != test.match { - t.Errorf("test %d: filter.Match(%q, %q): expected %v, got %v", - i, test.pattern, test.path, test.match, 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 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) } } }