Fix fnmatch tests for Windows
This commit is contained in:
parent
9818e2b550
commit
04a5f9cb04
|
@ -18,13 +18,15 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Convert(pattern string, flags int) (*regexp.Regexp, error) {
|
func Convert(pattern string, flags int) (*regexp.Regexp, error) {
|
||||||
|
any := "."
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
flags |= FNM_NOESCAPE
|
flags |= FNM_NOESCAPE
|
||||||
pattern = filepath.FromSlash(pattern)
|
pattern = filepath.FromSlash(pattern)
|
||||||
}
|
if flags&FNM_PATHNAME != 0 {
|
||||||
|
any = "[^\\\\]"
|
||||||
any := "."
|
}
|
||||||
if flags&FNM_PATHNAME != 0 {
|
} else if flags&FNM_PATHNAME != 0 {
|
||||||
any = "[^/]"
|
any = "[^/]"
|
||||||
}
|
}
|
||||||
if flags&FNM_NOESCAPE != 0 {
|
if flags&FNM_NOESCAPE != 0 {
|
||||||
|
|
|
@ -5,15 +5,19 @@
|
||||||
package fnmatch
|
package fnmatch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testCases = []struct {
|
type testcase struct {
|
||||||
pat string
|
pat string
|
||||||
name string
|
name string
|
||||||
flags int
|
flags int
|
||||||
match bool
|
match bool
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
var testcases = []testcase{
|
||||||
{"", "", 0, true},
|
{"", "", 0, true},
|
||||||
{"*", "", 0, true},
|
{"*", "", 0, true},
|
||||||
{"*", "foo", 0, true},
|
{"*", "foo", 0, true},
|
||||||
|
@ -24,9 +28,6 @@ var testCases = []struct {
|
||||||
{"*.*", "foo.txt", 0, true},
|
{"*.*", "foo.txt", 0, true},
|
||||||
{"foo*.txt", "foobar.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", 0, true},
|
|
||||||
{"foo\\.txt", "foo.txt", FNM_NOESCAPE, false},
|
|
||||||
|
|
||||||
{"foo.txt", "bar/foo.txt", 0, false},
|
{"foo.txt", "bar/foo.txt", 0, false},
|
||||||
{"*/foo.txt", "bar/foo.txt", 0, true},
|
{"*/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", "fco.txt", 0, false},
|
||||||
{"f[ab]o.txt", "fabo.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, 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", 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", 0, true},
|
||||||
{"*foo.txt", "bar/foo.txt", FNM_PATHNAME, false},
|
{"*foo.txt", "bar/foo.txt", FNM_PATHNAME, false},
|
||||||
|
@ -56,8 +55,16 @@ var testCases = []struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMatch(t *testing.T) {
|
func TestMatch(t *testing.T) {
|
||||||
for _, tc := range testCases {
|
if runtime.GOOS != "windows" {
|
||||||
if m, err := Match(tc.pat, tc.name, tc.flags); m != tc.match {
|
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 {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue