github.com/watchlyhq/go-zglob@v0.0.0-20160607002833-2dbd7f37a45e/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 testZGlobs = []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/*z`, []string{`foo/baz`}, nil}, 33 {`foo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`}, nil}, 34 {`*oo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`, `hoo/bar`}, nil}, 35 {`*oo/*.txt`, []string{}, nil}, 36 {`*oo/*/*.txt`, []string{`foo/bar/baz.txt`}, nil}, 37 {`*oo/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, nil}, 38 {`doo`, nil, os.ErrNotExist}, 39 {`./f*`, []string{`foo`}, nil}, 40 } 41 42 func setup(t *testing.T) string { 43 tmpdir, err := ioutil.TempDir("", "zglob") 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 os.MkdirAll(filepath.Join(tmpdir, "foo/baz"), 0755) 49 os.MkdirAll(filepath.Join(tmpdir, "foo/bar"), 0755) 50 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz.txt"), []byte{}, 0644) 51 os.MkdirAll(filepath.Join(tmpdir, "foo/bar/baz"), 0755) 52 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz/noo.txt"), []byte{}, 0644) 53 os.MkdirAll(filepath.Join(tmpdir, "hoo/bar"), 0755) 54 ioutil.WriteFile(filepath.Join(tmpdir, "foo/bar/baz.txt"), []byte{}, 0644) 55 56 return tmpdir 57 } 58 59 func TestZGlob(t *testing.T) { 60 tmpdir := setup(t) 61 defer os.RemoveAll(tmpdir) 62 63 curdir, err := os.Getwd() 64 if err != nil { 65 t.Fatal(err) 66 } 67 err = os.Chdir(tmpdir) 68 if err != nil { 69 t.Fatal(err) 70 } 71 defer os.Chdir(curdir) 72 73 tmpdir = "." 74 for _, test := range testZGlobs { 75 got, err := Glob(test.pattern) 76 if err != nil { 77 if test.err != err { 78 t.Error(err) 79 } 80 continue 81 } 82 if !check(test.expected, got) { 83 t.Errorf(`zglob failed: pattern %q: expected %v but got %v`, test.pattern, test.expected, got) 84 } 85 } 86 } 87 88 func TestZGlobAbs(t *testing.T) { 89 tmpdir := setup(t) 90 defer os.RemoveAll(tmpdir) 91 92 curdir, err := os.Getwd() 93 if err != nil { 94 t.Fatal(err) 95 } 96 err = os.Chdir(tmpdir) 97 if err != nil { 98 t.Fatal(err) 99 } 100 defer os.Chdir(curdir) 101 102 for _, test := range testZGlobs { 103 test.pattern = filepath.ToSlash(filepath.Join(tmpdir, test.pattern)) 104 for i, expected := range test.expected { 105 test.expected[i] = filepath.ToSlash(filepath.Join(tmpdir, expected)) 106 } 107 got, err := Glob(test.pattern) 108 if err != nil { 109 if test.err != err { 110 t.Error(err) 111 } 112 continue 113 } 114 if !check(test.expected, got) { 115 t.Errorf(`zglob failed: pattern %q: expected %v but got %v`, test.pattern, test.expected, got) 116 } 117 } 118 }