diff --git a/src/Stack.Tests/Commands/Stack/StackSwitchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Stack/StackSwitchCommandHandlerTests.cs index 111eef13..7d10dcf4 100644 --- a/src/Stack.Tests/Commands/Stack/StackSwitchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Stack/StackSwitchCommandHandlerTests.cs @@ -19,37 +19,42 @@ public async Task WhenNoInputsAreProvided_AsksForBranch_ChangesToBranch() var sourceBranch = Some.BranchName(); var anotherBranch = Some.BranchName(); var branchToSwitchTo = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToSwitchTo) - .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(branchToSwitchTo))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(anotherBranch))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); - var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig); + var gitClient = Substitute.For(); + var currentBranch = sourceBranch; + gitClient.GetCurrentBranch().Returns(sourceBranch); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetBranchesThatExistLocally(Arg.Any()).Returns([sourceBranch, anotherBranch, branchToSwitchTo]); + gitClient.DoesLocalBranchExist(branchToSwitchTo).Returns(true); + gitClient + .When(g => g.ChangeBranch(Arg.Any())) + .Do(ci => currentBranch = ci.Arg()); - gitClient.ChangeBranch(sourceBranch); + var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig); - inputProvider.SelectGrouped(Questions.SelectBranch, Arg.Any[]>()).Returns(branchToSwitchTo); + inputProvider + .SelectGrouped(Questions.SelectBranch, Arg.Any[]>()) + .Returns(branchToSwitchTo); // Act await handler.Handle(new StackSwitchCommandInputs(null)); // Assert - gitClient.GetCurrentBranch().Should().Be(branchToSwitchTo); + currentBranch.Should().Be(branchToSwitchTo); } [Fact] @@ -59,35 +64,38 @@ public async Task WhenBranchIsProvided_DoesNotAskForBranch_ChangesToBranch() var sourceBranch = Some.BranchName(); var anotherBranch = Some.BranchName(); var branchToSwitchTo = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToSwitchTo) - .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(branchToSwitchTo))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(anotherBranch))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); - var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig); + var gitClient = Substitute.For(); + var currentBranch = sourceBranch; + gitClient.GetCurrentBranch().Returns(sourceBranch); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetBranchesThatExistLocally(Arg.Any()).Returns([sourceBranch, anotherBranch, branchToSwitchTo]); + gitClient.DoesLocalBranchExist(branchToSwitchTo).Returns(true); + gitClient + .When(g => g.ChangeBranch(Arg.Any())) + .Do(ci => currentBranch = ci.Arg()); - gitClient.ChangeBranch(sourceBranch); + var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig); // Act await handler.Handle(new StackSwitchCommandInputs(branchToSwitchTo)); // Assert - gitClient.GetCurrentBranch().Should().Be(branchToSwitchTo); + currentBranch.Should().Be(branchToSwitchTo); inputProvider.ReceivedCalls().Should().BeEmpty(); } @@ -98,32 +106,36 @@ public async Task WhenBranchIsProvided_AndBranchDoesNotExist_Throws() var sourceBranch = Some.BranchName(); var anotherBranch = Some.BranchName(); var branchToSwitchTo = Some.BranchName(); - using var repo = new TestGitRepositoryBuilder() - .WithBranch(sourceBranch) - .WithBranch(branchToSwitchTo) - .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(branchToSwitchTo))) .WithStack(stack => stack .WithName("Stack2") - .WithRemoteUri(repo.RemoteUri) + .WithRemoteUri(remoteUri) .WithSourceBranch(sourceBranch) .WithBranch(b => b.WithName(anotherBranch))) .Build(); var inputProvider = Substitute.For(); var logger = new TestLogger(testOutputHelper); - var gitClient = new GitClient(logger, repo.GitClientSettings); - var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig); + var gitClient = Substitute.For(); + var currentBranch = sourceBranch; + gitClient.GetCurrentBranch().Returns(sourceBranch); + gitClient.GetRemoteUri().Returns(remoteUri); + gitClient.GetBranchesThatExistLocally(Arg.Any()).Returns([sourceBranch, anotherBranch, branchToSwitchTo]); + gitClient + .When(g => g.ChangeBranch(Arg.Any())) + .Do(ci => currentBranch = ci.Arg()); - gitClient.ChangeBranch(sourceBranch); + var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig); // Act and assert var invalidBranchName = Some.BranchName(); + gitClient.DoesLocalBranchExist(invalidBranchName).Returns(false); + await handler.Invoking(h => h.Handle(new StackSwitchCommandInputs(invalidBranchName))) .Should().ThrowAsync() .WithMessage($"Branch '{invalidBranchName}' does not exist.");