gopkg.in/mattn/go-zglob.v0@v0.0.3/zglob_test.go (about) 1 package zglob 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "reflect" 8 "sort" 9 "testing" 10 ) 11 12 func check(got []string, expected []string) bool { 13 sort.Strings(got) 14 sort.Strings(expected) 15 return reflect.DeepEqual(expected, got) 16 } 17 18 type testZGlob struct { 19 pattern string 20 expected []string 21 err error 22 } 23 24 var testGlobs = []testZGlob{ 25 {`fo*`, []string{`foo`}, nil}, 26 {`foo`, []string{`foo`}, nil}, 27 {`foo/*`, []string{`foo/bar`, `foo/baz`}, nil}, 28 {`foo/**`, []string{`foo/bar`, `foo/baz`}, nil}, 29 {`f*o/**`, []string{`foo/bar`, `foo/baz`}, nil}, 30 {`*oo/**`, []string{`foo/bar`, `foo/baz`, `hoo/bar`}, nil}, 31 {`*oo/b*`, []string{`foo/bar`, `foo/baz`, `hoo/bar`}, nil}, 32 {`*oo/bar`, []string{`foo/bar`, `hoo/bar`}, nil}, 33 {`*oo/*z`, []string{`foo/baz`}, nil}, 34 {`foo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`}, nil}, 35 {`*oo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`, `hoo/bar`}, nil}, 36 {`*oo/*.txt`, []string{}, nil}, 37 {`*oo/*/*.txt`, []string{`foo/bar/baz.txt`}, nil}, 38 {`*oo/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, nil}, 39 {`doo`, nil, os.ErrNotExist}, 40 {`./f*`, []string{`foo`}, nil}, 41 {`**/bar/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, nil}, 42 {`**/bar/**/*.{jpg,png}`, []string{`zzz/bar/baz/joo.png`, `zzz/bar/baz/zoo.jpg`}, nil}, 43 {`zzz/bar/baz/zoo.{jpg,png}`, []string{`zzz/bar/baz/zoo.jpg`}, nil}, 44 } 45 46 func setup(t *testing.T) string { 47 tmpdir, err := ioutil.TempDir("", "zglob") 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 os.MkdirAll(filepath.Join(tmpdir, "foo/baz"), 0755) 53 os.MkdirAll(filepath.Join(tmpdir, "foo/bar"), 0755) 54 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz.txt"), []byte{}, 0644) 55 os.MkdirAll(filepath.Join(tmpdir, "foo/bar/baz"), 0755) 56 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz/noo.txt"), []byte{}, 0644) 57 os.MkdirAll(filepath.Join(tmpdir, "hoo/bar"), 0755) 58 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz.txt"), []byte{}, 0644) 59 os.MkdirAll(filepath.Join(tmpdir, "zzz/bar/baz"), 0755) 60 ioutil.WriteFile(filepath.Join(tmpdir, "zzz/bar/baz/zoo.jpg"), []byte{}, 0644) 61 ioutil.WriteFile(filepath.Join(tmpdir, "zzz/bar/baz/joo.png"), []byte{}, 0644) 62 63 return tmpdir 64 } 65 66 func TestGlob(t *testing.T) { 67 tmpdir := setup(t) 68 defer os.RemoveAll(tmpdir) 69 70 curdir, err := os.Getwd() 71 if err != nil { 72 t.Fatal(err) 73 } 74 err = os.Chdir(tmpdir) 75 if err != nil { 76 t.Fatal(err) 77 } 78 defer os.Chdir(curdir) 79 80 tmpdir = "." 81 for _, test := range testGlobs { 82 expected := make([]string, len(test.expected)) 83 for i, e := range test.expected { 84 expected[i] = e 85 } 86 got, err := Glob(test.pattern) 87 if err != nil { 88 if test.err != err { 89 t.Error(err) 90 } 91 continue 92 } 93 if !check(expected, got) { 94 t.Errorf(`zglob failed: pattern %q(%q): expected %v but got %v`, test.pattern, tmpdir, expected, got) 95 } 96 } 97 } 98 99 func TestGlobAbs(t *testing.T) { 100 tmpdir := setup(t) 101 defer os.RemoveAll(tmpdir) 102 103 curdir, err := os.Getwd() 104 if err != nil { 105 t.Fatal(err) 106 } 107 err = os.Chdir(tmpdir) 108 if err != nil { 109 t.Fatal(err) 110 } 111 defer os.Chdir(curdir) 112 113 for _, test := range testGlobs { 114 pattern := filepath.ToSlash(filepath.Join(tmpdir, test.pattern)) 115 expected := make([]string, len(test.expected)) 116 for i, e := range test.expected { 117 expected[i] = filepath.ToSlash(filepath.Join(tmpdir, e)) 118 } 119 got, err := Glob(pattern) 120 if err != nil { 121 if test.err != err { 122 t.Error(err) 123 } 124 continue 125 } 126 if !check(expected, got) { 127 t.Errorf(`zglob failed: pattern %q(%q): expected %v but got %v`, pattern, tmpdir, expected, got) 128 } 129 } 130 } 131 132 func TestMatch(t *testing.T) { 133 for _, test := range testGlobs { 134 for _, f := range test.expected { 135 got, err := Match(test.pattern, f) 136 if err != nil { 137 t.Error(err) 138 continue 139 } 140 if !got { 141 t.Errorf("%q should match with %q", f, test.pattern) 142 } 143 } 144 } 145 } 146 147 func TestFollowSymlinks(t *testing.T) { 148 tmpdir, err := ioutil.TempDir("", "zglob") 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 os.MkdirAll(filepath.Join(tmpdir, "foo"), 0755) 154 ioutil.WriteFile(filepath.Join(tmpdir, "foo/baz.txt"), []byte{}, 0644) 155 defer os.RemoveAll(tmpdir) 156 157 err = os.Symlink(filepath.Join(tmpdir, "foo"), filepath.Join(tmpdir, "bar")) 158 if err != nil { 159 t.Skip(err.Error()) 160 } 161 162 curdir, err := os.Getwd() 163 if err != nil { 164 t.Fatal(err) 165 } 166 err = os.Chdir(tmpdir) 167 if err != nil { 168 t.Fatal(err) 169 } 170 defer os.Chdir(curdir) 171 172 got, err := GlobFollowSymlinks("**/*") 173 if err != nil { 174 t.Fatal(err) 175 } 176 expected := []string{"foo", "foo/baz.txt", "bar/baz.txt"} 177 178 if !check(expected, got) { 179 t.Errorf(`zglob failed: expected %v but got %v`, expected, got) 180 } 181 }