-
Notifications
You must be signed in to change notification settings - Fork 0
Add rename command
#366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add rename command
#366
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7acea21
Initial plan
Copilot ce422a1
Implement rename command with validation and tests
Copilot 87a95a0
Add README documentation for rename command
Copilot 035a2d5
Move Name option to RenameStackCommand and add test for cross-remote …
Copilot 82f290c
Use Questions.StackName instead of Questions.NewStackName for rename …
Copilot 6bd519b
Add no stacks found check to rename command
Copilot d8b8b74
Cleanup
geofflamrock c0ad3fb
More cleanup
geofflamrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
320 changes: 320 additions & 0 deletions
320
src/Stack.Tests/Commands/Stack/RenameStackCommandHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,320 @@ | ||
| using FluentAssertions; | ||
| using NSubstitute; | ||
| using Meziantou.Extensions.Logging.Xunit; | ||
| using Microsoft.Extensions.Logging; | ||
| using Stack.Commands; | ||
| using Stack.Commands.Helpers; | ||
| using Stack.Config; | ||
| using Stack.Git; | ||
| using Stack.Infrastructure; | ||
| using Stack.Tests.Helpers; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Stack.Tests.Commands.Stack; | ||
|
|
||
| public class RenameStackCommandHandlerTests(ITestOutputHelper testOutputHelper) | ||
| { | ||
| [Fact] | ||
| public async Task WhenNoInputsAreProvided_AsksForStackAndNewName_RenamesStack() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .WithStack(stack => stack | ||
| .WithName("Stack2") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1"); | ||
| inputProvider.Text(Questions.StackName, Arg.Any<CancellationToken>()).Returns("RenamedStack"); | ||
|
|
||
| // Act | ||
| await handler.Handle(RenameStackCommandInputs.Empty, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| stackConfig.Stacks.Should().HaveCount(2); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "RenamedStack"); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "Stack2"); | ||
| stackConfig.Stacks.Should().NotContain(s => s.Name == "Stack1"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenStackNameProvided_AsksOnlyForNewName_RenamesStack() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| inputProvider.Text(Questions.StackName, Arg.Any<CancellationToken>()).Returns("RenamedStack"); | ||
|
|
||
| // Act | ||
| await handler.Handle(new RenameStackCommandInputs("Stack1", null), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| await inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()); | ||
| stackConfig.Stacks.Should().HaveCount(1); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "RenamedStack"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenNewNameProvided_AsksOnlyForStack_RenamesStack() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()).Returns("Stack1"); | ||
|
|
||
| // Act | ||
| await handler.Handle(new RenameStackCommandInputs(null, "RenamedStack"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| await inputProvider.DidNotReceive().Text(Questions.StackName, Arg.Any<CancellationToken>()); | ||
| stackConfig.Stacks.Should().HaveCount(1); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "RenamedStack"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenBothInputsProvided_DoesNotAskForAnything_RenamesStack() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| // Act | ||
| await handler.Handle(new RenameStackCommandInputs("Stack1", "RenamedStack"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| await inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any<string[]>(), Arg.Any<CancellationToken>()); | ||
| await inputProvider.DidNotReceive().Text(Questions.StackName, Arg.Any<CancellationToken>()); | ||
| stackConfig.Stacks.Should().HaveCount(1); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "RenamedStack"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenStackDoesNotExist_ThrowsException() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| // Act & Assert | ||
| var act = async () => await handler.Handle(new RenameStackCommandInputs("NonExistentStack", "NewName"), CancellationToken.None); | ||
| await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("Stack not found."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenNewNameAlreadyExists_ThrowsException() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .WithStack(stack => stack | ||
| .WithName("Stack2") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| // Act & Assert | ||
| var act = async () => await handler.Handle(new RenameStackCommandInputs("Stack1", "Stack2"), CancellationToken.None); | ||
| await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("A stack with the name 'Stack2' already exists for this remote."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenRenamingToSameName_DoesNotThrowException() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| // Act | ||
| await handler.Handle(new RenameStackCommandInputs("Stack1", "Stack1"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| stackConfig.Stacks.Should().HaveCount(1); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "Stack1"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenStacksWithSameNameExistAcrossDifferentRemotes_AllowsRename() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri1 = Some.HttpsUri().ToString(); | ||
| var remoteUri2 = Some.HttpsUri().ToString(); | ||
|
|
||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri1) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .WithStack(stack => stack | ||
| .WithName("ExistingName") | ||
| .WithRemoteUri(remoteUri2) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri1); | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| // Act - Rename Stack1 to ExistingName (which exists on a different remote) | ||
| await handler.Handle(new RenameStackCommandInputs("Stack1", "ExistingName"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| stackConfig.Stacks.Should().HaveCount(2); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "ExistingName" && s.RemoteUri == remoteUri1); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "ExistingName" && s.RemoteUri == remoteUri2); | ||
| stackConfig.Stacks.Should().NotContain(s => s.Name == "Stack1"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenNoStacksExistForRemote_LogsNoStacksMessageAndReturns() | ||
| { | ||
| // Arrange | ||
| var sourceBranch = Some.BranchName(); | ||
| var remoteUri1 = Some.HttpsUri().ToString(); | ||
| var remoteUri2 = Some.HttpsUri().ToString(); | ||
|
|
||
| // Create a stack config with stacks only for a different remote | ||
| var stackConfig = new TestStackConfigBuilder() | ||
| .WithStack(stack => stack | ||
| .WithName("Stack1") | ||
| .WithRemoteUri(remoteUri2) | ||
| .WithSourceBranch(sourceBranch)) | ||
| .Build(); | ||
|
|
||
| var inputProvider = Substitute.For<IInputProvider>(); | ||
| var logger = XUnitLogger.CreateLogger<RenameStackCommandHandler>(testOutputHelper); | ||
| var gitClient = Substitute.For<IGitClient>(); | ||
|
|
||
| gitClient.GetRemoteUri().Returns(remoteUri1); // Different remote than the stack | ||
| gitClient.GetCurrentBranch().Returns(sourceBranch); | ||
|
|
||
| var handler = new RenameStackCommandHandler(inputProvider, logger, gitClient, stackConfig); | ||
|
|
||
| // Act | ||
| await handler.Handle(new RenameStackCommandInputs("Stack1", "NewName"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| // Verify the stack config was not modified | ||
| stackConfig.Stacks.Should().HaveCount(1); | ||
| stackConfig.Stacks.Should().Contain(s => s.Name == "Stack1" && s.RemoteUri == remoteUri2); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.