Don't match pattern on any error (#3040)

This prevents a pattern with no wildcards from matching in case
permissions is denied.
This commit is contained in:
Daniel Nelson 2017-07-21 14:28:14 -07:00 committed by GitHub
parent a6c44e3a00
commit e822d22565
2 changed files with 20 additions and 2 deletions

View File

@ -45,7 +45,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
if !g.hasMeta { if !g.hasMeta {
out := make(map[string]os.FileInfo) out := make(map[string]os.FileInfo)
info, err := os.Stat(g.path) info, err := os.Stat(g.path)
if !os.IsNotExist(err) { if err == nil {
out[g.path] = info out[g.path] = info
} }
return out return out
@ -55,7 +55,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
files, _ := filepath.Glob(g.path) files, _ := filepath.Glob(g.path)
for _, file := range files { for _, file := range files {
info, err := os.Stat(file) info, err := os.Stat(file)
if !os.IsNotExist(err) { if err == nil {
out[file] = info out[file] = info
} }
} }

View File

@ -1,6 +1,7 @@
package globpath package globpath
import ( import (
"os"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
@ -70,3 +71,20 @@ func getTestdataDir() string {
_, filename, _, _ := runtime.Caller(1) _, filename, _, _ := runtime.Caller(1)
return strings.Replace(filename, "globpath_test.go", "testdata", 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)
}
}