internal/policy/codeowners_test.go
45 lines · 1401 bytes
1package policy
2
3import (
4 "reflect"
5 "testing"
6)
7
8func TestCodeowners(t *testing.T) {
9 rules := ParseCodeowners(`
10# comment
11* @alice
12*.go @bob @carol
13docs/ @dana
14/deploy/ @erin
15/cmd/gitbay/main.go @frank
16internal/* @grace
17`)
18 cases := []struct {
19 path string
20 want []string
21 }{
22 {"README.org", []string{"alice"}},
23 {"x/y/z.txt", []string{"alice"}},
24 {"main.go", []string{"bob", "carol"}},
25 {"deep/nested/thing.go", []string{"bob", "carol"}},
26 {"docs/users.org", []string{"dana"}},
27 {"sub/docs/x.md", []string{"dana"}}, // unanchored dir matches anywhere
28 {"deploy/cloud-init.yaml", []string{"erin"}}, // anchored dir
29 {"cmd/gitbay/main.go", []string{"frank"}}, // exact anchored path beats *.go (later rule)
30 {"internal/policy", []string{"grace"}}, // single-segment glob
31 }
32 for _, tc := range cases {
33 if got := OwnersFor(rules, tc.path); !reflect.DeepEqual(got, tc.want) {
34 t.Errorf("OwnersFor(%q) = %v, want %v", tc.path, got, tc.want)
35 }
36 }
37 // Later rules win: a .go file under docs/ belongs to dana, not bob.
38 if got := OwnersFor(rules, "docs/gen.go"); !reflect.DeepEqual(got, []string{"dana"}) {
39 t.Errorf("last-match-wins failed: %v", got)
40 }
41 // Anchored dir does not match nested occurrences.
42 if got := OwnersFor(rules, "x/deploy/f"); !reflect.DeepEqual(got, []string{"alice"}) {
43 t.Errorf("anchored dir leaked: %v", got)
44 }
45}