2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-08-26 01:11:25 -07:00
|
|
|
|
|
|
|
package fnmatch
|
|
|
|
|
|
|
|
import (
|
2014-08-26 04:07:59 -07:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2014-08-26 01:11:25 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-26 04:07:59 -07:00
|
|
|
type testcase struct {
|
2014-08-26 01:11:25 -07:00
|
|
|
pat string
|
|
|
|
name string
|
|
|
|
flags int
|
|
|
|
match bool
|
2014-08-26 04:07:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var testcases = []testcase{
|
2014-08-26 01:11:25 -07:00
|
|
|
{"", "", 0, true},
|
|
|
|
{"*", "", 0, true},
|
|
|
|
{"*", "foo", 0, true},
|
|
|
|
{"*", "bar", 0, true},
|
|
|
|
{"*", "*", 0, true},
|
|
|
|
{"**", "f", 0, true},
|
|
|
|
{"**", "foo.txt", 0, true},
|
|
|
|
{"*.*", "foo.txt", 0, true},
|
|
|
|
{"foo*.txt", "foobar.txt", 0, true},
|
|
|
|
{"foo.txt", "foo.txt", 0, true},
|
|
|
|
|
|
|
|
{"foo.txt", "bar/foo.txt", 0, false},
|
|
|
|
{"*/foo.txt", "bar/foo.txt", 0, true},
|
|
|
|
{"f?o.txt", "foo.txt", 0, true},
|
|
|
|
{"f?o.txt", "fooo.txt", 0, false},
|
|
|
|
{"f[ab]o.txt", "foo.txt", 0, false},
|
|
|
|
{"f[ab]o.txt", "fao.txt", 0, true},
|
|
|
|
{"f[ab]o.txt", "fbo.txt", 0, true},
|
|
|
|
{"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},
|
2015-04-04 13:03:03 -07:00
|
|
|
{"f\\[ab\\]o.txt", "f[ab]o.txt", NoEscape, false},
|
2014-08-26 01:11:25 -07:00
|
|
|
|
|
|
|
{"*foo.txt", "bar/foo.txt", 0, true},
|
2015-04-04 13:03:03 -07:00
|
|
|
{"*foo.txt", "bar/foo.txt", PathName, false},
|
2014-08-26 01:11:25 -07:00
|
|
|
{"*/foo.txt", "bar/foo.txt", 0, true},
|
2015-04-04 13:03:03 -07:00
|
|
|
{"*/foo.txt", "bar/foo.txt", PathName, true},
|
2014-08-26 01:11:25 -07:00
|
|
|
{"*/foo.txt", "bar/baz/foo.txt", 0, true},
|
2015-04-04 13:03:03 -07:00
|
|
|
{"*/foo.txt", "bar/baz/foo.txt", PathName, false},
|
2014-08-26 01:11:25 -07:00
|
|
|
{"**/foo.txt", "bar/baz/foo.txt", 0, true},
|
2015-04-04 13:03:03 -07:00
|
|
|
{"**/foo.txt", "bar/baz/foo.txt", PathName, true},
|
2014-08-26 01:11:25 -07:00
|
|
|
|
2015-04-04 13:03:03 -07:00
|
|
|
{"foo.txt", "foo.TXT", CaseFold, true},
|
2015-03-30 13:41:12 -07:00
|
|
|
{"(?i)foo.txt", "foo.TXT", 0, true},
|
2015-02-19 00:10:32 -07:00
|
|
|
|
|
|
|
// These characters are literals in glob, but not in regexp.
|
|
|
|
{"hey$hello", "hey$hello", 0, true},
|
|
|
|
{"hey^hello", "hey^hello", 0, true},
|
|
|
|
{"hey{hello", "hey{hello", 0, true},
|
|
|
|
{"hey}hello", "hey}hello", 0, true},
|
2015-02-20 02:11:01 -07:00
|
|
|
{"hey(hello", "hey(hello", 0, true},
|
|
|
|
{"hey)hello", "hey)hello", 0, true},
|
|
|
|
{"hey|hello", "hey|hello", 0, true},
|
|
|
|
{"hey|hello", "hey|other", 0, false},
|
2014-08-26 01:11:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMatch(t *testing.T) {
|
2014-09-16 14:14:03 -07:00
|
|
|
switch runtime.GOOS {
|
2015-04-04 13:05:15 -07:00
|
|
|
case "windows", "darwin":
|
2014-09-16 14:14:03 -07:00
|
|
|
testcases = append(testcases, testcase{"foo.txt", "foo.TXT", 0, true})
|
|
|
|
fallthrough
|
|
|
|
default:
|
2014-08-26 04:07:59 -07:00
|
|
|
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})
|
2015-04-04 13:03:03 -07:00
|
|
|
testcases = append(testcases, testcase{"foo\\.txt", "foo.txt", NoEscape, false})
|
2014-08-26 04:07:59 -07:00
|
|
|
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 {
|
2014-08-26 01:11:25 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Errorf("Match(%q, %q, %d) != %v", tc.pat, tc.name, tc.flags, tc.match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalid(t *testing.T) {
|
|
|
|
if _, err := Match("foo[bar", "...", 0); err == nil {
|
|
|
|
t.Error("Unexpected nil error")
|
|
|
|
}
|
|
|
|
}
|