示例#1
0
func TestGetBaseCommit(t *testing.T) {
	repo := repository.NewMockRepoForTest()

	submittedSimpleReview := Get(repo, repository.TestCommitB)
	submittedSimpleReviewBase, err := submittedSimpleReview.GetBaseCommit()
	if err != nil {
		t.Fatal("Unable to compute the base commit for a known review of a simple commit: ", err)
	}
	if submittedSimpleReviewBase != repository.TestCommitA {
		t.Fatal("Unexpected base commit computed for a known review of a simple commit.")
	}

	submittedMergeReview := Get(repo, repository.TestCommitD)
	submittedMergeReviewBase, err := submittedMergeReview.GetBaseCommit()
	if err != nil {
		t.Fatal("Unable to compute the base commit for a known review of a merge commit: ", err)
	}
	if submittedMergeReviewBase != repository.TestCommitC {
		t.Fatal("Unexpected base commit computed for a known review of a merge commit.")
	}

	pendingReview := Get(repo, repository.TestCommitG)
	pendingReviewBase, err := pendingReview.GetBaseCommit()
	if err != nil {
		t.Fatal("Unable to compute the base commit for a known review of a merge commit: ", err)
	}
	if pendingReviewBase != repository.TestCommitF {
		t.Fatal("Unexpected base commit computed for a pending review.")
	}
}
示例#2
0
func TestGetHeadCommit(t *testing.T) {
	repo := repository.NewMockRepoForTest()

	submittedSimpleReview := Get(repo, repository.TestCommitB)
	submittedSimpleReviewHead, err := submittedSimpleReview.GetHeadCommit()
	if err != nil {
		t.Fatal("Unable to compute the head commit for a known review of a simple commit: ", err)
	}
	if submittedSimpleReviewHead != repository.TestCommitB {
		t.Fatal("Unexpected head commit computed for a known review of a simple commit.")
	}

	submittedModifiedReview := Get(repo, repository.TestCommitD)
	submittedModifiedReviewHead, err := submittedModifiedReview.GetHeadCommit()
	if err != nil {
		t.Fatal("Unable to compute the head commit for a known, multi-commit review: ", err)
	}
	if submittedModifiedReviewHead != repository.TestCommitE {
		t.Fatal("Unexpected head commit for a known, multi-commit review.")
	}

	pendingReview := Get(repo, repository.TestCommitG)
	pendingReviewHead, err := pendingReview.GetHeadCommit()
	if err != nil {
		t.Fatal("Unable to compute the head commit for a known review of a merge commit: ", err)
	}
	if pendingReviewHead != repository.TestCommitI {
		t.Fatal("Unexpected head commit computed for a pending review.")
	}
}
示例#3
0
func TestRebase(t *testing.T) {
	repo := repository.NewMockRepoForTest()
	pendingReview, err := Get(repo, repository.TestCommitG)
	if err != nil {
		t.Fatal(err)
	}

	// Rebase the review and then confirm that it has been updated correctly.
	if err := pendingReview.Rebase(true); err != nil {
		t.Fatal(err)
	}
	reviewJSON, err := pendingReview.GetJSON()
	if err != nil {
		t.Fatal(err)
	}
	headRef, err := repo.GetHeadRef()
	if err != nil {
		t.Fatal(err)
	}
	if headRef != pendingReview.Request.ReviewRef {
		t.Fatal("Failed to switch to the review ref during a rebase")
	}
	isAncestor, err := repo.IsAncestor(pendingReview.Revision, archiveRef)
	if err != nil {
		t.Fatal(err)
	}
	if !isAncestor {
		t.Fatalf("Commit %q is not archived", pendingReview.Revision)
	}
	reviewCommit, err := repo.GetCommitHash(pendingReview.Request.ReviewRef)
	if err != nil {
		t.Fatal(err)
	}
	reviewAlias := pendingReview.Request.Alias
	if reviewAlias == "" || reviewAlias == pendingReview.Revision || reviewCommit != reviewAlias {
		t.Fatalf("Failed to set the review alias: %q", reviewJSON)
	}

	// Submit the review.
	if err := repo.SwitchToRef(pendingReview.Request.TargetRef); err != nil {
		t.Fatal(err)
	}
	if err := repo.MergeRef(pendingReview.Request.ReviewRef, true); err != nil {
		t.Fatal(err)
	}

	// Reread the review and confirm that it has been submitted.
	submittedReview, err := Get(repo, pendingReview.Revision)
	if err != nil {
		t.Fatal(err)
	}
	submittedReviewJSON, err := submittedReview.GetJSON()
	if err != nil {
		t.Fatal(err)
	}
	if !submittedReview.Submitted {
		t.Fatalf("Failed to submit the review: %q", submittedReviewJSON)
	}
}
func TestMirrorRepo(t *testing.T) {
	repo := repository.NewMockRepoForTest()
	tool := mockReviewTool{make(map[string]request.Request)}
	syncToRemote := true
	mirrorRepoToReview(repo, &tool, syncToRemote)
	if len(tool.Requests) != len(review.ListAll(repo)) {
		t.Errorf("Review requests are not what we expected: %v", tool.Requests)
	}
}
示例#5
0
func TestGetRequests(t *testing.T) {
	repo := repository.NewMockRepoForTest()
	pendingReview, err := Get(repo, repository.TestCommitG)
	if err != nil {
		t.Fatal(err)
	}
	if len(pendingReview.AllRequests) != 3 || pendingReview.Request.Description != "Final description of G" {
		t.Fatal("Unexpected requests for a pending review: ", pendingReview.AllRequests, pendingReview.Request)
	}
}
func TestConvertPullRequest(t *testing.T) {
	testRepo := repository.NewMockRepoForTest()
	reqNum := 4
	pullRef := fmt.Sprintf("refs/pull/%d/head", reqNum)
	pr := buildTestPullRequest(testRepo, reqNum)
	r, err := ConvertPullRequest(pr)
	if err != nil {
		t.Fatal(err)
	}
	if r == nil {
		t.Fatal("Unexpected nil request")
	}
	if r.ReviewRef != pullRef || r.TargetRef != *pr.Base.Ref || r.Requester != contributorLogin ||
		!strings.Contains(r.Description, *pr.Title) || !strings.Contains(r.Description, *pr.Body) ||
		r.BaseCommit != *pr.Base.SHA || r.Timestamp != ConvertTime(*pr.CreatedAt) {
		t.Errorf("Unexpected request %v", r)
	}
}
示例#7
0
func TestRebaseDetachedHead(t *testing.T) {
	repo := repository.NewMockRepoForTest()
	pendingReview, err := Get(repo, repository.TestCommitG)
	if err != nil {
		t.Fatal(err)
	}

	// Switch the review to having a review ref that is not a branch.
	pendingReview.Request.ReviewRef = repository.TestAlternateReviewRef
	newNote, err := pendingReview.Request.Write()
	if err != nil {
		t.Fatal(err)
	}
	if err := repo.AppendNote(request.Ref, pendingReview.Revision, newNote); err != nil {
		t.Fatal(err)
	}
	pendingReview, err = Get(repo, repository.TestCommitG)
	if err != nil {
		t.Fatal(err)
	}

	// Rebase the review and then confirm that it has been updated correctly.
	if err := pendingReview.Rebase(true); err != nil {
		t.Fatal(err)
	}
	headRef, err := repo.GetHeadRef()
	if err != nil {
		t.Fatal(err)
	}
	if headRef != pendingReview.Request.Alias {
		t.Fatal("Failed to switch to a detached head during a rebase")
	}
	isAncestor, err := repo.IsAncestor(pendingReview.Revision, archiveRef)
	if err != nil {
		t.Fatal(err)
	}
	if !isAncestor {
		t.Fatalf("Commit %q is not archived", pendingReview.Revision)
	}

	// Submit the review.
	if err := repo.SwitchToRef(pendingReview.Request.TargetRef); err != nil {
		t.Fatal(err)
	}
	reviewHead, err := pendingReview.GetHeadCommit()
	if err != nil {
		t.Fatal(err)
	}
	if err := repo.MergeRef(reviewHead, true); err != nil {
		t.Fatal(err)
	}

	// Reread the review and confirm that it has been submitted.
	submittedReview, err := Get(repo, pendingReview.Revision)
	if err != nil {
		t.Fatal(err)
	}
	submittedReviewJSON, err := submittedReview.GetJSON()
	if err != nil {
		t.Fatal(err)
	}
	if !submittedReview.Submitted {
		t.Fatalf("Failed to submit the review: %q", submittedReviewJSON)
	}
}
func TestConvertPullRequestToReview(t *testing.T) {
	testRepo := repository.NewMockRepoForTest()
	reqNum := 4
	pr := buildTestPullRequest(testRepo, reqNum)
	now := time.Now()

	issueComment1 := "Please sign our CLA"
	issueTime1 := now.Add(-2 * time.Hour)
	issueComment2 := "Done"
	issueTime2 := now.Add(-1 * time.Hour)
	issueComments := []github.IssueComment{
		github.IssueComment{
			Body: &issueComment1,
			User: &github.User{
				Login: &repoOwner,
			},
			CreatedAt: &issueTime1,
		},
		github.IssueComment{
			Body: &issueComment2,
			User: &github.User{
				Login: &contributorLogin,
			},
			CreatedAt: &issueTime2,
		},
	}

	filePath := "example.go"
	diffHunk := "@@ -4,6 +10,10 @@ func changedMethod() {\n \t// This is an existing line\n \t// This is another existing line\n-\t//This is a removed line\n+\t//This is a new line\n+\t//This is a second new line, with a comment\")"
	var commentLineNumber uint32 = 14
	diffComment1 := "Comment on line 14"
	diffTime1 := now.Add(-2 * time.Hour)
	diffComment2 := "Reply to comment on line 14"
	diffTime2 := now.Add(-2 * time.Hour)
	diffCommit := repository.TestCommitG
	diffComments := []github.PullRequestComment{
		github.PullRequestComment{
			Body:             &diffComment1,
			Path:             &filePath,
			OriginalCommitID: &diffCommit,
			DiffHunk:         &diffHunk,
			User: &github.User{
				Login: &repoOwner,
			},
			CreatedAt: &diffTime1,
		},
		github.PullRequestComment{
			Body:             &diffComment2,
			Path:             &filePath,
			OriginalCommitID: &diffCommit,
			DiffHunk:         &diffHunk,
			User: &github.User{
				Login: &contributorLogin,
			},
			CreatedAt: &diffTime2,
		},
	}

	r, err := ConvertPullRequestToReview(pr, issueComments, diffComments, testRepo)
	if err != nil {
		t.Fatal(err)
	}
	if r == nil {
		t.Fatal("Unexpected nil review")
	}
	reviewJSON, err := r.GetJSON()
	if err != nil {
		t.Fatal(err)
	}
	if !verifyCommentPresent(r, issueComment1, repoOwner) ||
		!verifyCommentPresent(r, issueComment2, contributorLogin) {
		t.Fatal("Missing expected top-level comments")
	}
	if !verifyCommentPresentAtLine(r, diffComment1, repoOwner, commentLineNumber) ||
		!verifyCommentPresentAtLine(r, diffComment2, contributorLogin, commentLineNumber) {
		t.Errorf("Missing expected line comments: %s", reviewJSON)
	}
}