github.com/nickbruun/go-zglob@v0.0.0-20180531213714-f7876564d6d2/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 } 43 44 func setup(t *testing.T) string { 45 tmpdir, err := ioutil.TempDir("", "zglob") 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 os.MkdirAll(filepath.Join(tmpdir, "foo/baz"), 0755) 51 os.MkdirAll(filepath.Join(tmpdir, "foo/bar"), 0755) 52 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz.txt"), []byte{}, 0644) 53 os.MkdirAll(filepath.Join(tmpdir, "foo/bar/baz"), 0755) 54 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz/noo.txt"), []byte{}, 0644) 55 os.MkdirAll(filepath.Join(tmpdir, "hoo/bar"), 0755) 56 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz.txt"), []byte{}, 0644) 57 58 return tmpdir 59 } 60 61 func TestGlob(t *testing.T) { 62 tmpdir := setup(t) 63 defer os.RemoveAll(tmpdir) 64 65 curdir, err := os.Getwd() 66 if err != nil { 67 t.Fatal(err) 68 } 69 err = os.Chdir(tmpdir) 70 if err != nil { 71 t.Fatal(err) 72 } 73 defer os.Chdir(curdir) 74 75 tmpdir = "." 76 for _, test := range testGlobs { 77 expected := make([]string, len(test.expected)) 78 for i, e := range test.expected { 79 expected[i] = e 80 } 81 got, err := Glob(test.pattern) 82 if err != nil { 83 if test.err != err { 84 t.Error(err) 85 } 86 continue 87 } 88 if !check(expected, got) { 89 t.Errorf(`zglob failed: pattern %q(%q): expected %v but got %v`, test.pattern, tmpdir, expected, got) 90 } 91 } 92 } 93 94 func TestGlobAbs(t *testing.T) { 95 tmpdir := setup(t) 96 defer os.RemoveAll(tmpdir) 97 98 curdir, err := os.Getwd() 99 if err != nil { 100 t.Fatal(err) 101 } 102 err = os.Chdir(tmpdir) 103 if err != nil { 104 t.Fatal(err) 105 } 106 defer os.Chdir(curdir) 107 108 for _, test := range testGlobs { 109 pattern := filepath.ToSlash(filepath.Join(tmpdir, test.pattern)) 110 expected := make([]string, len(test.expected)) 111 for i, e := range test.expected { 112 expected[i] = filepath.ToSlash(filepath.Join(tmpdir, e)) 113 } 114 got, err := Glob(pattern) 115 if err != nil { 116 if test.err != err { 117 t.Error(err) 118 } 119 continue 120 } 121 if !check(expected, got) { 122 t.Errorf(`zglob failed: pattern %q(%q): expected %v but got %v`, pattern, tmpdir, expected, got) 123 } 124 } 125 } 126 127 func TestMatch(t *testing.T) { 128 for _, test := range testGlobs { 129 for _, f := range test.expected { 130 got, err := Match(test.pattern, f) 131 if err != nil { 132 t.Error(err) 133 continue 134 } 135 if !got { 136 t.Errorf("%q should match with %q", f, test.pattern) 137 } 138 } 139 } 140 } 141 142 func TestFollowSymlinks(t *testing.T) { 143 tmpdir, err := ioutil.TempDir("", "zglob") 144 if err != nil { 145 t.Fatal(err) 146 } 147 148 os.MkdirAll(filepath.Join(tmpdir, "foo"), 0755) 149 ioutil.WriteFile(filepath.Join(tmpdir, "foo/baz.txt"), []byte{}, 0644) 150 defer os.RemoveAll(tmpdir) 151 152 err = os.Symlink(filepath.Join(tmpdir, "foo"), filepath.Join(tmpdir, "bar")) 153 if err != nil { 154 t.Skip(err.Error()) 155 } 156 157 curdir, err := os.Getwd() 158 if err != nil { 159 t.Fatal(err) 160 } 161 err = os.Chdir(tmpdir) 162 if err != nil { 163 t.Fatal(err) 164 } 165 defer os.Chdir(curdir) 166 167 got, err := GlobFollowSymlinks("**/*") 168 if err != nil { 169 t.Fatal(err) 170 } 171 expected := []string{"foo", "foo/baz.txt", "bar/baz.txt"} 172 173 if !check(expected, got) { 174 t.Errorf(`zglob failed: expected %v but got %v`, expected, got) 175 } 176 }