2011-03-06 15:33:23 -07:00
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2014-03-04 10:00:45 -07:00
|
|
|
|
package filepath_test
|
2011-03-06 15:33:23 -07:00
|
|
|
|
|
|
|
|
|
import (
|
2014-03-04 10:00:45 -07:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
. "path/filepath"
|
2011-03-16 17:41:23 -06:00
|
|
|
|
"runtime"
|
2012-03-18 23:51:06 -06:00
|
|
|
|
"strings"
|
2011-11-02 13:54:16 -06:00
|
|
|
|
"testing"
|
2011-03-06 15:33:23 -07:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MatchTest struct {
|
|
|
|
|
pattern, s string
|
|
|
|
|
match bool
|
2011-11-01 20:05:34 -06:00
|
|
|
|
err error
|
2011-03-06 15:33:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchTests = []MatchTest{
|
|
|
|
|
{"abc", "abc", true, nil},
|
|
|
|
|
{"*", "abc", true, nil},
|
|
|
|
|
{"*c", "abc", true, nil},
|
|
|
|
|
{"a*", "a", true, nil},
|
|
|
|
|
{"a*", "abc", true, nil},
|
|
|
|
|
{"a*", "ab/c", false, nil},
|
|
|
|
|
{"a*/b", "abc/b", true, nil},
|
|
|
|
|
{"a*/b", "a/c/b", false, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
|
|
|
|
|
{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
|
|
|
|
|
{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
|
|
|
|
|
{"ab[c]", "abc", true, nil},
|
|
|
|
|
{"ab[b-d]", "abc", true, nil},
|
|
|
|
|
{"ab[e-g]", "abc", false, nil},
|
|
|
|
|
{"ab[^c]", "abc", false, nil},
|
|
|
|
|
{"ab[^b-d]", "abc", false, nil},
|
|
|
|
|
{"ab[^e-g]", "abc", true, nil},
|
|
|
|
|
{"a\\*b", "a*b", true, nil},
|
|
|
|
|
{"a\\*b", "ab", false, nil},
|
|
|
|
|
{"a?b", "a☺b", true, nil},
|
|
|
|
|
{"a[^a]b", "a☺b", true, nil},
|
|
|
|
|
{"a???b", "a☺b", false, nil},
|
|
|
|
|
{"a[^a][^a][^a]b", "a☺b", false, nil},
|
|
|
|
|
{"[a-ζ]*", "α", true, nil},
|
|
|
|
|
{"*[a-ζ]", "A", false, nil},
|
|
|
|
|
{"a?b", "a/b", false, nil},
|
|
|
|
|
{"a*b", "a/b", false, nil},
|
|
|
|
|
{"[\\]a]", "]", true, nil},
|
|
|
|
|
{"[\\-]", "-", true, nil},
|
|
|
|
|
{"[x\\-]", "x", true, nil},
|
|
|
|
|
{"[x\\-]", "-", true, nil},
|
|
|
|
|
{"[x\\-]", "z", false, nil},
|
|
|
|
|
{"[\\-x]", "x", true, nil},
|
|
|
|
|
{"[\\-x]", "-", true, nil},
|
|
|
|
|
{"[\\-x]", "a", false, nil},
|
2011-04-04 14:09:34 -06:00
|
|
|
|
{"[]a]", "]", false, ErrBadPattern},
|
|
|
|
|
{"[-]", "-", false, ErrBadPattern},
|
|
|
|
|
{"[x-]", "x", false, ErrBadPattern},
|
|
|
|
|
{"[x-]", "-", false, ErrBadPattern},
|
|
|
|
|
{"[x-]", "z", false, ErrBadPattern},
|
|
|
|
|
{"[-x]", "x", false, ErrBadPattern},
|
|
|
|
|
{"[-x]", "-", false, ErrBadPattern},
|
|
|
|
|
{"[-x]", "a", false, ErrBadPattern},
|
|
|
|
|
{"\\", "a", false, ErrBadPattern},
|
|
|
|
|
{"[a-b-c]", "a", false, ErrBadPattern},
|
2013-07-31 00:58:28 -06:00
|
|
|
|
{"[", "a", false, ErrBadPattern},
|
|
|
|
|
{"[^", "a", false, ErrBadPattern},
|
|
|
|
|
{"[^bc", "a", false, ErrBadPattern},
|
|
|
|
|
{"a[", "a", false, nil},
|
|
|
|
|
{"a[", "ab", false, ErrBadPattern},
|
2011-03-06 15:33:23 -07:00
|
|
|
|
{"*x", "xxx", true, nil},
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
|
func errp(e error) string {
|
2011-05-31 22:17:09 -06:00
|
|
|
|
if e == nil {
|
|
|
|
|
return "<nil>"
|
|
|
|
|
}
|
2011-11-01 20:05:34 -06:00
|
|
|
|
return e.Error()
|
2011-05-31 22:17:09 -06:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-06 15:33:23 -07:00
|
|
|
|
func TestMatch(t *testing.T) {
|
|
|
|
|
for _, tt := range matchTests {
|
2012-03-18 23:51:06 -06:00
|
|
|
|
pattern := tt.pattern
|
|
|
|
|
s := tt.s
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
if strings.Index(pattern, "\\") >= 0 {
|
|
|
|
|
// no escape allowed on windows.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
pattern = Clean(pattern)
|
|
|
|
|
s = Clean(s)
|
|
|
|
|
}
|
|
|
|
|
ok, err := Match(pattern, s)
|
2011-03-06 15:33:23 -07:00
|
|
|
|
if ok != tt.match || err != tt.err {
|
2012-03-18 23:51:06 -06:00
|
|
|
|
t.Errorf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
|
2011-03-06 15:33:23 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// contains returns true if vector contains the string s.
|
|
|
|
|
func contains(vector []string, s string) bool {
|
|
|
|
|
for _, elem := range vector {
|
|
|
|
|
if elem == s {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var globTests = []struct {
|
|
|
|
|
pattern, result string
|
|
|
|
|
}{
|
|
|
|
|
{"match.go", "match.go"},
|
|
|
|
|
{"mat?h.go", "match.go"},
|
|
|
|
|
{"*", "match.go"},
|
|
|
|
|
{"../*/match.go", "../filepath/match.go"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGlob(t *testing.T) {
|
|
|
|
|
for _, tt := range globTests {
|
2012-03-18 23:51:06 -06:00
|
|
|
|
pattern := tt.pattern
|
|
|
|
|
result := tt.result
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
pattern = Clean(pattern)
|
|
|
|
|
result = Clean(result)
|
|
|
|
|
}
|
|
|
|
|
matches, err := Glob(pattern)
|
2011-04-04 14:09:34 -06:00
|
|
|
|
if err != nil {
|
2012-03-18 23:51:06 -06:00
|
|
|
|
t.Errorf("Glob error for %q: %s", pattern, err)
|
2011-04-04 14:09:34 -06:00
|
|
|
|
continue
|
|
|
|
|
}
|
2012-03-18 23:51:06 -06:00
|
|
|
|
if !contains(matches, result) {
|
|
|
|
|
t.Errorf("Glob(%#q) = %#v want %v", pattern, matches, result)
|
2011-03-06 15:33:23 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-16 21:30:54 -06:00
|
|
|
|
for _, pattern := range []string{"no_match", "../*/no_match"} {
|
|
|
|
|
matches, err := Glob(pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Glob error for %q: %s", pattern, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if len(matches) != 0 {
|
|
|
|
|
t.Errorf("Glob(%#q) = %#v want []", pattern, matches)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-06 15:33:23 -07:00
|
|
|
|
}
|
2011-04-04 14:09:34 -06:00
|
|
|
|
|
|
|
|
|
func TestGlobError(t *testing.T) {
|
|
|
|
|
_, err := Glob("[7]")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error("expected error for bad pattern; got none")
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-04 10:00:45 -07:00
|
|
|
|
|
|
|
|
|
var globSymlinkTests = []struct {
|
|
|
|
|
path, dest string
|
|
|
|
|
brokenLink bool
|
|
|
|
|
}{
|
|
|
|
|
{"test1", "link1", false},
|
|
|
|
|
{"test2", "link2", true},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGlobSymlink(t *testing.T) {
|
|
|
|
|
switch runtime.GOOS {
|
2014-07-17 01:02:46 -06:00
|
|
|
|
case "nacl", "plan9":
|
2014-05-20 10:10:19 -06:00
|
|
|
|
t.Skipf("skipping on %s", runtime.GOOS)
|
2014-07-17 01:02:46 -06:00
|
|
|
|
case "windows":
|
|
|
|
|
if !supportsSymlinks {
|
|
|
|
|
t.Skipf("skipping on %s", runtime.GOOS)
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-04 10:00:45 -07:00
|
|
|
|
}
|
2014-05-20 10:10:19 -06:00
|
|
|
|
|
2014-03-04 10:00:45 -07:00
|
|
|
|
tmpDir, err := ioutil.TempDir("", "globsymlink")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("creating temp dir:", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
|
|
for _, tt := range globSymlinkTests {
|
|
|
|
|
path := Join(tmpDir, tt.path)
|
|
|
|
|
dest := Join(tmpDir, tt.dest)
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
err = os.Symlink(path, dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if tt.brokenLink {
|
|
|
|
|
// Break the symlink.
|
|
|
|
|
os.Remove(path)
|
|
|
|
|
}
|
|
|
|
|
matches, err := Glob(dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("GlobSymlink error for %q: %s", dest, err)
|
|
|
|
|
}
|
|
|
|
|
if !contains(matches, dest) {
|
|
|
|
|
t.Errorf("Glob(%#q) = %#v want %v", dest, matches, dest)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|