mirror of
https://github.com/golang/go
synced 2024-11-22 04:44:39 -07:00
filepath: fix Glob to return no error on nonmatching patterns
filepath.Glob is documented to return nil if no files match and an error only if the pattern is invalid. This change fixes it to work as documented and adds a regression test. R=golang-dev, r CC=golang-dev https://golang.org/cl/5040045
This commit is contained in:
parent
46eb718c99
commit
44f12eb5ad
@ -215,7 +215,7 @@ func getEsc(chunk string) (r int, nchunk string, err os.Error) {
|
|||||||
func Glob(pattern string) (matches []string, err os.Error) {
|
func Glob(pattern string) (matches []string, err os.Error) {
|
||||||
if !hasMeta(pattern) {
|
if !hasMeta(pattern) {
|
||||||
if _, err = os.Stat(pattern); err != nil {
|
if _, err = os.Stat(pattern); err != nil {
|
||||||
return
|
return nil, nil
|
||||||
}
|
}
|
||||||
return []string{pattern}, nil
|
return []string{pattern}, nil
|
||||||
}
|
}
|
||||||
|
@ -124,6 +124,16 @@ func TestGlob(t *testing.T) {
|
|||||||
t.Errorf("Glob(%#q) = %#v want %v", tt.pattern, matches, tt.result)
|
t.Errorf("Glob(%#q) = %#v want %v", tt.pattern, matches, tt.result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, pattern := range []string{"no_match", "../*/no_match"} {
|
||||||
|
matches, err := Glob(pattern)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Glob error for %q: %s", pattern, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(matches) != 0 {
|
||||||
|
t.Errorf("Glob(%#q) = %#v want []", pattern, matches)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGlobError(t *testing.T) {
|
func TestGlobError(t *testing.T) {
|
||||||
|
@ -210,7 +210,8 @@ func ParseTemplateFiles(filenames ...string) (*Set, os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseTemplateGlob creates a set by parsing the files matched
|
// ParseTemplateGlob creates a set by parsing the files matched
|
||||||
// by the pattern, each of which defines a single template. Each
|
// by the pattern, each of which defines a single template. The pattern
|
||||||
|
// is processed by filepath.Glob and must match at least one file. Each
|
||||||
// template will be named the base name of its file.
|
// template will be named the base name of its file.
|
||||||
// Unlike with ParseGlob, each file should be a stand-alone template
|
// Unlike with ParseGlob, each file should be a stand-alone template
|
||||||
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
// definition suitable for Template.Parse (not Set.Parse); that is, the
|
||||||
@ -225,6 +226,9 @@ func ParseTemplateGlob(pattern string) (*Set, os.Error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(filenames) == 0 {
|
||||||
|
return nil, fmt.Errorf("pattern matches no files: %#q", pattern)
|
||||||
|
}
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
t, err := ParseFile(filename)
|
t, err := ParseFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user