package accessctl import ( "errors" "reflect" "testing" ) type filterCommitMatchTest struct { descr string filter FilterInterface req CommitRequest match bool // assumes match == true, and will ensure that the returned wrapped error is // this one. matchErr error } func runCommitMatchTests(t *testing.T, tests []filterCommitMatchTest) { for _, test := range tests { t.Run(test.descr, func(t *testing.T) { err := test.filter.MatchCommit(test.req) shouldMatch := test.match && test.matchErr == nil if shouldMatch && err != nil { t.Fatalf("expected to match, got %v", err) } else if shouldMatch { return } else if fErr := new(ErrFilterNoMatch); !errors.As(err, fErr) { t.Fatalf("expected ErrFilterNoMatch, got %#v", err) } else if test.matchErr != nil && !reflect.DeepEqual(fErr.Err, test.matchErr) { t.Fatalf("expected err %#v, not %#v", test.matchErr, fErr.Err) } }) } } func TestFilterCommitType(t *testing.T) { mkReq := func(commitType string) CommitRequest { return CommitRequest{Type: commitType} } runCommitMatchTests(t, []filterCommitMatchTest{ { descr: "single match", filter: FilterCommitType{ Type: "foo", }, req: mkReq("foo"), match: true, }, { descr: "single no match", filter: FilterCommitType{ Type: "foo", }, req: mkReq("bar"), match: false, }, { descr: "multi match first", filter: FilterCommitType{ Types: []string{"foo", "bar"}, }, req: mkReq("foo"), match: true, }, { descr: "multi match second", filter: FilterCommitType{ Types: []string{"foo", "bar"}, }, req: mkReq("bar"), match: true, }, { descr: "multi no match", filter: FilterCommitType{ Types: []string{"foo", "bar"}, }, req: mkReq("baz"), match: false, }, }) } func TestFilterCommitAttributes(t *testing.T) { mkReq := func(nonFF bool) CommitRequest { return CommitRequest{NonFastForward: nonFF} } runCommitMatchTests(t, []filterCommitMatchTest{ { descr: "ff with empty filter", filter: FilterCommitAttributes{}, req: mkReq(false), match: true, }, { descr: "non-ff with empty filter", filter: FilterCommitAttributes{}, req: mkReq(true), match: true, }, { descr: "ff with non-ff filter", filter: FilterCommitAttributes{NonFastForward: true}, req: mkReq(false), match: false, }, { descr: "non-ff with non-ff filter", filter: FilterCommitAttributes{NonFastForward: true}, req: mkReq(true), match: true, }, { descr: "ff with inverted non-ff filter", filter: FilterNot{Filter: Filter{ CommitAttributes: &FilterCommitAttributes{NonFastForward: true}, }}, req: mkReq(false), match: true, }, { descr: "non-ff with inverted non-ff filter", filter: FilterNot{Filter: Filter{ CommitAttributes: &FilterCommitAttributes{NonFastForward: true}, }}, req: mkReq(true), match: false, }, }) }