提交 97adc9da 编写于 作者: C crumblycake

Enhance merge test coverage to better test possible outcomes.

上级 bc101b2b
using System; using System.Linq;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers; using LibGit2Sharp.Tests.TestHelpers;
using Xunit; using Xunit;
using Xunit.Extensions;
namespace LibGit2Sharp.Tests namespace LibGit2Sharp.Tests
{ {
...@@ -81,9 +81,11 @@ public void CanRetrieveTheBranchBeingMerged() ...@@ -81,9 +81,11 @@ public void CanRetrieveTheBranchBeingMerged()
Assert.Null(mergedHeads[1].Tip); Assert.Null(mergedHeads[1].Tip);
} }
} }
[Fact] [Theory]
public void CanMergeRepoNonFastForward() [InlineData(true)]
[InlineData(false)]
public void CanMergeRepoNonFastForward(bool shouldMergeOccurInDetachedHeadState)
{ {
const string firstBranchFileName = "first branch file.txt"; const string firstBranchFileName = "first branch file.txt";
const string secondBranchFileName = "second branch file.txt"; const string secondBranchFileName = "second branch file.txt";
...@@ -104,7 +106,15 @@ public void CanMergeRepoNonFastForward() ...@@ -104,7 +106,15 @@ public void CanMergeRepoNonFastForward()
// Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one).
AddFileCommitToRepo(repo, firstBranchFileName); AddFileCommitToRepo(repo, firstBranchFileName);
secondBranch.Checkout(); if (shouldMergeOccurInDetachedHeadState)
{
// Detaches HEAD
repo.Checkout(secondBranch.Tip);
}
else
{
secondBranch.Checkout();
}
// Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). // Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit).
AddFileCommitToRepo(repo, secondBranchFileName); AddFileCommitToRepo(repo, secondBranchFileName);
...@@ -116,6 +126,13 @@ public void CanMergeRepoNonFastForward() ...@@ -116,6 +126,13 @@ public void CanMergeRepoNonFastForward()
Assert.Equal(repo.Head.Tip, mergeResult.Commit); Assert.Equal(repo.Head.Tip, mergeResult.Commit);
Assert.Equal(originalTreeCount + 3, mergeResult.Commit.Tree.Count); // Expecting original tree count plussed by the 3 added files. Assert.Equal(originalTreeCount + 3, mergeResult.Commit.Tree.Count); // Expecting original tree count plussed by the 3 added files.
Assert.Equal(2, mergeResult.Commit.Parents.Count()); // Merge commit should have 2 parents Assert.Equal(2, mergeResult.Commit.Parents.Count()); // Merge commit should have 2 parents
Assert.Equal(shouldMergeOccurInDetachedHeadState, repo.Info.IsHeadDetached);
if (!shouldMergeOccurInDetachedHeadState)
{
// Ensure HEAD is still attached and points to SecondBranch
Assert.Equal(repo.Refs.Head.TargetIdentifier, secondBranch.CanonicalName);
}
} }
} }
...@@ -143,8 +160,10 @@ public void IsUpToDateMerge() ...@@ -143,8 +160,10 @@ public void IsUpToDateMerge()
} }
} }
[Fact] [Theory]
public void CanFastForwardRepos() [InlineData(true)]
[InlineData(false)]
public void CanFastForwardRepos(bool shouldMergeOccurInDetachedHeadState)
{ {
const string firstBranchFileName = "first branch file.txt"; const string firstBranchFileName = "first branch file.txt";
const string sharedBranchFileName = "first+second branch file.txt"; const string sharedBranchFileName = "first+second branch file.txt";
...@@ -169,14 +188,33 @@ public void CanFastForwardRepos() ...@@ -169,14 +188,33 @@ public void CanFastForwardRepos()
// Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one).
AddFileCommitToRepo(repo, firstBranchFileName); AddFileCommitToRepo(repo, firstBranchFileName);
secondBranch.Checkout(); if (shouldMergeOccurInDetachedHeadState)
{
// Detaches HEAD
repo.Checkout(secondBranch.Tip);
}
else
{
secondBranch.Checkout();
}
Assert.Equal(shouldMergeOccurInDetachedHeadState, repo.Info.IsHeadDetached);
MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature);
Assert.Equal(MergeStatus.FastForward, mergeResult.Status); Assert.Equal(MergeStatus.FastForward, mergeResult.Status);
Assert.Equal(repo.Branches["FirstBranch"].Tip, mergeResult.Commit); Assert.Equal(repo.Branches["FirstBranch"].Tip, mergeResult.Commit);
Assert.Equal(repo.Branches["FirstBranch"].Tip, repo.Head.Tip); Assert.Equal(repo.Branches["FirstBranch"].Tip, repo.Head.Tip);
Assert.Equal(repo.Head.Tip, mergeResult.Commit);
Assert.Equal(0, repo.Index.RetrieveStatus().Count()); Assert.Equal(0, repo.Index.RetrieveStatus().Count());
Assert.Equal(shouldMergeOccurInDetachedHeadState, repo.Info.IsHeadDetached);
if (!shouldMergeOccurInDetachedHeadState)
{
// Ensure HEAD is still attached and points to SecondBranch
Assert.Equal(repo.Refs.Head.TargetIdentifier, secondBranch.CanonicalName);
}
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册