Skip to content
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

Use proper Git repository for delete stack tests #147

Merged
merged 26 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
81d5c5a
Wip adding proper Git tests
geofflamrock Dec 15, 2024
5789694
Use output provider for Git operations
geofflamrock Dec 16, 2024
19c5dbe
Use output provider in GitHub operations
geofflamrock Dec 16, 2024
15f16a0
Merge branch 'git-operations-use-output-provider' into proper-git-tes…
geofflamrock Dec 16, 2024
d591429
More wip
geofflamrock Dec 16, 2024
ab16a65
Merge branch 'main' into proper-git-tests-1
geofflamrock Dec 16, 2024
d92f997
More add branch tests with proper Git repo
geofflamrock Dec 16, 2024
3eddc88
Change single new branch test to use proper Git repo
geofflamrock Dec 16, 2024
f2956a0
Cleanup
geofflamrock Dec 16, 2024
1c9274a
Merge branch 'proper-git-tests-1' into proper-git-tests-2
geofflamrock Dec 16, 2024
6530c28
More new branch tests with Git repo
geofflamrock Dec 16, 2024
d05eceb
Add initial cleanup stack test with proper Git repo
geofflamrock Dec 16, 2024
149e319
Some cleanup
geofflamrock Dec 16, 2024
eff579b
Make all cleanup tests use proper Git repo
geofflamrock Dec 17, 2024
3459f44
Change delete stack test that cleans up branches to use proper Git repo
geofflamrock Dec 17, 2024
460892d
Make all delete tests use proper Git repo
geofflamrock Dec 17, 2024
35ce798
Cleanup
geofflamrock Dec 17, 2024
fd76b3b
Merge branch 'main' into proper-git-tests-2
geofflamrock Dec 17, 2024
fe228f8
Merge branch 'proper-git-tests-2' into proper-git-tests-3
geofflamrock Dec 17, 2024
f58a17a
Merge branch 'proper-git-tests-3' into proper-git-tests-4
geofflamrock Dec 17, 2024
38c48e8
Fix namespace
geofflamrock Dec 17, 2024
712c568
Merge branch 'proper-git-tests-2' into proper-git-tests-3
geofflamrock Dec 17, 2024
238d186
Merge branch 'proper-git-tests-3' into proper-git-tests-4
geofflamrock Dec 17, 2024
bc2df72
Merge branch 'main' into proper-git-tests-3
geofflamrock Dec 17, 2024
8a302de
Merge branch 'proper-git-tests-3' into proper-git-tests-4
geofflamrock Dec 17, 2024
c3afcc8
Merge branch 'main' into proper-git-tests-4
geofflamrock Dec 19, 2024
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
150 changes: 69 additions & 81 deletions src/Stack.Tests/Commands/Stack/DeleteStackCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ public class DeleteStackCommandHandlerTests
public async Task WhenNoInputsAreProvided_AsksForName_AndConfirmation_AndDeletesStack()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
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
Expand All @@ -47,30 +45,28 @@ public async Task WhenNoInputsAreProvided_AsksForName_AndConfirmation_AndDeletes
response.Should().Be(new DeleteStackCommandResponse("Stack1"));
stacks.Should().BeEquivalentTo(new List<Config.Stack>
{
new("Stack2", remoteUri, "branch-2", [])
new("Stack2", repo.RemoteUri, sourceBranch, [])
});
}

[Fact]
public async Task WhenConfirmationIsFalse_DoesNotDeleteStack()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
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
Expand All @@ -87,31 +83,29 @@ public async Task WhenConfirmationIsFalse_DoesNotDeleteStack()
response.Should().Be(new DeleteStackCommandResponse(null));
stacks.Should().BeEquivalentTo(new List<Config.Stack>
{
new("Stack1", remoteUri, "branch-1", []),
new("Stack2", remoteUri, "branch-2", [])
new("Stack1", repo.RemoteUri, sourceBranch, []),
new("Stack2", repo.RemoteUri, sourceBranch, [])
});
}

[Fact]
public async Task WhenNameIsProvided_AsksForConfirmation_AndDeletesStack()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
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
Expand All @@ -127,7 +121,7 @@ public async Task WhenNameIsProvided_AsksForConfirmation_AndDeletesStack()
response.Should().Be(new DeleteStackCommandResponse("Stack1"));
stacks.Should().BeEquivalentTo(new List<Config.Stack>
{
new("Stack2", remoteUri, "branch-2", [])
new("Stack2", repo.RemoteUri, sourceBranch, [])
});

inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any<string[]>());
Expand All @@ -137,22 +131,20 @@ public async Task WhenNameIsProvided_AsksForConfirmation_AndDeletesStack()
public async Task WhenForceIsProvided_DoesNotAskForConfirmation_AndDeletesStack()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
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
Expand All @@ -168,7 +160,7 @@ public async Task WhenForceIsProvided_DoesNotAskForConfirmation_AndDeletesStack(
response.Should().Be(new DeleteStackCommandResponse("Stack1"));
stacks.Should().BeEquivalentTo(new List<Config.Stack>
{
new("Stack2", remoteUri, "branch-2", [])
new("Stack2", repo.RemoteUri, sourceBranch, [])
});

inputProvider.DidNotReceive().Confirm(Questions.ConfirmDeleteStack);
Expand All @@ -178,22 +170,20 @@ public async Task WhenForceIsProvided_DoesNotAskForConfirmation_AndDeletesStack(
public async Task WhenNameAndForceAreProvided_DoesNotAskForAnyInput_AndDeletesStack()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
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
Expand All @@ -207,7 +197,7 @@ public async Task WhenNameAndForceAreProvided_DoesNotAskForAnyInput_AndDeletesSt
response.Should().Be(new DeleteStackCommandResponse("Stack1"));
stacks.Should().BeEquivalentTo(new List<Config.Stack>
{
new("Stack2", remoteUri, "branch-2", [])
new("Stack2", repo.RemoteUri, sourceBranch, [])
});

inputProvider.ReceivedCalls().Should().BeEmpty();
Expand All @@ -217,22 +207,20 @@ public async Task WhenNameAndForceAreProvided_DoesNotAskForAnyInput_AndDeletesSt
public async Task WhenStackDoesNotExist_Throws()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
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
Expand All @@ -252,24 +240,26 @@ await handler
public async Task WhenThereAreLocalBranchesThatAreNotInTheRemote_AsksToCleanup_AndDeletesThemBeforeDeletingStack()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<string[]>()).Returns(["branch-1", "branch-3"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
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);
Expand All @@ -288,30 +278,28 @@ public async Task WhenThereAreLocalBranchesThatAreNotInTheRemote_AsksToCleanup_A
response.Should().Be(new DeleteStackCommandResponse("Stack1"));
stacks.Should().BeEquivalentTo(new List<Config.Stack>
{
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<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
var sourceBranch = Some.BranchName();
using var repo = new TestGitRepositoryBuilder().Build();

var stackConfig = Substitute.For<IStackConfig>();
var inputProvider = Substitute.For<IInputProvider>();
var outputProvider = Substitute.For<IOutputProvider>();
var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings);
var gitHubOperations = Substitute.For<IGitHubOperations>();
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<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", [])
new("Stack1", repo.RemoteUri, sourceBranch, [])
]);
stackConfig.Load().Returns(stacks);
stackConfig
Expand Down
Loading