From aec45a4ca7ac69461a835761a71a9aafedf719b6 Mon Sep 17 00:00:00 2001 From: Mateusz Szostok Date: Sat, 17 Oct 2020 02:14:12 +0200 Subject: [PATCH] Add err handling in Glob func --- zglob.go | 7 ++++++- zglob_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/zglob.go b/zglob.go index de5d4f3..5d49de7 100644 --- a/zglob.go +++ b/zglob.go @@ -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:] } @@ -228,6 +228,11 @@ func glob(pattern string, followSymlinks bool) ([]string, error) { } return nil }) + + if err != nil { + return nil, err + } + return matches, nil } diff --git a/zglob_test.go b/zglob_test.go index fcd06d1..deb7a4b 100644 --- a/zglob_test.go +++ b/zglob_test.go @@ -1,6 +1,7 @@ package zglob import ( + "errors" "io/ioutil" "os" "path" @@ -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) + } +}