Skip to content

Commit

Permalink
return err
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Aug 28, 2024
1 parent 27bd15d commit 92ed4e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
6 changes: 5 additions & 1 deletion zglob.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,13 @@ func New(pattern string) (*zenv, error) {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
filemask = "(?i:" + filemask + ")"
}
fre, err := regexp.Compile("^" + filemask + "$")
if err != nil {
return nil, err
}
return &zenv{
dirmask: path.Dir(dirmask) + "/",
fre: regexp.MustCompile("^" + filemask + "$"),
fre: fre,
pattern: pattern,
root: filepath.Clean(root),
}, nil
Expand Down
58 changes: 30 additions & 28 deletions zglob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)

Expand All @@ -20,35 +21,36 @@ func check(got []string, expected []string) bool {
type testZGlob struct {
pattern string
expected []string
err error
err string
}

var testGlobs = []testZGlob{
{`fo*`, []string{`foo`}, nil},
{`foo`, []string{`foo`}, nil},
{`foo/*`, []string{`foo/bar`, `foo/baz`}, nil},
{`foo/b[a]*`, []string{`foo/bar`, `foo/baz`}, nil},
{`foo/b[a][r]*`, []string{`foo/bar`}, nil},
{`foo/b[a-z]*`, []string{`foo/bar`, `foo/baz`}, nil},
{`foo/b[c-z]*`, []string{}, nil},
{`foo/**`, []string{`foo/bar`, `foo/baz`}, nil},
{`f*o/**`, []string{`foo/bar`, `foo/baz`}, nil},
{`*oo/**`, []string{`foo/bar`, `foo/baz`, `hoo/bar`}, nil},
{`*oo/b*`, []string{`foo/bar`, `foo/baz`, `hoo/bar`}, nil},
{`*oo/bar`, []string{`foo/bar`, `hoo/bar`}, nil},
{`*oo/*z`, []string{`foo/baz`}, nil},
{`foo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`}, nil},
{`*oo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`, `hoo/bar`}, nil},
{`*oo/*.txt`, []string{}, nil},
{`*oo/*/*.txt`, []string{`foo/bar/baz.txt`}, nil},
{`*oo/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, nil},
{`doo`, nil, os.ErrNotExist},
{`./f*`, []string{`foo`}, nil},
{`**/bar/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, nil},
{`**/bar/**/*.{jpg,png}`, []string{`zzz/bar/baz/joo.png`, `zzz/bar/baz/zoo.jpg`}, nil},
{`zzz/bar/baz/zoo.{jpg,png}`, []string{`zzz/bar/baz/zoo.jpg`}, nil},
{`zzz/bar/{baz,z}/zoo.jpg`, []string{`zzz/bar/baz/zoo.jpg`}, nil},
{`zzz/nar/\{noo,x\}/joo.png`, []string{`zzz/nar/{noo,x}/joo.png`}, nil},
{`fo*`, []string{`foo`}, ""},
{`foo`, []string{`foo`}, ""},
{`foo/*`, []string{`foo/bar`, `foo/baz`}, ""},
{`foo/b[a]*`, []string{`foo/bar`, `foo/baz`}, ""},
{`foo/b[a][r]*`, []string{`foo/bar`}, ""},
{`foo/b[a-z]*`, []string{`foo/bar`, `foo/baz`}, ""},
{`foo/b[c-z]*`, []string{}, ""},
{`foo/b[z-c]*`, []string{}, "error parsing regexp"},
{`foo/**`, []string{`foo/bar`, `foo/baz`}, ""},
{`f*o/**`, []string{`foo/bar`, `foo/baz`}, ""},
{`*oo/**`, []string{`foo/bar`, `foo/baz`, `hoo/bar`}, ""},
{`*oo/b*`, []string{`foo/bar`, `foo/baz`, `hoo/bar`}, ""},
{`*oo/bar`, []string{`foo/bar`, `hoo/bar`}, ""},
{`*oo/*z`, []string{`foo/baz`}, ""},
{`foo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`}, ""},
{`*oo/**/*`, []string{`foo/bar`, `foo/bar/baz`, `foo/bar/baz.txt`, `foo/bar/baz/noo.txt`, `foo/baz`, `hoo/bar`}, ""},
{`*oo/*.txt`, []string{}, ""},
{`*oo/*/*.txt`, []string{`foo/bar/baz.txt`}, ""},
{`*oo/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, ""},
{`doo`, nil, "file does not exist"},
{`./f*`, []string{`foo`}, ""},
{`**/bar/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, ""},
{`**/bar/**/*.{jpg,png}`, []string{`zzz/bar/baz/joo.png`, `zzz/bar/baz/zoo.jpg`}, ""},
{`zzz/bar/baz/zoo.{jpg,png}`, []string{`zzz/bar/baz/zoo.jpg`}, ""},
{`zzz/bar/{baz,z}/zoo.jpg`, []string{`zzz/bar/baz/zoo.jpg`}, ""},
{`zzz/nar/\{noo,x\}/joo.png`, []string{`zzz/nar/{noo,x}/joo.png`}, ""},
}

func fatalIf(err error) {
Expand Down Expand Up @@ -101,7 +103,7 @@ func TestGlob(t *testing.T) {
}
got, err := Glob(test.pattern)
if err != nil {
if test.err != err {
if !strings.Contains(err.Error(), test.err) {
t.Error(err)
}
continue
Expand Down Expand Up @@ -134,7 +136,7 @@ func TestGlobAbs(t *testing.T) {
}
got, err := Glob(pattern)
if err != nil {
if test.err != err {
if !strings.Contains(err.Error(), test.err) {
t.Error(err)
}
continue
Expand Down

0 comments on commit 92ed4e5

Please sign in to comment.