Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Stack.Tests/Commands/Helpers/StackActionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using FluentAssertions;
using NSubstitute;
using Stack.Commands;
using Stack.Commands.Helpers;
using Stack.Git;
using Stack.Infrastructure;
using Xunit.Abstractions;

namespace Stack.Tests.Helpers;

public class StackActionsTests(ITestOutputHelper testOutputHelper)
{
[Fact]
public void PullChanges_WhenSomeBranchesHaveChanges_AndOthersDoNot_OnlyPullsChangesForBranchesThatNeedIt()
{
// Arrange
var sourceBranch = Some.BranchName();
var branchWithRemoteChanges = Some.BranchName();
var branchWithoutRemoteChanges = Some.BranchName();

var gitClient = Substitute.For<IGitClient>();

var branchStatus = new Dictionary<string, GitBranchStatus>
{
{ sourceBranch, new GitBranchStatus(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) },
{ branchWithRemoteChanges, new GitBranchStatus(branchWithRemoteChanges, $"origin/{branchWithRemoteChanges}", true, false, 0, 3, new Commit(Some.Sha(), Some.Name())) },
{ branchWithoutRemoteChanges, new GitBranchStatus(branchWithoutRemoteChanges, $"origin/{branchWithoutRemoteChanges}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) }
};

gitClient.GetBranchStatuses(Arg.Any<string[]>()).Returns(branchStatus);

var stack = new TestStackBuilder()
.WithSourceBranch(sourceBranch)
.WithBranch(b => b.WithName(branchWithRemoteChanges))
.WithBranch(b => b.WithName(branchWithoutRemoteChanges))
.Build();

var stackActions = new StackActions(gitClient, new TestLogger(testOutputHelper));

// Act
stackActions.PullChanges(stack);

// Assert
gitClient.DidNotReceive().PullBranch(sourceBranch);
gitClient.Received().PullBranch(branchWithRemoteChanges);
gitClient.DidNotReceive().PullBranch(branchWithoutRemoteChanges);
}

[Fact]
public void PullChanges_WhenSomeBranchesDoNotExistInRemote_OnlyPullsBranchesThatExistInRemote()
{
// Arrange
var sourceBranch = Some.BranchName();
var branchThatExistsInRemote = Some.BranchName();
var branchThatDoesNotExistInRemote = Some.BranchName();

var gitClient = Substitute.For<IGitClient>();

var branchStatus = new Dictionary<string, GitBranchStatus>
{
{ sourceBranch, new GitBranchStatus(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) },
{ branchThatExistsInRemote, new GitBranchStatus(branchThatExistsInRemote, $"origin/{branchThatExistsInRemote}", true, false, 0, 2, new Commit(Some.Sha(), Some.Name())) },
{ branchThatDoesNotExistInRemote, new GitBranchStatus(branchThatDoesNotExistInRemote, $"origin/{branchThatDoesNotExistInRemote}", false, false, 0, 0, new Commit(Some.Sha(), Some.Name())) }
};

gitClient.GetBranchStatuses(Arg.Any<string[]>()).Returns(branchStatus);

var stack = new TestStackBuilder()
.WithSourceBranch(sourceBranch)
.WithBranch(b => b.WithName(branchThatExistsInRemote))
.WithBranch(b => b.WithName(branchThatDoesNotExistInRemote))
.Build();

var stackActions = new StackActions(gitClient, new TestLogger(testOutputHelper));

// Act
stackActions.PullChanges(stack);

// Assert
gitClient.DidNotReceive().PullBranch(sourceBranch);
gitClient.Received().PullBranch(branchThatExistsInRemote);
gitClient.DidNotReceive().PullBranch(branchThatDoesNotExistInRemote);
}
}
34 changes: 0 additions & 34 deletions src/Stack.Tests/Commands/Helpers/StackHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,38 +472,4 @@ public void PushChanges_WhenSomeLocalBranchesAreAhead_OnlyPushesChangesForBranch
// Assert
branchesPushedToRemote.ToArray().Should().BeEquivalentTo([branchAheadOfRemote]);
}

[Fact]
public void PullChanges_WhenSomeBranchesHaveChanges_AndOthersDoNot_OnlyPullsChangesForBranchesThatNeedIt()
{
// Arrange
var sourceBranch = Some.BranchName();
var branchWithRemoteChanges = Some.BranchName();
var branchWithoutRemoteChanges = Some.BranchName();

var gitClient = Substitute.For<IGitClient>();

var branchStatus = new Dictionary<string, GitBranchStatus>
{
{ sourceBranch, new GitBranchStatus(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) },
{ branchWithRemoteChanges, new GitBranchStatus(branchWithRemoteChanges, $"origin/{branchWithRemoteChanges}", true, false, 0, 3, new Commit(Some.Sha(), Some.Name())) },
{ branchWithoutRemoteChanges, new GitBranchStatus(branchWithoutRemoteChanges, $"origin/{branchWithoutRemoteChanges}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) }
};

gitClient.GetBranchStatuses(Arg.Any<string[]>()).Returns(branchStatus);

var stack = new TestStackBuilder()
.WithSourceBranch(sourceBranch)
.WithBranch(b => b.WithName(branchWithRemoteChanges))
.WithBranch(b => b.WithName(branchWithoutRemoteChanges))
.Build();

// Act
StackHelpers.PullChanges(stack, gitClient, new TestLogger(testOutputHelper));

// Assert
gitClient.DidNotReceive().PullBranch(sourceBranch);
gitClient.Received().PullBranch(branchWithRemoteChanges);
gitClient.DidNotReceive().PullBranch(branchWithoutRemoteChanges);
}
}
Loading
Loading