1 // Copyright © 2014 Steve Francia <spf@spf13.com>.
2 // Copyright 2009 The Go Authors. All rights reserved.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
24 // contains returns true if vector contains the string s.
25 func contains(vector []string, s string) bool {
26 for _, elem := range vector {
34 func setupGlobDirRoot(t *testing.T, fs Fs) string {
36 setupGlobFiles(t, fs, path)
40 func setupGlobDirReusePath(t *testing.T, fs Fs, path string) string {
41 testRegistry[fs] = append(testRegistry[fs], path)
42 return setupGlobFiles(t, fs, path)
45 func setupGlobFiles(t *testing.T, fs Fs, path string) string {
46 testSubDir := filepath.Join(path, "globs", "bobs")
47 err := fs.MkdirAll(testSubDir, 0700)
48 if err != nil && !os.IsExist(err) {
52 f, err := fs.Create(filepath.Join(testSubDir, "/matcher"))
56 f.WriteString("Testfile 1 content")
59 f, err = fs.Create(filepath.Join(testSubDir, "/../submatcher"))
63 f.WriteString("Testfile 2 content")
66 f, err = fs.Create(filepath.Join(testSubDir, "/../../match"))
70 f.WriteString("Testfile 3 content")
76 func TestGlob(t *testing.T) {
77 defer removeAllTestFiles(t)
79 for i, fs := range Fss {
81 testDir = setupGlobDirRoot(t, fs)
83 setupGlobDirReusePath(t, fs, testDir)
87 var globTests = []struct {
88 pattern, result string
90 {testDir + "/globs/bobs/matcher", testDir + "/globs/bobs/matcher"},
91 {testDir + "/globs/*/mat?her", testDir + "/globs/bobs/matcher"},
92 {testDir + "/globs/bobs/../*", testDir + "/globs/submatcher"},
93 {testDir + "/match", testDir + "/match"},
96 for _, fs := range Fss {
98 for _, tt := range globTests {
101 if runtime.GOOS == "windows" {
102 pattern = filepath.Clean(pattern)
103 result = filepath.Clean(result)
105 matches, err := Glob(fs, pattern)
107 t.Errorf("Glob error for %q: %s", pattern, err)
110 if !contains(matches, result) {
111 t.Errorf("Glob(%#q) = %#v want %v", pattern, matches, result)
114 for _, pattern := range []string{"no_match", "../*/no_match"} {
115 matches, err := Glob(fs, pattern)
117 t.Errorf("Glob error for %q: %s", pattern, err)
120 if len(matches) != 0 {
121 t.Errorf("Glob(%#q) = %#v want []", pattern, matches)
128 func TestGlobSymlink(t *testing.T) {
129 defer removeAllTestFiles(t)
132 testDir := setupGlobDirRoot(t, fs)
134 err := os.Symlink("target", filepath.Join(testDir, "symlink"))
136 t.Skipf("skipping on %s", runtime.GOOS)
139 var globSymlinkTests = []struct {
143 {"test1", "link1", false},
144 {"test2", "link2", true},
147 for _, tt := range globSymlinkTests {
148 path := filepath.Join(testDir, tt.path)
149 dest := filepath.Join(testDir, tt.dest)
150 f, err := fs.Create(path)
154 if err := f.Close(); err != nil {
157 err = os.Symlink(path, dest)
162 // Break the symlink.
165 matches, err := Glob(fs, dest)
167 t.Errorf("GlobSymlink error for %q: %s", dest, err)
169 if !contains(matches, dest) {
170 t.Errorf("Glob(%#q) = %#v want %v", dest, matches, dest)
176 func TestGlobError(t *testing.T) {
177 for _, fs := range Fss {
178 _, err := Glob(fs, "[7]")
180 t.Error("expected error for bad pattern; got none")