@@ -24,6 +24,8 @@ import (
24
24
"testing"
25
25
26
26
"sigs.k8s.io/prow/pkg/config/secret"
27
+ "sigs.k8s.io/prow/pkg/github"
28
+ "sigs.k8s.io/prow/pkg/github/fakegithub"
27
29
)
28
30
29
31
func TestValidateOptions (t * testing.T ) {
@@ -337,3 +339,86 @@ func TestCDToRootDir(t *testing.T) {
337
339
})
338
340
}
339
341
}
342
+
343
+ func TestCreateOrUpdatePROnDefaultBranch (t * testing.T ) {
344
+ testCases := []struct {
345
+ name string
346
+ org string
347
+ repo string
348
+ prTitle string
349
+ prBody string
350
+ existingPRs []* github.PullRequest
351
+ expectError bool
352
+ errorMsg string
353
+ }{
354
+ {
355
+ name : "no existing PRs" ,
356
+ org : "org" ,
357
+ repo : "repo" ,
358
+ prTitle : "Test Title" ,
359
+ prBody : "Test Body" ,
360
+ existingPRs : []* github.PullRequest {},
361
+ expectError : false ,
362
+ },
363
+ {
364
+ name : "existing PR with same title" ,
365
+ org : "org" ,
366
+ repo : "repo" ,
367
+ prTitle : "Test Title" ,
368
+ prBody : "Test Body" ,
369
+ existingPRs : []* github.PullRequest {
370
+ {
371
+ Title : "Test Title" ,
372
+ Body : "Existing PR Body" ,
373
+ },
374
+ },
375
+ expectError : false ,
376
+ },
377
+ {
378
+ name : "error creating PR" ,
379
+ org : "org" ,
380
+ repo : "repo" ,
381
+ prTitle : "Test Title" ,
382
+ prBody : "Test Body" ,
383
+ existingPRs : []* github.PullRequest {
384
+ {
385
+ Title : "Existing PR Title" ,
386
+ Body : "Existing PR Body" ,
387
+ },
388
+ },
389
+ expectError : true ,
390
+ errorMsg : "failed to create pull request" ,
391
+ },
392
+ }
393
+
394
+ for _ , tc := range testCases {
395
+ t .Run (tc .name , func (t * testing.T ) {
396
+ fakeClient := & fakegithub.FakeClient {
397
+ PullRequests : map [int ]* github.PullRequest {},
398
+ }
399
+
400
+ for i , pr := range tc .existingPRs {
401
+ fakeClient .PullRequests [i ] = pr
402
+ }
403
+
404
+ opts := & Options {
405
+ GitHubOrg : tc .org ,
406
+ GitHubRepo : tc .repo ,
407
+ GitHubLogin : "login" ,
408
+ HeadBranchName : "head-branch" ,
409
+ }
410
+
411
+ err := CreateOrUpdatePROnDefaultBranch (fakeClient , tc .org , tc .repo , opts , tc .prTitle , tc .prBody )
412
+
413
+ if tc .expectError && err == nil {
414
+ t .Errorf ("Expected to get an error but the result is nil" )
415
+ }
416
+ if ! tc .expectError && err != nil {
417
+ t .Errorf ("Expected to not get an error but got one: %v" , err )
418
+ }
419
+ if tc .expectError && err != nil && ! strings .Contains (err .Error (), tc .errorMsg ) {
420
+ t .Errorf ("Expected error message to contain %q but got %v" , tc .errorMsg , err )
421
+ }
422
+ })
423
+ }
424
+ }
0 commit comments