gunk-dev/the-valley

reviewer_test.go

source672eff5ffaecmain's published tip history
4.8 KiB
1package github
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "reflect"
8 "strings"
9 "testing"
10)
11
12// reviewerGH answers the two gh calls the resolver makes and records every call.
13type reviewerGH struct {
14 user string
15 userErr error
16 collaborators string // raw `gh api --paginate` output
17 collabErr error
18 calls [][]string
19}
20
21func (f *reviewerGH) run(_ context.Context, args ...string) ([]byte, error) {
22 f.calls = append(f.calls, args)
23 switch {
24 case reflect.DeepEqual(args, []string{"api", "user", "-q", ".login"}):
25 return []byte(f.user + "\n"), f.userErr
26 case len(args) == 3 && args[0] == "api" && args[1] == "--paginate":
27 return []byte(f.collaborators), f.collabErr
28 }
29 return nil, fmt.Errorf("unexpected gh call: %v", args)
30}
31
32func TestReviewerResolverResolve(t *testing.T) {
33 tests := []struct {
34 name string
35 repo string
36 configured string
37 gh reviewerGH
38 want string
39 wantWarning []string // substrings; nil means no warning
40 wantNoGHCall bool
41 }{
42 {
43 name: "configured pr_reviewer wins without gh",
44 repo: "maintainer/klaus",
45 configured: "carol",
46 want: "carol",
47 wantNoGHCall: true,
48 },
49 {
50 name: "owner picked over other writers",
51 repo: "maintainer/klaus",
52 gh: reviewerGH{user: "maintainer-agent", collaborators: `[
53 {"login":"alice","type":"User"},
54 {"login":"maintainer-agent","type":"User"},
55 {"login":"Maintainer","type":"User"},
56 {"login":"bob","type":"User"}]`},
57 want: "Maintainer",
58 },
59 {
60 // Two pages, as --paginate prints them: arrays back to back.
61 name: "single non-author writer when owner is an org",
62 repo: "acme/widget",
63 gh: reviewerGH{user: "acme-agent", collaborators: `[{"login":"acme-agent","type":"User"},{"login":"renovate","type":"Bot"}]
64[{"login":"alice","type":"User"}]`},
65 want: "alice",
66 },
67 {
68 name: "author and bots are excluded even when they own the repo",
69 repo: "acme/widget",
70 gh: reviewerGH{user: "Acme", collaborators: `[
71 {"login":"acme","type":"User"},
72 {"login":"acme-bot","type":"Bot"},
73 {"login":"alice","type":"User"}]`},
74 want: "alice",
75 },
76 {
77 name: "several candidates and no owner",
78 repo: "acme/widget",
79 gh: reviewerGH{user: "acme-agent", collaborators: `[
80 {"login":"alice","type":"User"},
81 {"login":"acme-agent","type":"User"},
82 {"login":"bob","type":"User"}]`},
83 wantWarning: []string{"alice, bob", "pr_reviewer"},
84 },
85 {
86 name: "nobody but the author can approve",
87 repo: "acme/widget",
88 gh: reviewerGH{user: "acme-agent", collaborators: `[{"login":"acme-agent","type":"User"},{"login":"ci","type":"Bot"}]`},
89 wantWarning: []string{"acme-agent", "pr_reviewer"},
90 },
91 {
92 name: "gh user lookup fails",
93 repo: "acme/widget",
94 gh: reviewerGH{userErr: errors.New("exit status 4: gh auth login\nto authenticate")},
95 wantWarning: []string{"gh auth login to authenticate", "pr_reviewer"},
96 },
97 {
98 name: "collaborators call fails",
99 repo: "acme/widget",
100 gh: reviewerGH{user: "acme-agent", collabErr: errors.New("HTTP 403: Must have push access")},
101 wantWarning: []string{"HTTP 403", "pr_reviewer"},
102 },
103 {
104 name: "unknown repo",
105 repo: "",
106 wantWarning: []string{"origin", "pr_reviewer"},
107 },
108 }
109
110 for _, tt := range tests {
111 t.Run(tt.name, func(t *testing.T) {
112 r := &ReviewerResolver{Run: tt.gh.run}
113 got, warning := r.Resolve(context.Background(), tt.repo, tt.configured)
114 if got != tt.want {
115 t.Errorf("reviewer = %q, want %q (warning %q)", got, tt.want, warning)
116 }
117 if tt.wantWarning == nil && warning != "" {
118 t.Errorf("unexpected warning %q", warning)
119 }
120 for _, sub := range tt.wantWarning {
121 if !strings.Contains(warning, sub) {
122 t.Errorf("warning %q does not mention %q", warning, sub)
123 }
124 }
125 if strings.Contains(warning, "\n") {
126 t.Errorf("warning spans several lines: %q", warning)
127 }
128 if tt.wantNoGHCall && len(tt.gh.calls) > 0 {
129 t.Errorf("gh called %v, want no calls", tt.gh.calls)
130 }
131 })
132 }
133}
134
135// The collaborators query is the one contract with GitHub that the fake can't
136// check for itself: push access, direct and outside collaborators alike, every
137// page.
138func TestReviewerResolverQueriesPushCollaborators(t *testing.T) {
139 f := &reviewerGH{user: "acme-agent", collaborators: `[{"login":"alice","type":"User"}]`}
140 r := &ReviewerResolver{Run: f.run}
141 r.Resolve(context.Background(), "acme/widget", "")
142 r.Resolve(context.Background(), "acme/gadget", "")
143
144 want := [][]string{
145 {"api", "user", "-q", ".login"},
146 {"api", "--paginate", "repos/acme/widget/collaborators?permission=push&affiliation=all"},
147 {"api", "--paginate", "repos/acme/gadget/collaborators?permission=push&affiliation=all"},
148 }
149 if !reflect.DeepEqual(f.calls, want) {
150 t.Errorf("gh calls = %v, want %v (the gh user is looked up once)", f.calls, want)
151 }
152}