aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath/match_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2020-10-22 12:11:29 -0400
committerRuss Cox <rsc@golang.org>2020-10-23 14:59:03 +0000
commitb5ddc42b465dd5b9532ee336d98343d81a6d35b2 (patch)
treed3ea8f2fe5c41f4befebaeda5600832839b0bbbb /src/path/filepath/match_test.go
parent4a2cc73f8789e3df43c1c96944c90f55757a23b0 (diff)
downloadgo-b5ddc42b465dd5b9532ee336d98343d81a6d35b2.tar.xz
io/fs, path, path/filepath, testing/fstest: validate patterns in Match, Glob
According to #28614, proposal review agreed in December 2018 that Match should return an error for failed matches where the unmatched part of the pattern has a syntax error. (The failed match has to date caused the scan of the pattern to stop early.) This change implements that behavior: the match loop continues scanning to the end of the pattern, even after a confirmed mismatch, to check whether the pattern is even well-formed. The change applies to both path.Match and filepath.Match. Then filepath.Glob and fs.Glob make a single validity-checking call to Match before beginning their usual processing. Also update fstest.TestFS to check for correct validation in custom Glob implementations. Fixes #28614. Change-Id: Ic1d35a4bb9c3565184ae83dbefc425c5c96318e7 Reviewed-on: https://go-review.googlesource.com/c/go/+/264397 Trust: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/path/filepath/match_test.go')
-rw-r--r--src/path/filepath/match_test.go12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/path/filepath/match_test.go b/src/path/filepath/match_test.go
index b8657626bc..1c3b567fa3 100644
--- a/src/path/filepath/match_test.go
+++ b/src/path/filepath/match_test.go
@@ -75,8 +75,10 @@ var matchTests = []MatchTest{
{"[", "a", false, ErrBadPattern},
{"[^", "a", false, ErrBadPattern},
{"[^bc", "a", false, ErrBadPattern},
- {"a[", "a", false, nil},
+ {"a[", "a", false, ErrBadPattern},
{"a[", "ab", false, ErrBadPattern},
+ {"a[", "x", false, ErrBadPattern},
+ {"a/b[", "x", false, ErrBadPattern},
{"*x", "xxx", true, nil},
}
@@ -155,9 +157,11 @@ func TestGlob(t *testing.T) {
}
func TestGlobError(t *testing.T) {
- _, err := Glob("[]")
- if err == nil {
- t.Error("expected error for bad pattern; got none")
+ bad := []string{`[]`, `nonexist/[]`}
+ for _, pattern := range bad {
+ if _, err := Glob(pattern); err != ErrBadPattern {
+ t.Errorf("Glob(%#q) returned err=%v, want ErrBadPattern", pattern, err)
+ }
}
}