@@ -2,6 +2,7 @@ package main
22
33import (
44 "bytes"
5+ "fmt"
56 "strings"
67 "testing"
78)
@@ -142,6 +143,195 @@ func TestParseReviewStatus(t *testing.T) {
142143 }
143144}
144145
146+ func TestSubmitPRReview (t * testing.T ) {
147+ tests := []struct {
148+ name string
149+ prs map [string ]fakePR
150+ submitErr error
151+ event string
152+ wantURL string
153+ wantErr string
154+ }{
155+ {
156+ name : "APPROVE success" ,
157+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {URL : "https://github.com/org/repo/pull/1" , Number : "1" }},
158+ event : "APPROVE" ,
159+ wantURL : "https://github.com/org/repo/pull/1" ,
160+ },
161+ {
162+ name : "REQUEST_CHANGES success" ,
163+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {URL : "https://github.com/org/repo/pull/2" , Number : "2" }},
164+ event : "REQUEST_CHANGES" ,
165+ wantURL : "https://github.com/org/repo/pull/2" ,
166+ },
167+ {
168+ name : "no PR found" ,
169+ prs : map [string ]fakePR {},
170+ event : "APPROVE" ,
171+ wantErr : "no open PR" ,
172+ },
173+ {
174+ name : "SubmitReview fails" ,
175+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {URL : "https://github.com/org/repo/pull/1" , Number : "1" }},
176+ submitErr : fmt .Errorf ("API error" ),
177+ event : "APPROVE" ,
178+ wantErr : "submitting review" ,
179+ },
180+ }
181+
182+ for _ , tc := range tests {
183+ t .Run (tc .name , func (t * testing.T ) {
184+ client := & fakeGitHubPRClient {
185+ prs : tc .prs ,
186+ SubmitReviewErr : tc .submitErr ,
187+ }
188+ url , err := submitPRReview (client , "org/repo" , "myfork" , "wl/rig/w-123" , tc .event , "looks good" )
189+ if tc .wantErr != "" {
190+ if err == nil {
191+ t .Fatal ("expected error" )
192+ }
193+ if ! strings .Contains (err .Error (), tc .wantErr ) {
194+ t .Errorf ("error %q should contain %q" , err , tc .wantErr )
195+ }
196+ return
197+ }
198+ if err != nil {
199+ t .Fatalf ("unexpected error: %v" , err )
200+ }
201+ if url != tc .wantURL {
202+ t .Errorf ("got URL %q, want %q" , url , tc .wantURL )
203+ }
204+ })
205+ }
206+ }
207+
208+ func TestPRApprovalStatus (t * testing.T ) {
209+ tests := []struct {
210+ name string
211+ prs map [string ]fakePR
212+ reviews map [string ][]byte
213+ listReviewsErr error
214+ wantApproval bool
215+ wantChangesReq bool
216+ }{
217+ {
218+ name : "has approval" ,
219+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {Number : "1" }},
220+ reviews : map [string ][]byte {"1" : []byte (`[{"user":{"login":"alice"},"state":"APPROVED"}]` )},
221+ wantApproval : true ,
222+ },
223+ {
224+ name : "has changes requested" ,
225+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {Number : "1" }},
226+ reviews : map [string ][]byte {"1" : []byte (`[{"user":{"login":"alice"},"state":"CHANGES_REQUESTED"}]` )},
227+ wantChangesReq : true ,
228+ },
229+ {
230+ name : "no PR found" ,
231+ prs : map [string ]fakePR {},
232+ },
233+ {
234+ name : "ListReviews error" ,
235+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {Number : "1" }},
236+ listReviewsErr : fmt .Errorf ("API error" ),
237+ },
238+ }
239+
240+ for _ , tc := range tests {
241+ t .Run (tc .name , func (t * testing.T ) {
242+ client := & fakeGitHubPRClient {
243+ prs : tc .prs ,
244+ reviews : tc .reviews ,
245+ ListReviewsErr : tc .listReviewsErr ,
246+ }
247+ gotApproval , gotChangesReq := prApprovalStatus (client , "org/repo" , "myfork" , "wl/rig/w-123" )
248+ if gotApproval != tc .wantApproval {
249+ t .Errorf ("hasApproval = %v, want %v" , gotApproval , tc .wantApproval )
250+ }
251+ if gotChangesReq != tc .wantChangesReq {
252+ t .Errorf ("hasChangesRequested = %v, want %v" , gotChangesReq , tc .wantChangesReq )
253+ }
254+ })
255+ }
256+ }
257+
258+ func TestCloseGitHubPR (t * testing.T ) {
259+ tests := []struct {
260+ name string
261+ prs map [string ]fakePR
262+ closeErr error
263+ deleteRefErr error
264+ wantContains []string
265+ wantNotContains []string
266+ wantCloseCalls int
267+ wantCommentCalls int
268+ wantDeleteCalls int
269+ }{
270+ {
271+ name : "full success" ,
272+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {URL : "https://github.com/org/repo/pull/1" , Number : "1" }},
273+ wantContains : []string {"Closed PR" },
274+ wantCloseCalls : 1 ,
275+ wantCommentCalls : 1 ,
276+ wantDeleteCalls : 1 ,
277+ },
278+ {
279+ name : "no PR found" ,
280+ prs : map [string ]fakePR {},
281+ wantNotContains : []string {"Closed PR" , "warning" },
282+ },
283+ {
284+ name : "close fails" ,
285+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {URL : "https://github.com/org/repo/pull/1" , Number : "1" }},
286+ closeErr : fmt .Errorf ("API error" ),
287+ wantContains : []string {"warning" },
288+ wantNotContains : []string {"Closed PR" },
289+ wantCloseCalls : 1 ,
290+ },
291+ {
292+ name : "deleteRef fails" ,
293+ prs : map [string ]fakePR {"myfork:wl/rig/w-123" : {URL : "https://github.com/org/repo/pull/1" , Number : "1" }},
294+ deleteRefErr : fmt .Errorf ("ref error" ),
295+ wantContains : []string {"warning" , "Closed PR" },
296+ wantCloseCalls : 1 ,
297+ wantCommentCalls : 1 ,
298+ wantDeleteCalls : 1 ,
299+ },
300+ }
301+
302+ for _ , tc := range tests {
303+ t .Run (tc .name , func (t * testing.T ) {
304+ client := & fakeGitHubPRClient {
305+ prs : tc .prs ,
306+ ClosePRErr : tc .closeErr ,
307+ DeleteRefErr : tc .deleteRefErr ,
308+ }
309+ var buf bytes.Buffer
310+ closeGitHubPR (client , "org/repo" , "myfork" , "forkdb" , "wl/rig/w-123" , & buf )
311+ output := buf .String ()
312+ for _ , want := range tc .wantContains {
313+ if ! strings .Contains (output , want ) {
314+ t .Errorf ("output %q should contain %q" , output , want )
315+ }
316+ }
317+ for _ , notWant := range tc .wantNotContains {
318+ if strings .Contains (output , notWant ) {
319+ t .Errorf ("output %q should not contain %q" , output , notWant )
320+ }
321+ }
322+ if len (client .ClosePRCalls ) != tc .wantCloseCalls {
323+ t .Errorf ("ClosePR calls = %d, want %d" , len (client .ClosePRCalls ), tc .wantCloseCalls )
324+ }
325+ if len (client .AddCommentCalls ) != tc .wantCommentCalls {
326+ t .Errorf ("AddComment calls = %d, want %d" , len (client .AddCommentCalls ), tc .wantCommentCalls )
327+ }
328+ if len (client .DeleteRefCalls ) != tc .wantDeleteCalls {
329+ t .Errorf ("DeleteRef calls = %d, want %d" , len (client .DeleteRefCalls ), tc .wantDeleteCalls )
330+ }
331+ })
332+ }
333+ }
334+
145335func TestExtractWantedID (t * testing.T ) {
146336 tests := []struct {
147337 branch , want string
0 commit comments