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
55 changes: 46 additions & 9 deletions src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Stack.Config;
using Stack.Git;
using Stack.Infrastructure;
using Stack.Infrastructure.Settings;
using Stack.Tests.Helpers;
using Xunit.Abstractions;

Expand Down Expand Up @@ -42,7 +43,11 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndParentBranchAndCo
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Select(Questions.SelectBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(branchToAdd);
Expand Down Expand Up @@ -83,7 +88,11 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_AddsBranchFromSta
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(branchToAdd);
inputProvider.Select(Questions.SelectParentBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(anotherBranch);
Expand Down Expand Up @@ -123,7 +132,11 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_AddsBranchFromSt
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(branchToAdd);
inputProvider.Select(Questions.SelectParentBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(anotherBranch);
Expand Down Expand Up @@ -163,7 +176,11 @@ public async Task WhenStackNameProvided_ButStackDoesNotExist_Throws()
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

// Act and assert
var invalidStackName = Some.Name();
Expand Down Expand Up @@ -197,7 +214,11 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_AddsBranchFromS
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Select(Questions.SelectParentBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(anotherBranch);
Expand Down Expand Up @@ -238,7 +259,11 @@ public async Task WhenBranchNameProvided_ButBranchDoesNotExistLocally_Throws()
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");

Expand Down Expand Up @@ -277,7 +302,11 @@ public async Task WhenBranchNameProvided_ButBranchAlreadyExistsInStack_Throws()
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");

Expand Down Expand Up @@ -312,7 +341,11 @@ public async Task WhenAllInputsProvided_DoesNotAskForAnything_AddsBranchFromStac
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetLocalBranchesOrderedByMostRecentCommitterDate().Returns(new[] { sourceBranch, anotherBranch, branchToAdd });
gitClient.DoesLocalBranchExist(branchToAdd).Returns(true);
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

// Act
await handler.Handle(new AddBranchCommandInputs("Stack1", branchToAdd, anotherBranch), CancellationToken.None);
Expand Down Expand Up @@ -354,7 +387,11 @@ public async Task WhenParentBranchProvided_DoesNotAskForParentBranch_CreatesNewB
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new AddBranchCommandHandler(inputProvider, logger, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Select(Questions.SelectBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(branchToAdd);
Expand Down
61 changes: 51 additions & 10 deletions src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Stack.Config;
using Stack.Git;
using Stack.Infrastructure;
using Stack.Infrastructure.Settings;
using Stack.Tests.Helpers;
using Xunit.Abstractions;

Expand Down Expand Up @@ -38,7 +39,11 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndParentBranch_Crea
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Text(Questions.BranchName, Arg.Any<CancellationToken>(), Arg.Any<string>()).Returns(newBranch);
Expand Down Expand Up @@ -85,7 +90,11 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_CreatesAndAddsBra
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Text(Questions.BranchName, Arg.Any<CancellationToken>(), Arg.Any<string>()).Returns(newBranch);
inputProvider.Select(Questions.SelectParentBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(anotherBranch);
Expand Down Expand Up @@ -127,7 +136,11 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_CreatesAndAddsBr
.WithSourceBranch(sourceBranch)
.WithBranch(branch => branch.WithName(anotherBranch)))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Text(Questions.BranchName, Arg.Any<CancellationToken>(), Arg.Any<string>()).Returns(newBranch);
inputProvider.Select(Questions.SelectParentBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(anotherBranch);
Expand Down Expand Up @@ -172,7 +185,11 @@ public async Task WhenStackNameProvided_ButStackDoesNotExist_Throws()
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

gitClient.GetRemoteUri().Returns(remoteUri);
gitClient.GetCurrentBranch().Returns(sourceBranch);
Expand Down Expand Up @@ -210,7 +227,11 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_CreatesAndAddsB
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Select(Questions.SelectParentBranch, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns(anotherBranch);
Expand Down Expand Up @@ -254,7 +275,11 @@ public async Task WhenBranchNameProvided_ButBranchAlreadyExistLocally_Throws()
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
gitClient.GetRemoteUri().Returns(remoteUri);
Expand Down Expand Up @@ -295,7 +320,11 @@ public async Task WhenBranchNameProvided_ButBranchAlreadyExistsInStack_Throws()
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
gitClient.GetRemoteUri().Returns(remoteUri);
Expand Down Expand Up @@ -334,7 +363,11 @@ public async Task WhenPushToTheRemoteFails_StillCreatesTheBranchLocallyAndAddsIt
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

gitClient.When(gc => gc.PushNewBranch(newBranch)).Do(_ => { throw new Exception("Failed to push branch"); });

Expand Down Expand Up @@ -385,7 +418,11 @@ public async Task WhenParentBranchNotProvided_AsksForParentBranch_CreatesNewBran
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Text(Questions.BranchName, Arg.Any<CancellationToken>(), Arg.Any<string>()).Returns(newBranch);
Expand Down Expand Up @@ -433,7 +470,11 @@ public async Task WhenParentBranchProvided_DoesNotAskForParentBranch_CreatesNewB
.WithRemoteUri(remoteUri)
.WithSourceBranch(sourceBranch))
.Build();
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClient, stackConfig);
var gitClientFactory = Substitute.For<IGitClientFactory>();
var executionContext = new CliExecutionContext { WorkingDirectory = "/some/path" };
var handler = new NewBranchCommandHandler(inputProvider, logger, displayProvider, gitClientFactory, executionContext, stackConfig);

gitClientFactory.Create(executionContext.WorkingDirectory).Returns(gitClient);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1");
inputProvider.Text(Questions.BranchName, Arg.Any<CancellationToken>(), Arg.Any<string>()).Returns(newBranch);
Expand Down
Loading
Loading