Skip to content

Commit

Permalink
Merge pull request #37 from mszostok/fix-err-handling
Browse files Browse the repository at this point in the history
Add err handling in Glob func
  • Loading branch information
mattn committed Oct 17, 2020
2 parents 602f751 + aec45a4 commit 70beb52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion zglob.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func glob(pattern string, followSymlinks bool) ([]string, error) {
relative := !filepath.IsAbs(pattern)
matches := []string{}

fastwalk.FastWalk(zenv.root, func(path string, info os.FileMode) error {
err = fastwalk.FastWalk(zenv.root, func(path string, info os.FileMode) error {
if zenv.root == "." && len(zenv.root) < len(path) {
path = path[len(zenv.root)+1:]
}
Expand Down Expand Up @@ -228,6 +228,11 @@ func glob(pattern string, followSymlinks bool) ([]string, error) {
}
return nil
})

if err != nil {
return nil, err
}

return matches, nil
}

Expand Down
32 changes: 32 additions & 0 deletions zglob_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zglob

import (
"errors"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -190,3 +191,34 @@ func TestFollowSymlinks(t *testing.T) {
t.Errorf(`zglob failed: expected %v but got %v`, expected, got)
}
}

func TestGlobError(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "zglob")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)

err = os.MkdirAll(filepath.Join(tmpdir, "foo"), 0222)
if err != nil {
t.Fatal(err)
}

curdir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(tmpdir)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(curdir)

got, err := Glob("**/*")
if !errors.Is(err, os.ErrPermission) {
t.Errorf(`zglob failed: expected %v but got %v`, os.ErrPermission, err)
}
if !check(nil, got) {
t.Errorf(`zglob failed: expected %v but got %v`, nil, got)
}
}

0 comments on commit 70beb52

Please sign in to comment.