Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip more validation #101

Merged
merged 3 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doublestar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ func TestMatchUnvalidated(t *testing.T) {
for idx, tt := range matchTests {
testMatchUnvalidatedWith(t, idx, tt)
}

// Not only test that they get the right matches, but make sure Unvalidated is properly *skipping* validation
unvalidatedTests := []struct {
pattern, testPath string // a pattern and path to test the pattern on
expectedErrValidated error // the error expected if the pattern was being validated
expectedErrUnvalidated error // the error expected if the pattern was not being validated
}{
{"", "", nil, nil},
{"*", "", nil, nil},
{"a[", "a", ErrBadPattern, nil}, // End early because got to end of match; do not need to validate the rest
{"a[", "b", ErrBadPattern, nil}, // End early because failed to match; do not need to validate the rest
{"[", "a", ErrBadPattern, ErrBadPattern}, // Error right up front, needs to fail whether validate or not
}
for idx, tt := range unvalidatedTests {
_, errValidated := matchWithSeparator(tt.pattern, tt.testPath, '/', true)
_, errUnvalidated := matchWithSeparator(tt.pattern, tt.testPath, '/', false)
if errValidated != tt.expectedErrValidated {
t.Errorf("#%v. Validated error of Match(%#q, %#q) = %v want %v", idx, tt.pattern, tt.testPath, errValidated, tt.expectedErrValidated)
}
if errUnvalidated != tt.expectedErrUnvalidated {
t.Errorf("#%v. Unvalidated error of Match(%#q, %#q) = %v want %v", idx, tt.pattern, tt.testPath, errUnvalidated, tt.expectedErrUnvalidated)
}
}
}

func testMatchUnvalidatedWith(t *testing.T, idx int, tt MatchTest) {
Expand Down
10 changes: 5 additions & 5 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ MATCH:
// we've reached the end of `name`; we've successfully matched if we've also
// reached the end of `pattern`, or if the rest of `pattern` can match a
// zero-length string
return isZeroLengthPattern(pattern[patIdx:], separator)
return isZeroLengthPattern(pattern[patIdx:], separator, validate)
}

func isZeroLengthPattern(pattern string, separator rune) (ret bool, err error) {
func isZeroLengthPattern(pattern string, separator rune, validate bool) (ret bool, err error) {
// `/**`, `**/`, and `/**/` are special cases - a pattern such as `path/to/a/**` or `path/to/a/**/`
// *should* match `path/to/a` because `a` might be a directory
if pattern == "" ||
Expand Down Expand Up @@ -350,18 +350,18 @@ func isZeroLengthPattern(pattern string, separator rune) (ret bool, err error) {
}
commaIdx += patIdx

ret, err = isZeroLengthPattern(pattern[patIdx:commaIdx]+pattern[closingIdx+1:], separator)
ret, err = isZeroLengthPattern(pattern[patIdx:commaIdx]+pattern[closingIdx+1:], separator, validate)
if ret || err != nil {
return
}

patIdx = commaIdx + 1
}
return isZeroLengthPattern(pattern[patIdx:closingIdx]+pattern[closingIdx+1:], separator)
return isZeroLengthPattern(pattern[patIdx:closingIdx]+pattern[closingIdx+1:], separator, validate)
}

// no luck - validate the rest of the pattern
if !doValidatePattern(pattern, separator) {
if validate && !doValidatePattern(pattern, separator) {
return false, ErrBadPattern
}
return false, nil
Expand Down
Loading