From 45daefc72e5c4409af74e4ad0eb8222d302dadb8 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Thu, 19 Dec 2024 16:42:06 +1100 Subject: [PATCH] Use proper Git repository for delete stack tests (#147) --- .../Stack/DeleteStackCommandHandlerTests.cs | 150 ++++++++---------- 1 file changed, 69 insertions(+), 81 deletions(-) diff --git a/src/Stack.Tests/Commands/Stack/DeleteStackCommandHandlerTests.cs b/src/Stack.Tests/Commands/Stack/DeleteStackCommandHandlerTests.cs index bb2ace0..1e20364 100644 --- a/src/Stack.Tests/Commands/Stack/DeleteStackCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Stack/DeleteStackCommandHandlerTests.cs @@ -15,22 +15,20 @@ public class DeleteStackCommandHandlerTests public async Task WhenNoInputsAreProvided_AsksForName_AndConfirmation_AndDeletesStack() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -47,7 +45,7 @@ public async Task WhenNoInputsAreProvided_AsksForName_AndConfirmation_AndDeletes response.Should().Be(new DeleteStackCommandResponse("Stack1")); stacks.Should().BeEquivalentTo(new List { - new("Stack2", remoteUri, "branch-2", []) + new("Stack2", repo.RemoteUri, sourceBranch, []) }); } @@ -55,22 +53,20 @@ public async Task WhenNoInputsAreProvided_AsksForName_AndConfirmation_AndDeletes public async Task WhenConfirmationIsFalse_DoesNotDeleteStack() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -87,8 +83,8 @@ public async Task WhenConfirmationIsFalse_DoesNotDeleteStack() response.Should().Be(new DeleteStackCommandResponse(null)); stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); } @@ -96,22 +92,20 @@ public async Task WhenConfirmationIsFalse_DoesNotDeleteStack() public async Task WhenNameIsProvided_AsksForConfirmation_AndDeletesStack() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -127,7 +121,7 @@ public async Task WhenNameIsProvided_AsksForConfirmation_AndDeletesStack() response.Should().Be(new DeleteStackCommandResponse("Stack1")); stacks.Should().BeEquivalentTo(new List { - new("Stack2", remoteUri, "branch-2", []) + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); @@ -137,22 +131,20 @@ public async Task WhenNameIsProvided_AsksForConfirmation_AndDeletesStack() public async Task WhenForceIsProvided_DoesNotAskForConfirmation_AndDeletesStack() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -168,7 +160,7 @@ public async Task WhenForceIsProvided_DoesNotAskForConfirmation_AndDeletesStack( response.Should().Be(new DeleteStackCommandResponse("Stack1")); stacks.Should().BeEquivalentTo(new List { - new("Stack2", remoteUri, "branch-2", []) + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Confirm(Questions.ConfirmDeleteStack); @@ -178,22 +170,20 @@ public async Task WhenForceIsProvided_DoesNotAskForConfirmation_AndDeletesStack( public async Task WhenNameAndForceAreProvided_DoesNotAskForAnyInput_AndDeletesStack() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -207,7 +197,7 @@ public async Task WhenNameAndForceAreProvided_DoesNotAskForAnyInput_AndDeletesSt response.Should().Be(new DeleteStackCommandResponse("Stack1")); stacks.Should().BeEquivalentTo(new List { - new("Stack2", remoteUri, "branch-2", []) + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.ReceivedCalls().Should().BeEmpty(); @@ -217,22 +207,20 @@ public async Task WhenNameAndForceAreProvided_DoesNotAskForAnyInput_AndDeletesSt public async Task WhenStackDoesNotExist_Throws() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -252,24 +240,26 @@ await handler public async Task WhenThereAreLocalBranchesThatAreNotInTheRemote_AsksToCleanup_AndDeletesThemBeforeDeletingStack() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var branchToCleanup = Some.BranchName(); + var branchToKeep = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch, true) + .WithBranch(branchToCleanup, false) + .WithBranch(branchToKeep, true) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.GetBranchesThatExistLocally(Arg.Any()).Returns(["branch-1", "branch-3"]); - gitOperations.GetBranchesThatExistInRemote(Arg.Any()).Returns(["branch-1"]); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", []) + new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); @@ -288,30 +278,28 @@ public async Task WhenThereAreLocalBranchesThatAreNotInTheRemote_AsksToCleanup_A response.Should().Be(new DeleteStackCommandResponse("Stack1")); stacks.Should().BeEquivalentTo(new List { - new("Stack2", remoteUri, "branch-2", []) + new("Stack2", repo.RemoteUri, sourceBranch, []) }); - gitOperations.Received().DeleteLocalBranch("branch-3"); + gitOperations.GetBranchesThatExistLocally([branchToCleanup, branchToKeep]).Should().BeEquivalentTo([branchToKeep]); } [Fact] public async Task WhenOnlyOneStackExists_DoesNotAskForStackName() { // Arrange - var gitOperations = Substitute.For(); - var gitHubOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder().Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); + var gitHubOperations = Substitute.For(); var handler = new DeleteStackCommandHandler(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", []) + new("Stack1", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig