Skip to content

Commit

Permalink
Add additional tests for wildcard matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Oct 11, 2018
1 parent 03fe3ff commit 4eb1043
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions wildcard/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
)

var (
errEmptyPattern = errors.New("input pattern was empty string")
errInvalidWildcard = errors.New("wildcard '*' can only appear between two separators")
errInvalidDoubleWildcard = errors.New("wildcard '**' can only appear at end of pattern")
errRegexpCompile = errors.New("unable to compile generated regex (internal bug)")
)
Expand Down Expand Up @@ -74,6 +76,10 @@ func NewWithSeparator(pattern string, separator rune) (Matcher, error) {
// - Wildcard '**' should match all chars, including forward slash
// All other regex meta chars will need to be quoted

if pattern == "" {
return nil, errEmptyPattern
}

segments := strings.Split(pattern, string(separator))

var regex bytes.Buffer
Expand All @@ -98,6 +104,9 @@ loop:
break loop
default:
// Segment to match literal string
if strings.Contains(segment, "*") {
return nil, errInvalidWildcard
}
regex.WriteString(regexp.QuoteMeta(segment))
}

Expand Down
15 changes: 15 additions & 0 deletions wildcard/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,18 @@ func TestMatchingWithMetaChars(t *testing.T) {
"invalid",
})
}

func TestInvalidPatterns(t *testing.T) {
for _, pattern := range []string{
"",
"test://foo*/asdf",
"test://*foo/asdf",
"test://**/asdf",
"**://foo/asdf",
} {
_, err := New(pattern)
if err == nil {
t.Errorf("should reject invalid pattern '%s'", pattern)
}
}
}

0 comments on commit 4eb1043

Please sign in to comment.