Skip to content
Merged
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
78 changes: 45 additions & 33 deletions src/Stack.Tests/Commands/Stack/StackSwitchCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IInputProvider>();
var logger = new TestLogger(testOutputHelper);
var gitClient = new GitClient(logger, repo.GitClientSettings);
var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig);
var gitClient = Substitute.For<IGitClient>();
var currentBranch = sourceBranch;
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetRemoteUri().Returns(remoteUri);
gitClient.GetBranchesThatExistLocally(Arg.Any<string[]>()).Returns([sourceBranch, anotherBranch, branchToSwitchTo]);
gitClient.DoesLocalBranchExist(branchToSwitchTo).Returns(true);
gitClient
.When(g => g.ChangeBranch(Arg.Any<string>()))
.Do(ci => currentBranch = ci.Arg<string>());

gitClient.ChangeBranch(sourceBranch);
var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig);

inputProvider.SelectGrouped(Questions.SelectBranch, Arg.Any<ChoiceGroup<string>[]>()).Returns(branchToSwitchTo);
inputProvider
.SelectGrouped(Questions.SelectBranch, Arg.Any<ChoiceGroup<string>[]>())
.Returns(branchToSwitchTo);

// Act
await handler.Handle(new StackSwitchCommandInputs(null));

// Assert
gitClient.GetCurrentBranch().Should().Be(branchToSwitchTo);
currentBranch.Should().Be(branchToSwitchTo);
}

[Fact]
Expand All @@ -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<IInputProvider>();
var logger = new TestLogger(testOutputHelper);
var gitClient = new GitClient(logger, repo.GitClientSettings);
var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig);
var gitClient = Substitute.For<IGitClient>();
var currentBranch = sourceBranch;
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetRemoteUri().Returns(remoteUri);
gitClient.GetBranchesThatExistLocally(Arg.Any<string[]>()).Returns([sourceBranch, anotherBranch, branchToSwitchTo]);
gitClient.DoesLocalBranchExist(branchToSwitchTo).Returns(true);
gitClient
.When(g => g.ChangeBranch(Arg.Any<string>()))
.Do(ci => currentBranch = ci.Arg<string>());

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();
}

Expand All @@ -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<IInputProvider>();
var logger = new TestLogger(testOutputHelper);
var gitClient = new GitClient(logger, repo.GitClientSettings);
var handler = new StackSwitchCommandHandler(inputProvider, gitClient, stackConfig);
var gitClient = Substitute.For<IGitClient>();
var currentBranch = sourceBranch;
gitClient.GetCurrentBranch().Returns(sourceBranch);
gitClient.GetRemoteUri().Returns(remoteUri);
gitClient.GetBranchesThatExistLocally(Arg.Any<string[]>()).Returns([sourceBranch, anotherBranch, branchToSwitchTo]);
gitClient
.When(g => g.ChangeBranch(Arg.Any<string>()))
.Do(ci => currentBranch = ci.Arg<string>());

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<InvalidOperationException>()
.WithMessage($"Branch '{invalidBranchName}' does not exist.");
Expand Down
Loading