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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Stack.Git;
using Stack.Infrastructure;
using Stack.Tests.Helpers;
using static Stack.Commands.CreatePullRequestsCommandHandler;

namespace Stack.Tests.Commands.PullRequests;

Expand Down Expand Up @@ -47,7 +48,9 @@ public async Task WhenNoPullRequestsExistForAStackWithMultipleBranches_CreatesPu
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch), new PullRequestCreateAction(branch2, branch1)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");

Expand Down Expand Up @@ -105,7 +108,9 @@ public async Task WhenCreatingPullRequestsForAStackWithMultipleBranches_EachPull
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch), new PullRequestCreateAction(branch2, branch1)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");
inputProvider.Text(Questions.PullRequestStackDescription, Arg.Any<string>()).Returns("A custom description");
Expand Down Expand Up @@ -179,7 +184,9 @@ public async Task WhenAPullRequestExistForABranch_AndNoneForAnotherBranch_Create
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(1)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch2, branch1)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");
inputProvider.Text(Questions.PullRequestStackDescription, Arg.Any<string>()).Returns("A custom description");
Expand Down Expand Up @@ -253,7 +260,9 @@ public async Task WhenStackNameIsProvided_PullRequestsAreCreatedForThatStack()
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch), new PullRequestCreateAction(branch2, branch1)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");

Expand Down Expand Up @@ -306,7 +315,9 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_PullRequestsAreC
]);
stackConfig.Load().Returns(stacks);

inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch), new PullRequestCreateAction(branch2, branch1)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");

Expand Down Expand Up @@ -404,7 +415,9 @@ public async Task WhenAPullRequestExistForABranch_AndHasBeenMerged_AndNoneForAno
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(1)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch2, sourceBranch)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");
inputProvider.Text(Questions.PullRequestStackDescription, Arg.Any<string>()).Returns("A custom description");
Expand Down Expand Up @@ -480,7 +493,9 @@ public async Task WhenAPullRequestTemplateExistsInTheRepo_ItIsUsedAsTheBodyOfANe
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(1)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");

Expand Down Expand Up @@ -535,7 +550,9 @@ public async Task WhenAPullRequestTemplateDoesNotExistInTheRepo_AnEmptyBodyIsUse
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(1)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");

Expand Down Expand Up @@ -589,7 +606,9 @@ public async Task WhenAskedWhetherToCreateAPullRequestAsADraft_AndTheAnswerIsYes
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch), new PullRequestCreateAction(branch2, branch1)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Confirm(Questions.CreatePullRequestAsDraft, false).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");
Expand All @@ -611,4 +630,59 @@ public async Task WhenAskedWhetherToCreateAPullRequestAsADraft_AndTheAnswerIsYes
gitHubClient.Received().CreatePullRequest(branch1, sourceBranch, "PR Title", Arg.Any<string>(), true);
gitHubClient.Received().CreatePullRequest(branch2, branch1, "PR Title", Arg.Any<string>(), true);
}

[Fact]
public async Task WhenOnlySelectingSomeBranchesToCreatePullRequestsFor_OnlyThosePullRequestsAreCreated()
{
// Arrange
var sourceBranch = Some.BranchName();
var branch1 = Some.BranchName();
var branch2 = Some.BranchName();
using var repo = new TestGitRepositoryBuilder()
.WithBranch(sourceBranch, true)
.WithBranch(branch1, true)
.WithBranch(branch2, true)
.Build();

var gitHubClient = Substitute.For<IGitHubClient>();
var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var fileOperations = Substitute.For<IFileOperations>();
var gitClient = new GitClient(outputProvider, repo.GitClientSettings);
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitClient, gitHubClient, fileOperations, stackConfig);

outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

var stacks = new List<Config.Stack>(
[
new("Stack1", repo.RemoteUri, sourceBranch, [branch1, branch2]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);
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
.MultiSelect(Questions.SelectPullRequestsToCreate, Arg.Any<PullRequestCreateAction[]>(), Arg.Any<Func<PullRequestCreateAction, string>>())
.Returns([new PullRequestCreateAction(branch1, sourceBranch)]);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle).Returns("PR Title");

var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false);
gitHubClient
.CreatePullRequest(branch1, sourceBranch, "PR Title", Arg.Any<string>(), false)
.Returns(prForBranch1);

// Act
await handler.Handle(CreatePullRequestsCommandInputs.Empty);

// Assert
gitHubClient.Received().CreatePullRequest(branch1, sourceBranch, "PR Title", Arg.Any<string>(), false);
gitHubClient.DidNotReceive().CreatePullRequest(branch2, branch1, Arg.Any<string>(), Arg.Any<string>(), false);
}
}
2 changes: 1 addition & 1 deletion src/Stack/Commands/Helpers/Questions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class Questions
public const string AddOrCreateBranch = "Add or create a branch:";
public const string ConfirmSwitchToBranch = "Do you want to switch to the new branch?";
public const string ConfirmPushBranch = "Do you want to push the new branch to the remote repository?";
public static string ConfirmStartCreatePullRequests(int numberOfBranchesWithoutPullRequests) => $"There {"are".ToQuantity(numberOfBranchesWithoutPullRequests, ShowQuantityAs.None)} {"branch".ToQuantity(numberOfBranchesWithoutPullRequests)} to create pull requests for. Do you want to continue?";
public const string SelectPullRequestsToCreate = "Select branches to create pull requests for:";
public const string ConfirmCreatePullRequests = "Are you sure you want to create pull requests for branches in this stack?";
public const string PullRequestTitle = "Title:";
public const string PullRequestStackDescription = "Stack description for pull request:";
Expand Down
Loading