From 95c886f24bef866bdcddedd9df4dedc3da1f4b1c Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Thu, 19 Dec 2024 19:05:03 +1100 Subject: [PATCH] Use proper Git repository for open pull requests tests (#158) --- .../OpenPullRequestsCommandHandlerTests.cs | 175 ++++++++++-------- 1 file changed, 95 insertions(+), 80 deletions(-) diff --git a/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs b/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs index 02f2a5b..81a2e7c 100644 --- a/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs @@ -7,7 +7,7 @@ using Stack.Infrastructure; using Stack.Tests.Helpers; -namespace Stack.Tests.Commands.Stack; +namespace Stack.Tests.Commands.PullRequests; public class OpenPullRequestsCommandHandlerTests { @@ -15,22 +15,27 @@ public class OpenPullRequestsCommandHandlerTests public async Task WhenThereAreMultiplePullRequestsInAStack_OpensAllPullRequests() { // Arrange - var gitOperations = Substitute.For(); + 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 gitHubOperations = Substitute.For(); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var fileOperations = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new OpenPullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [branch1, branch2]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -39,44 +44,49 @@ public async Task WhenThereAreMultiplePullRequestsInAStack_OpensAllPullRequests( inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); - var prForBranch3 = new GitHubPullRequest(1, "PR Title for branch-3", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-3") - .Returns(prForBranch3); + .GetPullRequest(branch1) + .Returns(prForBranch1); - var prForBranch5 = new GitHubPullRequest(2, "PR Title for branch-5", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch2 = new GitHubPullRequest(2, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-5") - .Returns(prForBranch5); + .GetPullRequest(branch2) + .Returns(prForBranch2); // Act await handler.Handle(OpenPullRequestsCommandInputs.Empty); // Assert - gitHubOperations.Received().OpenPullRequest(prForBranch3); - gitHubOperations.Received().OpenPullRequest(prForBranch5); + gitHubOperations.Received().OpenPullRequest(prForBranch1); + gitHubOperations.Received().OpenPullRequest(prForBranch2); } [Fact] public async Task WhenThereAreSomePullRequestsInAStack_OpensAllPullRequests() { // Arrange - var gitOperations = Substitute.For(); + 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 gitHubOperations = Substitute.For(); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var fileOperations = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new OpenPullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [branch1, branch2]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -85,135 +95,140 @@ public async Task WhenThereAreSomePullRequestsInAStack_OpensAllPullRequests() inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); - var prForBranch3 = new GitHubPullRequest(1, "PR Title for branch-3", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-3") - .Returns(prForBranch3); + .GetPullRequest(branch1) + .Returns(prForBranch1); - var prForBranch5 = new GitHubPullRequest(2, "PR Title for branch-5", string.Empty, GitHubPullRequestStates.Closed, Some.HttpsUri(), false); + var prForBranch2 = new GitHubPullRequest(2, "PR Title", string.Empty, GitHubPullRequestStates.Closed, Some.HttpsUri(), false); // Act await handler.Handle(OpenPullRequestsCommandInputs.Empty); // Assert - gitHubOperations.Received().OpenPullRequest(prForBranch3); - gitHubOperations.DidNotReceive().OpenPullRequest(prForBranch5); + gitHubOperations.Received().OpenPullRequest(prForBranch1); + gitHubOperations.DidNotReceive().OpenPullRequest(prForBranch2); } [Fact] public async Task WhenStackNameIsProvided_OpensAllPullRequestsForTheStack() { // Arrange - var gitOperations = Substitute.For(); + 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 gitHubOperations = Substitute.For(); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var fileOperations = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new OpenPullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [branch1, branch2]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); - var prForBranch3 = new GitHubPullRequest(1, "PR Title for branch-3", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-3") - .Returns(prForBranch3); + .GetPullRequest(branch1) + .Returns(prForBranch1); - var prForBranch5 = new GitHubPullRequest(2, "PR Title for branch-5", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch2 = new GitHubPullRequest(2, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-5") - .Returns(prForBranch5); + .GetPullRequest(branch2) + .Returns(prForBranch2); // Act await handler.Handle(new OpenPullRequestsCommandInputs("Stack1")); // Assert - gitHubOperations.Received().OpenPullRequest(prForBranch3); - gitHubOperations.Received().OpenPullRequest(prForBranch5); + gitHubOperations.Received().OpenPullRequest(prForBranch1); + gitHubOperations.Received().OpenPullRequest(prForBranch2); } [Fact] public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_OpensAllPullRequestsForTheStack() { // Arrange - var gitOperations = Substitute.For(); + 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 gitHubOperations = Substitute.For(); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var fileOperations = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new OpenPullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]) + new("Stack1", repo.RemoteUri, sourceBranch, [branch1, branch2]) ]); stackConfig.Load().Returns(stacks); - var prForBranch3 = new GitHubPullRequest(1, "PR Title for branch-3", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-3") - .Returns(prForBranch3); + .GetPullRequest(branch1) + .Returns(prForBranch1); - var prForBranch5 = new GitHubPullRequest(2, "PR Title for branch-5", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); + var prForBranch2 = new GitHubPullRequest(2, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); gitHubOperations - .GetPullRequest("branch-5") - .Returns(prForBranch5); + .GetPullRequest(branch2) + .Returns(prForBranch2); // Act await handler.Handle(OpenPullRequestsCommandInputs.Empty); // Assert - gitHubOperations.Received().OpenPullRequest(prForBranch3); - gitHubOperations.Received().OpenPullRequest(prForBranch5); + gitHubOperations.Received().OpenPullRequest(prForBranch1); + gitHubOperations.Received().OpenPullRequest(prForBranch2); } [Fact] public async Task WhenStackNameIsProvided_ButItStackDoesNotExist_Throws() { // Arrange - var gitOperations = Substitute.For(); + 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 gitHubOperations = Substitute.For(); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var fileOperations = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new OpenPullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [branch1, branch2]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); - var prForBranch3 = new GitHubPullRequest(1, "PR Title for branch-3", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); - gitHubOperations - .GetPullRequest("branch-3") - .Returns(prForBranch3); - - var prForBranch5 = new GitHubPullRequest(2, "PR Title for branch-5", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false); - gitHubOperations - .GetPullRequest("branch-5") - .Returns(prForBranch5); - // Act and assert var invalidStackName = Some.Name(); await handler.Invoking(h => h.Handle(new OpenPullRequestsCommandInputs(invalidStackName)))