diff --git a/src/Stack.Tests/Commands/Branch/RemoveBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/RemoveBranchCommandHandlerTests.cs index ab667311..fbf031c0 100644 --- a/src/Stack.Tests/Commands/Branch/RemoveBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/RemoveBranchCommandHandlerTests.cs @@ -18,27 +18,27 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_RemovesB // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Confirm(Questions.ConfirmRemoveBranch).Returns(true); @@ -49,8 +49,8 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_RemovesB // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new Config.Stack("Stack1", repo.RemoteUri, sourceBranch, []), - new Config.Stack("Stack2", repo.RemoteUri, sourceBranch, []) + new Config.Stack("Stack1", remoteUri, sourceBranch, []), + new Config.Stack("Stack2", remoteUri, sourceBranch, []) }); } @@ -60,27 +60,27 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_RemovesBranchFrom // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Confirm(Questions.ConfirmRemoveBranch).Returns(true); @@ -91,8 +91,8 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_RemovesBranchFrom inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, []), - new("Stack2", repo.RemoteUri, sourceBranch, []) + new("Stack1", remoteUri, sourceBranch, []), + new("Stack2", remoteUri, sourceBranch, []) }); } @@ -102,27 +102,27 @@ public async Task WhenStackNameProvided_ButStackDoesNotExist_Throws() // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + // Act and assert var invalidStackName = Some.Name(); await handler.Invoking(async h => await h.Handle(new RemoveBranchCommandInputs(invalidStackName, null, false))) @@ -137,27 +137,27 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_RemovesBranchFr // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Confirm(Questions.ConfirmRemoveBranch).Returns(true); @@ -167,8 +167,8 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_RemovesBranchFr // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, []), - new("Stack2", repo.RemoteUri, sourceBranch, []) + new("Stack1", remoteUri, sourceBranch, []), + new("Stack2", remoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Select(Questions.SelectBranch, Arg.Any()); } @@ -179,27 +179,27 @@ public async Task WhenBranchNameProvided_ButBranchDoesNotExistInStack_Throws() // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act and assert @@ -216,23 +216,23 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName() // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Confirm(Questions.ConfirmRemoveBranch).Returns(true); @@ -242,7 +242,7 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName() // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, []) + new("Stack1", remoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); @@ -254,27 +254,27 @@ public async Task WhenConfirmProvided_DoesNotAskForConfirmation_RemovesBranchFro // Arrange var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); @@ -284,8 +284,8 @@ public async Task WhenConfirmProvided_DoesNotAskForConfirmation_RemovesBranchFro // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, []), - new("Stack2", repo.RemoteUri, sourceBranch, []) + new("Stack1", remoteUri, sourceBranch, []), + new("Stack2", remoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Confirm(Questions.ConfirmRemoveBranch); } @@ -297,25 +297,24 @@ public async Task WhenSchemaIsV2_AndChildActionIsMoveChildrenToParent_RemovesBra var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); var childBranch = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .WithBranch(childBranch) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithSchemaVersion(SchemaVersion.V2) .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove).WithChildBranch(b => b.WithName(childBranch)))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Select(Questions.RemoveBranchChildAction, Arg.Any(), Arg.Any>()) @@ -328,7 +327,7 @@ public async Task WhenSchemaIsV2_AndChildActionIsMoveChildrenToParent_RemovesBra // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, [new Config.Branch(childBranch, [])]) + new("Stack1", remoteUri, sourceBranch, [new Config.Branch(childBranch, [])]) }); } @@ -339,25 +338,24 @@ public async Task WhenSchemaIsV2_AndChildActionIsRemoveChildren_RemovesBranchAnd var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); var childBranch = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .WithBranch(childBranch) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithSchemaVersion(SchemaVersion.V2) .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove).WithChildBranch(b => b.WithName(childBranch)))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Select(Questions.RemoveBranchChildAction, Arg.Any(), Arg.Any>()) @@ -370,7 +368,7 @@ public async Task WhenSchemaIsV2_AndChildActionIsRemoveChildren_RemovesBranchAnd // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, []) + new("Stack1", remoteUri, sourceBranch, []) }); } @@ -381,25 +379,24 @@ public async Task WhenSchemaIsV1_RemovesBranchAndMovesChildrenToParent() var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); var childBranch = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .WithBranch(childBranch) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithSchemaVersion(SchemaVersion.V1) .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove).WithChildBranch(b => b.WithName(childBranch)))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Confirm(Questions.ConfirmRemoveBranch).Returns(true); @@ -410,7 +407,7 @@ public async Task WhenSchemaIsV1_RemovesBranchAndMovesChildrenToParent() // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, [new Config.Branch(childBranch, [])]) + new("Stack1", remoteUri, sourceBranch, [new Config.Branch(childBranch, [])]) }); } @@ -421,25 +418,24 @@ public async Task WhenSchemaIsV2_AndRemoveChildrenIsProvided_RemovesBranchAndDel var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); var childBranch = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .WithBranch(childBranch) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithSchemaVersion(SchemaVersion.V2) .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove).WithChildBranch(b => b.WithName(childBranch)))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Select(Questions.RemoveBranchChildAction, Arg.Any(), Arg.Any>()) @@ -452,7 +448,7 @@ public async Task WhenSchemaIsV2_AndRemoveChildrenIsProvided_RemovesBranchAndDel // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, []) + new("Stack1", remoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Select(Questions.RemoveBranchChildAction, Arg.Any(), Arg.Any>()); @@ -465,25 +461,24 @@ public async Task WhenSchemaIsV2_AndMoveChildrenToParentIsProvided_RemovesBranch var sourceBranch = Some.BranchName(); var branchToRemove = Some.BranchName(); var childBranch = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToRemove) - .WithBranch(childBranch) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithSchemaVersion(SchemaVersion.V2) .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(branchToRemove).WithChildBranch(b => b.WithName(childBranch)))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new RemoveBranchCommandHandler(inputProvider, logger, gitClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToRemove); inputProvider.Select(Questions.RemoveBranchChildAction, Arg.Any(), Arg.Any>()) @@ -496,7 +491,7 @@ public async Task WhenSchemaIsV2_AndMoveChildrenToParentIsProvided_RemovesBranch // Assert stackConfig.Stacks.Should().BeEquivalentTo(new List { - new("Stack1", repo.RemoteUri, sourceBranch, [new Config.Branch(childBranch, [])]) + new("Stack1", remoteUri, sourceBranch, [new Config.Branch(childBranch, [])]) }); inputProvider.DidNotReceive().Select(Questions.RemoveBranchChildAction, Arg.Any(), Arg.Any>()); diff --git a/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs b/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs index c81729cd..1056deb5 100644 --- a/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/PullRequests/OpenPullRequestsCommandHandlerTests.cs @@ -19,31 +19,29 @@ public async Task WhenThereAreMultiplePullRequestsInAStack_OpensAllPullRequests( 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 remoteUri = Some.HttpsUri().ToString(); var gitHubClient = Substitute.For(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branch1)) .WithBranch(branch => branch.WithName(branch2))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); var fileOperations = Substitute.For(); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new OpenPullRequestsCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false, branch1); @@ -71,31 +69,29 @@ public async Task WhenThereAreSomePullRequestsInAStack_OpensAllPullRequests() 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 remoteUri = Some.HttpsUri().ToString(); var gitHubClient = Substitute.For(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branch1)) .WithBranch(branch => branch.WithName(branch2))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); var fileOperations = Substitute.For(); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new OpenPullRequestsCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false, branch1); @@ -120,31 +116,29 @@ public async Task WhenStackNameIsProvided_OpensAllPullRequestsForTheStack() 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 remoteUri = Some.HttpsUri().ToString(); var gitHubClient = Substitute.For(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branch1)) .WithBranch(branch => branch.WithName(branch2))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); var fileOperations = Substitute.For(); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new OpenPullRequestsCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false, branch1); gitHubClient .GetPullRequest(branch1) @@ -170,17 +164,13 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_OpensAllPullRequ 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 remoteUri = Some.HttpsUri().ToString(); var gitHubClient = Substitute.For(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branch1)) .WithBranch(branch => branch.WithName(branch2))) @@ -188,9 +178,11 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_OpensAllPullRequ var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); var fileOperations = Substitute.For(); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new OpenPullRequestsCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + var prForBranch1 = new GitHubPullRequest(1, "PR Title", string.Empty, GitHubPullRequestStates.Open, Some.HttpsUri(), false, branch1); gitHubClient .GetPullRequest(branch1) @@ -216,31 +208,29 @@ public async Task WhenStackNameIsProvided_ButItStackDoesNotExist_Throws() 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 remoteUri = Some.HttpsUri().ToString(); var gitHubClient = Substitute.For(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branch1)) .WithBranch(branch => branch.WithName(branch2))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); var fileOperations = Substitute.For(); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var handler = new OpenPullRequestsCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + // Act and assert var invalidStackName = Some.Name(); await handler.Invoking(h => h.Handle(new OpenPullRequestsCommandInputs(invalidStackName))) diff --git a/src/Stack.Tests/Commands/Stack/CleanupStackCommandHandlerTests.cs b/src/Stack.Tests/Commands/Stack/CleanupStackCommandHandlerTests.cs index dc9da89e..2b1ff85a 100644 --- a/src/Stack.Tests/Commands/Stack/CleanupStackCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Stack/CleanupStackCommandHandlerTests.cs @@ -19,38 +19,46 @@ public async Task WhenBranchExistsLocally_ButHasNotBeenPushedToTheRemote_BranchI var sourceBranch = Some.BranchName(); var branchWithoutRemoteTracking = Some.BranchName(); var branchToKeep = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch, true) - .WithBranch(branchWithoutRemoteTracking, false) - .WithBranch(branchToKeep, true) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchWithoutRemoteTracking)) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branch without remote tracking should not be cleaned up + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchWithoutRemoteTracking] = new(branchWithoutRemoteTracking, null, false, false, 0, 0, new Commit("def456", "local commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("ghi789", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); // Act await handler.Handle(CleanupStackCommandInputs.Empty); - // Assert - repo.GetBranches().Should().Contain(b => b.FriendlyName == branchWithoutRemoteTracking); + // Assert - branch without remote tracking should not be deleted + gitClient.DidNotReceive().DeleteLocalBranch(branchWithoutRemoteTracking); } [Fact] @@ -60,33 +68,37 @@ public async Task WhenBranchExistsLocally_AndHasBeenDeletedFromTheRemote_BranchI var sourceBranch = Some.BranchName(); var branchToCleanup = Some.BranchName(); var branchToKeep = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch, true) - .WithBranch(branchToCleanup, true) - .WithBranch(branchToKeep, true) - .Build(); - - repo.DeleteRemoteTrackingBranch(branchToCleanup); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToCleanup)) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branchToCleanup has been deleted from remote + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchToCleanup] = new(branchToCleanup, $"origin/{branchToCleanup}", false, false, 0, 0, new Commit("def456", "cleanup commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("ghi789", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); @@ -95,7 +107,7 @@ public async Task WhenBranchExistsLocally_AndHasBeenDeletedFromTheRemote_BranchI await handler.Handle(CleanupStackCommandInputs.Empty); // Assert - repo.GetBranches().Should().NotContain(b => b.FriendlyName == branchToCleanup); + gitClient.Received().DeleteLocalBranch(branchToCleanup); } [Fact] @@ -105,30 +117,38 @@ public async Task WhenBranchExistsLocally_AndInRemote_BranchIsNotDeletedLocally( var sourceBranch = Some.BranchName(); var branchToKeep = Some.BranchName(); var anotherBranchToKeep = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch, true) - .WithBranch(branchToKeep, true) - .WithBranch(anotherBranchToKeep, true) - .Build(); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToKeep)) .WithBranch(branch => branch.WithName(anotherBranchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - all branches exist in remote + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("def456", "keep commit")), + [anotherBranchToKeep] = new(anotherBranchToKeep, $"origin/{anotherBranchToKeep}", true, false, 0, 0, new Commit("ghi789", "another keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); @@ -136,7 +156,8 @@ public async Task WhenBranchExistsLocally_AndInRemote_BranchIsNotDeletedLocally( await handler.Handle(CleanupStackCommandInputs.Empty); // Assert - repo.GetBranches().Should().Contain(b => b.FriendlyName == anotherBranchToKeep); + gitClient.DidNotReceive().DeleteLocalBranch(branchToKeep); + gitClient.DidNotReceive().DeleteLocalBranch(anotherBranchToKeep); } [Fact] @@ -146,30 +167,38 @@ public async Task WhenConfirmationIsFalse_DoesNotDeleteAnyBranches() 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 remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToCleanup)) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branchToCleanup has remote tracking but remote branch was deleted + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchToCleanup] = new(branchToCleanup, $"origin/{branchToCleanup}", false, false, 0, 0, new Commit("def456", "cleanup commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("ghi789", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(false); @@ -177,7 +206,7 @@ public async Task WhenConfirmationIsFalse_DoesNotDeleteAnyBranches() await handler.Handle(CleanupStackCommandInputs.Empty); // Assert - gitClient.GetBranchesThatExistLocally([branchToKeep, branchToCleanup]).Should().BeEquivalentTo([branchToKeep, branchToCleanup]); + gitClient.DidNotReceive().DeleteLocalBranch(branchToCleanup); } [Fact] @@ -187,30 +216,38 @@ public async Task WhenStackNameIsProvided_ItIsNotAskedFor() 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 remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToCleanup)) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branchToCleanup has remote tracking but remote branch was deleted + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchToCleanup] = new(branchToCleanup, $"origin/{branchToCleanup}", false, false, 0, 0, new Commit("def456", "cleanup commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("ghi789", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); + inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); // Act @@ -227,30 +264,29 @@ public async Task WhenStackNameIsProvided_ButStackDoesNotExist_Throws() 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 remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToCleanup)) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); // Act and assert @@ -268,26 +304,34 @@ public async Task WhenOnlyASingleStackExists_StackIsSelectedAutomatically() 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 remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToCleanup)) .WithBranch(branch => branch.WithName(branchToKeep))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branchToCleanup has remote tracking but remote branch was deleted + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchToCleanup] = new(branchToCleanup, $"origin/{branchToCleanup}", false, false, 0, 0, new Commit("def456", "cleanup commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("ghi789", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); + inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); // Act @@ -304,32 +348,38 @@ public async Task WhenConfirmIsProvided_DoesNotAskForConfirmation_DeletesBranche var sourceBranch = Some.BranchName(); var branchToCleanup = Some.BranchName(); var branchToKeep = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch, true) - .WithBranch(branchToCleanup, true) - .WithBranch(branchToKeep, true) - .Build(); - - repo.DeleteRemoteTrackingBranch(branchToCleanup); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(branchToCleanup)) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branchToCleanup has been deleted from remote + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [branchToCleanup] = new(branchToCleanup, $"origin/{branchToCleanup}", false, false, 0, 0, new Commit("def456", "cleanup commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("ghi789", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); + inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act @@ -337,7 +387,7 @@ public async Task WhenConfirmIsProvided_DoesNotAskForConfirmation_DeletesBranche // Assert inputProvider.DidNotReceive().Confirm(Questions.ConfirmDeleteBranches); - repo.GetBranches().Should().NotContain(b => b.FriendlyName == branchToCleanup); + gitClient.Received().DeleteLocalBranch(branchToCleanup); } [Fact] @@ -348,34 +398,38 @@ public async Task WhenChildBranchExistsLocally_AndHasBeenDeletedFromTheRemote_Br var parentBranch = Some.BranchName(); var branchToCleanup = Some.BranchName(); var branchToKeep = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch, true) - .WithBranch(parentBranch, true) - .WithBranch(branchToCleanup, true) - .WithBranch(branchToKeep, true) - .Build(); - - repo.DeleteRemoteTrackingBranch(branchToCleanup); + var remoteUri = Some.HttpsUri().ToString(); var stackConfig = new TestStackConfigBuilder() .WithStack(stack => stack .WithName("Stack1") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(branch => branch.WithName(parentBranch).WithChildBranch(b => b.WithName(branchToCleanup))) .WithBranch(branch => branch.WithName(branchToKeep))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch)) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); + var gitClient = Substitute.For(); var gitHubClient = Substitute.For(); var handler = new CleanupStackCommandHandler(inputProvider, logger, gitClient, gitHubClient, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetCurrentBranch().Returns(sourceBranch); + + // Setup branch statuses - branchToCleanup has been deleted from remote + var branchStatuses = new Dictionary + { + [sourceBranch] = new(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit("abc123", "source commit")), + [parentBranch] = new(parentBranch, $"origin/{parentBranch}", true, false, 0, 0, new Commit("def456", "parent commit")), + [branchToCleanup] = new(branchToCleanup, $"origin/{branchToCleanup}", false, false, 0, 0, new Commit("ghi789", "cleanup commit")), + [branchToKeep] = new(branchToKeep, $"origin/{branchToKeep}", true, false, 0, 0, new Commit("jkl012", "keep commit")) + }; + gitClient.GetBranchStatuses(Arg.Any()).Returns(branchStatuses); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); inputProvider.Confirm(Questions.ConfirmDeleteBranches).Returns(true); @@ -384,6 +438,6 @@ public async Task WhenChildBranchExistsLocally_AndHasBeenDeletedFromTheRemote_Br await handler.Handle(CleanupStackCommandInputs.Empty); // Assert - repo.GetBranches().Should().NotContain(b => b.FriendlyName == branchToCleanup); + gitClient.Received().DeleteLocalBranch(branchToCleanup); } }