Skip to content

Commit 1f0b797

Browse files
authored
Make the PushCreate test declarative (#11229)
Reduce the code duplication in the PushCreate test and switch to a declarative format. * Instead of explicitly creating the repository re-use functions from the other declarative tests and add comments * Ensure that the test repository is deleted at the end of test * Slightly reorder the sub-tests Also reduce the code duplication in MergeFork and add some comments there too and make doGitCloneFail be self-contained. Signed-off-by: Andrew Thornton [email protected]
1 parent b0849ab commit 1f0b797

File tree

3 files changed

+72
-102
lines changed

3 files changed

+72
-102
lines changed

integrations/git_helper_for_declarative_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,13 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
115115
}
116116
}
117117

118-
func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) {
118+
func doGitCloneFail(u *url.URL) func(*testing.T) {
119119
return func(t *testing.T) {
120-
assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
121-
assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
120+
tmpDir, err := ioutil.TempDir("", "doGitCloneFail")
121+
assert.NoError(t, err)
122+
defer os.RemoveAll(tmpDir)
123+
assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
124+
assert.False(t, com.IsExist(filepath.Join(tmpDir, "README.md")))
122125
}
123126
}
124127

integrations/git_test.go

Lines changed: 63 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -433,133 +433,104 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
433433
defer PrintCurrentTest(t)()
434434
var pr api.PullRequest
435435
var err error
436+
437+
// Create a test pullrequest
436438
t.Run("CreatePullRequest", func(t *testing.T) {
437439
pr, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, baseBranch, headBranch)(t)
438440
assert.NoError(t, err)
439441
})
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
448447
var diffStr string
449448
t.Run("GetDiff", func(t *testing.T) {
450449
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index))
451450
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
452451
diffStr = resp.Body.String()
453452
})
453+
454+
// Now: Merge the PR & make sure that doesn't break the PR page or change its diff
454455
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
468460
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())
496487
}
497488
}
498489

499490
func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
500491
return func(t *testing.T) {
501492
defer PrintCurrentTest(t)()
493+
494+
// create a context for a currently non-existent repository
502495
ctx.Reponame = fmt.Sprintf("repo-tmp-push-create-%s", u.Scheme)
503496
u.Path = ctx.GitPath()
504497

498+
// Create a temporary directory
505499
tmpDir, err := ioutil.TempDir("", ctx.Reponame)
506500
assert.NoError(t, err)
501+
defer os.RemoveAll(tmpDir)
507502

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))
510506

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
543508
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"))
546510

547-
// Push to create enabled
511+
// Enable "Push To Create"
548512
setting.Repository.EnablePushCreateUser = true
549513

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))
553516

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"))
557519

558-
// Fetch repo from database
520+
// Finally, fetch repo from database and ensure the correct repository has been created
559521
repo, err := models.GetRepositoryByOwnerAndName(ctx.Username, ctx.Reponame)
560522
assert.NoError(t, err)
561523
assert.False(t, repo.IsEmpty)
562524
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"))
563534
}
564535
}
565536

integrations/ssh_key_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
113113

114114
sshURL := createSSHUrl(ctx.GitPath(), u)
115115

116-
t.Run("FailToClone", doGitCloneFail(dstPath, sshURL))
116+
t.Run("FailToClone", doGitCloneFail(sshURL))
117117

118118
t.Run("CreateUserKey", doAPICreateUserKey(ctx, keyname, keyFile, func(t *testing.T, publicKey api.PublicKey) {
119119
userKeyPublicKeyID = publicKey.ID
@@ -139,7 +139,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
139139

140140
sshURL := createSSHUrl(ctx.GitPath(), u)
141141

142-
t.Run("FailToClone", doGitCloneFail(dstPath, sshURL))
142+
t.Run("FailToClone", doGitCloneFail(sshURL))
143143

144144
// Should now be able to add...
145145
t.Run("AddReadOnlyDeployKey", doAPICreateDeployKey(ctx, keyname, keyFile, true))
@@ -204,15 +204,11 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
204204
})
205205

206206
t.Run("DeleteUserKeyShouldRemoveAbilityToClone", func(t *testing.T) {
207-
dstPath, err := ioutil.TempDir("", ctx.Reponame)
208-
assert.NoError(t, err)
209-
defer os.RemoveAll(dstPath)
210-
211207
sshURL := createSSHUrl(ctx.GitPath(), u)
212208

213209
t.Run("DeleteUserKey", doAPIDeleteUserKey(ctx, userKeyPublicKeyID))
214210

215-
t.Run("FailToClone", doGitCloneFail(dstPath, sshURL))
211+
t.Run("FailToClone", doGitCloneFail(sshURL))
216212
})
217213
})
218214
}

0 commit comments

Comments
 (0)