Browse Source

Fix fnmatch tests for Windows

Jakob Borg 11 years ago
parent
commit
04a5f9cb04
2 changed files with 22 additions and 13 deletions
  1. 6 4
      fnmatch/fnmatch.go
  2. 16 9
      fnmatch/fnmatch_test.go

+ 6 - 4
fnmatch/fnmatch.go

@@ -18,13 +18,15 @@ const (
 )
 
 func Convert(pattern string, flags int) (*regexp.Regexp, error) {
+	any := "."
+
 	if runtime.GOOS == "windows" {
 		flags |= FNM_NOESCAPE
 		pattern = filepath.FromSlash(pattern)
-	}
-
-	any := "."
-	if flags&FNM_PATHNAME != 0 {
+		if flags&FNM_PATHNAME != 0 {
+			any = "[^\\\\]"
+		}
+	} else if flags&FNM_PATHNAME != 0 {
 		any = "[^/]"
 	}
 	if flags&FNM_NOESCAPE != 0 {

+ 16 - 9
fnmatch/fnmatch_test.go

@@ -5,15 +5,19 @@
 package fnmatch
 
 import (
+	"path/filepath"
+	"runtime"
 	"testing"
 )
 
-var testCases = []struct {
+type testcase struct {
 	pat   string
 	name  string
 	flags int
 	match bool
-}{
+}
+
+var testcases = []testcase{
 	{"", "", 0, true},
 	{"*", "", 0, true},
 	{"*", "foo", 0, true},
@@ -24,9 +28,6 @@ var testCases = []struct {
 	{"*.*", "foo.txt", 0, true},
 	{"foo*.txt", "foobar.txt", 0, true},
 	{"foo.txt", "foo.txt", 0, true},
-	{"foo\\.txt", "foo.txt", 0, true},
-	{"foo\\*.txt", "foo*.txt", 0, true},
-	{"foo\\.txt", "foo.txt", FNM_NOESCAPE, false},
 
 	{"foo.txt", "bar/foo.txt", 0, false},
 	{"*/foo.txt", "bar/foo.txt", 0, true},
@@ -38,9 +39,7 @@ var testCases = []struct {
 	{"f[ab]o.txt", "fco.txt", 0, false},
 	{"f[ab]o.txt", "fabo.txt", 0, false},
 	{"f[ab]o.txt", "f[ab]o.txt", 0, false},
-	{"f\\[ab\\]o.txt", "f[ab]o.txt", 0, true},
 	{"f\\[ab\\]o.txt", "f[ab]o.txt", FNM_NOESCAPE, false},
-	{"f\\\\\\[ab\\\\\\]o.txt", "f\\[ab\\]o.txt", 0, true},
 
 	{"*foo.txt", "bar/foo.txt", 0, true},
 	{"*foo.txt", "bar/foo.txt", FNM_PATHNAME, false},
@@ -56,8 +55,16 @@ var testCases = []struct {
 }
 
 func TestMatch(t *testing.T) {
-	for _, tc := range testCases {
-		if m, err := Match(tc.pat, tc.name, tc.flags); m != tc.match {
+	if runtime.GOOS != "windows" {
+		testcases = append(testcases, testcase{"f\\[ab\\]o.txt", "f[ab]o.txt", 0, true})
+		testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", 0, true})
+		testcases = append(testcases, testcase{"foo\\*.txt", "foo*.txt", 0, true})
+		testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", FNM_NOESCAPE, false})
+		testcases = append(testcases, testcase{"f\\\\\\[ab\\\\\\]o.txt", "f\\[ab\\]o.txt", 0, true})
+	}
+
+	for _, tc := range testcases {
+		if m, err := Match(tc.pat, filepath.FromSlash(tc.name), tc.flags); m != tc.match {
 			if err != nil {
 				t.Error(err)
 			} else {