|
|
|
package accessctl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStringMatcher(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
descr string
|
|
|
|
matcher StringMatcher
|
|
|
|
str string
|
|
|
|
match bool
|
|
|
|
}{
|
|
|
|
// Pattern
|
|
|
|
{
|
|
|
|
descr: "pattern exact match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo",
|
|
|
|
},
|
|
|
|
str: "foo",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern exact no match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo",
|
|
|
|
},
|
|
|
|
str: "bar",
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern single star match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo/*",
|
|
|
|
},
|
|
|
|
str: "foo/bar",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern single star no match 1",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo/*",
|
|
|
|
},
|
|
|
|
str: "foo",
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern single star no match 2",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo/*",
|
|
|
|
},
|
|
|
|
str: "foo/bar/baz",
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern double star match 1",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo/**",
|
|
|
|
},
|
|
|
|
str: "foo/bar",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern double star match 2",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo/**",
|
|
|
|
},
|
|
|
|
str: "foo/bar/baz",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "pattern double star no match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Pattern: "foo/**",
|
|
|
|
},
|
|
|
|
str: "foo",
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Patterns, assumes individual pattern matching works correctly
|
|
|
|
{
|
|
|
|
descr: "patterns single match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Patterns: []string{"foo"},
|
|
|
|
},
|
|
|
|
str: "foo",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "patterns single no match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Patterns: []string{"foo"},
|
|
|
|
},
|
|
|
|
str: "bar",
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "patterns multi first match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Patterns: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
str: "foo",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "patterns multi second match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Patterns: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
str: "bar",
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "patterns multi no match",
|
|
|
|
matcher: StringMatcher{
|
|
|
|
Patterns: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
str: "baz",
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
|
|
|
err := test.matcher.Match(test.str)
|
|
|
|
if test.match && err != nil {
|
|
|
|
t.Fatalf("expected to match, got %v", err)
|
|
|
|
} else if !test.match && !errors.As(err, new(ErrFilterNoMatch)) {
|
|
|
|
t.Fatalf("expected ErrFilterNoMatch, got %#v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterFilesChanged(t *testing.T) {
|
|
|
|
mkReq := func(paths ...string) CommitRequest {
|
|
|
|
return CommitRequest{FilesChanged: paths}
|
|
|
|
}
|
|
|
|
|
|
|
|
runCommitMatchTests(t, []filterCommitMatchTest{
|
|
|
|
{
|
|
|
|
descr: "no paths",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Pattern: "foo"},
|
|
|
|
},
|
|
|
|
req: mkReq(),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "all paths against one pattern",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Pattern: "foo/*"},
|
|
|
|
},
|
|
|
|
req: mkReq("foo/bar", "foo/baz"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "all paths against multiple patterns",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Patterns: []string{"foo", "bar"}},
|
|
|
|
},
|
|
|
|
req: mkReq("foo", "bar"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "some paths against one pattern",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Pattern: "foo"},
|
|
|
|
},
|
|
|
|
req: mkReq("foo", "bar"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "some paths against many patterns",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Patterns: []string{"foo", "bar"}},
|
|
|
|
},
|
|
|
|
req: mkReq("foo", "baz"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "no paths against one pattern",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Pattern: "foo"},
|
|
|
|
},
|
|
|
|
req: mkReq("baz", "buz"),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "no paths against many patterns",
|
|
|
|
filter: FilterFilesChanged{
|
|
|
|
StringMatcher: StringMatcher{Patterns: []string{"foo", "bar"}},
|
|
|
|
},
|
|
|
|
req: mkReq("baz", "buz"),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|