@@ -433,133 +433,104 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
433
433
defer PrintCurrentTest (t )()
434
434
var pr api.PullRequest
435
435
var err error
436
+
437
+ // Create a test pullrequest
436
438
t .Run ("CreatePullRequest" , func (t * testing.T ) {
437
439
pr , err = doAPICreatePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , baseBranch , headBranch )(t )
438
440
assert .NoError (t , err )
439
441
})
440
- t .Run ("EnsureCanSeePull" , func (t * testing.T ) {
441
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
442
- ctx .Session .MakeRequest (t , req , http .StatusOK )
443
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/files" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
444
- ctx .Session .MakeRequest (t , req , http .StatusOK )
445
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/commits" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
446
- ctx .Session .MakeRequest (t , req , http .StatusOK )
447
- })
442
+
443
+ // Ensure the PR page works
444
+ t .Run ("EnsureCanSeePull" , doEnsureCanSeePull (baseCtx , pr ))
445
+
446
+ // Then get the diff string
448
447
var diffStr string
449
448
t .Run ("GetDiff" , func (t * testing.T ) {
450
449
req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d.diff" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
451
450
resp := ctx .Session .MakeRequest (t , req , http .StatusOK )
452
451
diffStr = resp .Body .String ()
453
452
})
453
+
454
+ // Now: Merge the PR & make sure that doesn't break the PR page or change its diff
454
455
t .Run ("MergePR" , doAPIMergePullRequest (baseCtx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
455
- t .Run ("EnsureCanSeePull" , func (t * testing.T ) {
456
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
457
- ctx .Session .MakeRequest (t , req , http .StatusOK )
458
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/files" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
459
- ctx .Session .MakeRequest (t , req , http .StatusOK )
460
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/commits" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
461
- ctx .Session .MakeRequest (t , req , http .StatusOK )
462
- })
463
- t .Run ("EnsureDiffNoChange" , func (t * testing.T ) {
464
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d.diff" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
465
- resp := ctx .Session .MakeRequest (t , req , http .StatusOK )
466
- assert .Equal (t , diffStr , resp .Body .String ())
467
- })
456
+ t .Run ("EnsureCanSeePull" , doEnsureCanSeePull (baseCtx , pr ))
457
+ t .Run ("EnsureDiffNoChange" , doEnsureDiffNoChange (baseCtx , pr , diffStr ))
458
+
459
+ // Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
468
460
t .Run ("DeleteHeadBranch" , doBranchDelete (baseCtx , baseCtx .Username , baseCtx .Reponame , headBranch ))
469
- t .Run ("EnsureCanSeePull" , func (t * testing.T ) {
470
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
471
- ctx .Session .MakeRequest (t , req , http .StatusOK )
472
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/files" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
473
- ctx .Session .MakeRequest (t , req , http .StatusOK )
474
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/commits" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
475
- ctx .Session .MakeRequest (t , req , http .StatusOK )
476
- })
477
- t .Run ("EnsureDiffNoChange" , func (t * testing.T ) {
478
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d.diff" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
479
- resp := ctx .Session .MakeRequest (t , req , http .StatusOK )
480
- assert .Equal (t , diffStr , resp .Body .String ())
481
- })
482
- t .Run ("DeleteRepository" , doAPIDeleteRepository (ctx ))
483
- t .Run ("EnsureCanSeePull" , func (t * testing.T ) {
484
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
485
- ctx .Session .MakeRequest (t , req , http .StatusOK )
486
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/files" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
487
- ctx .Session .MakeRequest (t , req , http .StatusOK )
488
- req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/commits" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
489
- ctx .Session .MakeRequest (t , req , http .StatusOK )
490
- })
491
- t .Run ("EnsureDiffNoChange" , func (t * testing.T ) {
492
- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d.diff" , url .PathEscape (baseCtx .Username ), url .PathEscape (baseCtx .Reponame ), pr .Index ))
493
- resp := ctx .Session .MakeRequest (t , req , http .StatusOK )
494
- assert .Equal (t , diffStr , resp .Body .String ())
495
- })
461
+ t .Run ("EnsureCanSeePull" , doEnsureCanSeePull (baseCtx , pr ))
462
+ t .Run ("EnsureDiffNoChange" , doEnsureDiffNoChange (baseCtx , pr , diffStr ))
463
+
464
+ // Delete the head repository & make sure that doesn't break the PR page or change its diff
465
+ t .Run ("DeleteHeadRepository" , doAPIDeleteRepository (ctx ))
466
+ t .Run ("EnsureCanSeePull" , doEnsureCanSeePull (baseCtx , pr ))
467
+ t .Run ("EnsureDiffNoChange" , doEnsureDiffNoChange (baseCtx , pr , diffStr ))
468
+ }
469
+ }
470
+
471
+ func doEnsureCanSeePull (ctx APITestContext , pr api.PullRequest ) func (t * testing.T ) {
472
+ return func (t * testing.T ) {
473
+ req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame ), pr .Index ))
474
+ ctx .Session .MakeRequest (t , req , http .StatusOK )
475
+ req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/files" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame ), pr .Index ))
476
+ ctx .Session .MakeRequest (t , req , http .StatusOK )
477
+ req = NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/commits" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame ), pr .Index ))
478
+ ctx .Session .MakeRequest (t , req , http .StatusOK )
479
+ }
480
+ }
481
+
482
+ func doEnsureDiffNoChange (ctx APITestContext , pr api.PullRequest , diffStr string ) func (t * testing.T ) {
483
+ return func (t * testing.T ) {
484
+ req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d.diff" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame ), pr .Index ))
485
+ resp := ctx .Session .MakeRequest (t , req , http .StatusOK )
486
+ assert .Equal (t , diffStr , resp .Body .String ())
496
487
}
497
488
}
498
489
499
490
func doPushCreate (ctx APITestContext , u * url.URL ) func (t * testing.T ) {
500
491
return func (t * testing.T ) {
501
492
defer PrintCurrentTest (t )()
493
+
494
+ // create a context for a currently non-existent repository
502
495
ctx .Reponame = fmt .Sprintf ("repo-tmp-push-create-%s" , u .Scheme )
503
496
u .Path = ctx .GitPath ()
504
497
498
+ // Create a temporary directory
505
499
tmpDir , err := ioutil .TempDir ("" , ctx .Reponame )
506
500
assert .NoError (t , err )
501
+ defer os .RemoveAll (tmpDir )
507
502
508
- err = git .InitRepository (tmpDir , false )
509
- assert .NoError (t , err )
503
+ // Now create local repository to push as our test and set its origin
504
+ t .Run ("InitTestRepository" , doGitInitTestRepository (tmpDir ))
505
+ t .Run ("AddRemote" , doGitAddRemote (tmpDir , "origin" , u ))
510
506
511
- _ , err = os .Create (filepath .Join (tmpDir , "test.txt" ))
512
- assert .NoError (t , err )
513
-
514
- err = git .AddChanges (tmpDir , true )
515
- assert .NoError (t , err )
516
-
517
- err = git .CommitChanges (tmpDir , git.CommitChangesOptions {
518
- Committer : & git.Signature {
519
-
520
- Name : "User Two" ,
521
- When : time .Now (),
522
- },
523
- Author : & git.Signature {
524
-
525
- Name : "User Two" ,
526
- When : time .Now (),
527
- },
528
- Message : fmt .Sprintf ("Testing push create @ %v" , time .Now ()),
529
- })
530
- assert .NoError (t , err )
531
-
532
- _ , err = git .NewCommand ("remote" , "add" , "origin" , u .String ()).RunInDir (tmpDir )
533
- assert .NoError (t , err )
534
-
535
- invalidCtx := ctx
536
- invalidCtx .Reponame = fmt .Sprintf ("invalid/repo-tmp-push-create-%s" , u .Scheme )
537
- u .Path = invalidCtx .GitPath ()
538
-
539
- _ , err = git .NewCommand ("remote" , "add" , "invalid" , u .String ()).RunInDir (tmpDir )
540
- assert .NoError (t , err )
541
-
542
- // Push to create disabled
507
+ // Disable "Push To Create" and attempt to push
543
508
setting .Repository .EnablePushCreateUser = false
544
- _ , err = git .NewCommand ("push" , "origin" , "master" ).RunInDir (tmpDir )
545
- assert .Error (t , err )
509
+ t .Run ("FailToPushAndCreateTestRepository" , doGitPushTestRepositoryFail (tmpDir , "origin" , "master" ))
546
510
547
- // Push to create enabled
511
+ // Enable " Push To Create"
548
512
setting .Repository .EnablePushCreateUser = true
549
513
550
- // Invalid repo
551
- _ , err = git .NewCommand ("push" , "invalid" , "master" ).RunInDir (tmpDir )
552
- assert .Error (t , err )
514
+ // Assert that cloning from a non-existent repository does not create it and that it definitely wasn't create above
515
+ t .Run ("FailToCloneFromNonExistentRepository" , doGitCloneFail (u ))
553
516
554
- // Valid repo
555
- _ , err = git .NewCommand ("push" , "origin" , "master" ).RunInDir (tmpDir )
556
- assert .NoError (t , err )
517
+ // Then "Push To Create"x
518
+ t .Run ("SuccessfullyPushAndCreateTestRepository" , doGitPushTestRepository (tmpDir , "origin" , "master" ))
557
519
558
- // Fetch repo from database
520
+ // Finally, fetch repo from database and ensure the correct repository has been created
559
521
repo , err := models .GetRepositoryByOwnerAndName (ctx .Username , ctx .Reponame )
560
522
assert .NoError (t , err )
561
523
assert .False (t , repo .IsEmpty )
562
524
assert .True (t , repo .IsPrivate )
525
+
526
+ // Now add a remote that is invalid to "Push To Create"
527
+ invalidCtx := ctx
528
+ invalidCtx .Reponame = fmt .Sprintf ("invalid/repo-tmp-push-create-%s" , u .Scheme )
529
+ u .Path = invalidCtx .GitPath ()
530
+ t .Run ("AddInvalidRemote" , doGitAddRemote (tmpDir , "invalid" , u ))
531
+
532
+ // Fail to "Push To Create" the invalid
533
+ t .Run ("FailToPushAndCreateInvalidTestRepository" , doGitPushTestRepositoryFail (tmpDir , "invalid" , "master" ))
563
534
}
564
535
}
565
536
0 commit comments