introducing early check for pattern as done in filepath.Glob

This commit is contained in:
JB Baudens 2021-05-10 13:04:06 -06:00
parent bc94f58bed
commit 10d709b658
2 changed files with 20 additions and 2 deletions

View File

@ -32,6 +32,10 @@ import (
// This was adapted from (http://golang.org/pkg/path/filepath) and uses several
// built-ins from that package.
func Glob(fs Fs, pattern string) (matches []string, err error) {
// Check pattern is well-formed.
if _, err := filepath.Match(pattern, ""); err != nil {
return nil, err
}
if !hasMeta(pattern) {
// Lstat not supported by a ll filesystems.
if _, err = lstatIfPossible(fs, pattern); err != nil {

View File

@ -174,9 +174,23 @@ func TestGlobSymlink(t *testing.T) {
func TestGlobError(t *testing.T) {
for _, fs := range Fss {
_, err := Glob(fs, "[7]")
if err != nil {
_, err := Glob(fs, "a/b[")
if err == nil {
t.Error("expected error for bad pattern; got none")
}
}
}
func TestGlobErrorDirExistsAndContainsFile(t *testing.T) {
for _, fs := range Fss {
fs.Mkdir("a", os.ModePerm)
fs.OpenFile("a/a.txt", os.O_CREATE, os.ModePerm)
defer fs.RemoveAll("a")
_, err := Glob(fs, "a/b[")
if err == nil {
t.Error("expected error for bad pattern; got none")
}
}
}