Skip to content

Commit

Permalink
Don't match pattern on any error (#3040)
Browse files Browse the repository at this point in the history
This prevents a pattern with no wildcards from matching in case
permissions is denied.
(cherry picked from commit 31ce98f)
  • Loading branch information
danielnelson committed Jul 25, 2017
1 parent d4f59a1 commit e7ce063
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/globpath/globpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
if !g.hasMeta {
out := make(map[string]os.FileInfo)
info, err := os.Stat(g.path)
if !os.IsNotExist(err) {
if err == nil {
out[g.path] = info
}
return out
Expand All @@ -55,7 +55,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
files, _ := filepath.Glob(g.path)
for _, file := range files {
info, err := os.Stat(file)
if !os.IsNotExist(err) {
if err == nil {
out[file] = info
}
}
Expand Down
18 changes: 18 additions & 0 deletions internal/globpath/globpath_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package globpath

import (
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -70,3 +71,20 @@ func getTestdataDir() string {
_, filename, _, _ := runtime.Caller(1)
return strings.Replace(filename, "globpath_test.go", "testdata", 1)
}

func TestMatch_ErrPermission(t *testing.T) {
tests := []struct {
input string
expected map[string]os.FileInfo
}{
{"/root/foo", map[string]os.FileInfo{}},
{"/root/f*", map[string]os.FileInfo{}},
}

for _, test := range tests {
glob, err := Compile(test.input)
require.NoError(t, err)
actual := glob.Match()
require.Equal(t, test.expected, actual)
}
}

0 comments on commit e7ce063

Please sign in to comment.