-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors to add tests for branch commands (#78)
- Loading branch information
1 parent
2169a14
commit 5c15d13
Showing
4 changed files
with
694 additions
and
25 deletions.
There are no files selected for viewing
266 changes: 266 additions & 0 deletions
266
src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
using FluentAssertions; | ||
using NSubstitute; | ||
using Stack.Commands; | ||
using Stack.Commands.Helpers; | ||
using Stack.Config; | ||
using Stack.Git; | ||
using Stack.Infrastructure; | ||
using Stack.Tests.Helpers; | ||
|
||
namespace Stack.Tests.Commands.Stack; | ||
|
||
public class AddBranchCommandHandlerTests | ||
{ | ||
[Fact] | ||
public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_AddsBranchToStack() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
gitOperations.DoesLocalBranchExist("branch-5").Returns(true); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
stackConfig | ||
.WhenForAnyArgs(s => s.Save(Arg.Any<List<Config.Stack>>())) | ||
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0)); | ||
|
||
inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1"); | ||
inputProvider.Select(Questions.SelectBranch, Arg.Any<string[]>()).Returns("branch-5"); | ||
|
||
// Act | ||
await handler.Handle(AddBranchCommandInputs.Empty); | ||
|
||
// Assert | ||
stacks.Should().BeEquivalentTo(new List<Config.Stack> | ||
{ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenStackNameProvided_DoesNotAskForStackName_AddsBranchFromStack() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
gitOperations.DoesLocalBranchExist("branch-5").Returns(true); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
stackConfig | ||
.WhenForAnyArgs(s => s.Save(Arg.Any<List<Config.Stack>>())) | ||
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0)); | ||
|
||
inputProvider.Select(Questions.SelectBranch, Arg.Any<string[]>()).Returns("branch-5"); | ||
|
||
// Act | ||
await handler.Handle(new AddBranchCommandInputs("Stack1", null)); | ||
|
||
// Assert | ||
inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any<string[]>()); | ||
stacks.Should().BeEquivalentTo(new List<Config.Stack> | ||
{ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenStackNameProvided_ButStackDoesNotExist_Throws() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
|
||
// Act and assert | ||
var invalidStackName = Some.Name(); | ||
await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(invalidStackName, null))) | ||
.Should() | ||
.ThrowAsync<InvalidOperationException>() | ||
.WithMessage($"Stack '{invalidStackName}' not found."); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenBranchNameProvided_DoesNotAskForBranchName_AddsBranchFromStack() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
gitOperations.DoesLocalBranchExist("branch-5").Returns(true); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
stackConfig | ||
.WhenForAnyArgs(s => s.Save(Arg.Any<List<Config.Stack>>())) | ||
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0)); | ||
|
||
inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1"); | ||
|
||
// Act | ||
await handler.Handle(new AddBranchCommandInputs(null, "branch-5")); | ||
|
||
// Assert | ||
stacks.Should().BeEquivalentTo(new List<Config.Stack> | ||
{ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
}); | ||
inputProvider.DidNotReceive().Select(Questions.SelectBranch, Arg.Any<string[]>()); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenBranchNameProvided_ButBranchDoesNotExistLocally_Throws() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
gitOperations.DoesLocalBranchExist("branch-5").Returns(false); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
|
||
inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1"); | ||
|
||
// Act and assert | ||
var invalidBranchName = Some.Name(); | ||
await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(null, invalidBranchName))) | ||
.Should() | ||
.ThrowAsync<InvalidOperationException>() | ||
.WithMessage($"Branch '{invalidBranchName}' does not exist locally."); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenBranchNameProvided_ButBranchAlreadyExistsInStack_Throws() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
gitOperations.DoesLocalBranchExist("branch-5").Returns(true); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
|
||
inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1"); | ||
|
||
// Act and assert | ||
await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(null, "branch-5"))) | ||
.Should() | ||
.ThrowAsync<InvalidOperationException>() | ||
.WithMessage($"Branch 'branch-5' already exists in stack 'Stack1'."); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenAllInputsProvided_DoesNotAskForAnything_AddsBranchFromStack() | ||
{ | ||
// Arrange | ||
var gitOperations = Substitute.For<IGitOperations>(); | ||
var stackConfig = Substitute.For<IStackConfig>(); | ||
var inputProvider = Substitute.For<IInputProvider>(); | ||
var outputProvider = Substitute.For<IOutputProvider>(); | ||
var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); | ||
|
||
var remoteUri = Some.HttpsUri().ToString(); | ||
|
||
gitOperations.GetRemoteUri().Returns(remoteUri); | ||
gitOperations.GetCurrentBranch().Returns("branch-1"); | ||
gitOperations.DoesLocalBranchExist("branch-5").Returns(true); | ||
|
||
var stacks = new List<Config.Stack>( | ||
[ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
]); | ||
stackConfig.Load().Returns(stacks); | ||
stackConfig | ||
.WhenForAnyArgs(s => s.Save(Arg.Any<List<Config.Stack>>())) | ||
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0)); | ||
|
||
// Act | ||
await handler.Handle(new AddBranchCommandInputs("Stack1", "branch-5")); | ||
|
||
// Assert | ||
stacks.Should().BeEquivalentTo(new List<Config.Stack> | ||
{ | ||
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), | ||
new("Stack2", remoteUri, "branch-2", ["branch-4"]) | ||
}); | ||
inputProvider.ReceivedCalls().Should().BeEmpty(); | ||
} | ||
} |
Oops, something went wrong.