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 cleanup stack tests #146

Merged
merged 19 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
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
174 changes: 95 additions & 79 deletions src/Stack.Tests/Commands/Stack/CleanupStackCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@ public class CleanupStackCommandHandlerTests
public async Task WhenBranchExistsLocally_ButNotInRemote_BranchIsDeletedLocally()
{
// 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"]),
new("Stack2", remoteUri, "branch-3", ["branch-4"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);

var remoteUri = Some.HttpsUri().ToString();
stackConfig.Load().Returns(stacks);

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
Expand All @@ -43,31 +47,33 @@ public async Task WhenBranchExistsLocally_ButNotInRemote_BranchIsDeletedLocally(
await handler.Handle(CleanupStackCommandInputs.Empty);

// Assert
gitOperations.Received().DeleteLocalBranch("branch-2");
gitOperations.GetBranchesThatExistLocally([branchToCleanup, branchToKeep]).Should().BeEquivalentTo([branchToKeep]);
}

[Fact]
public async Task WhenBranchExistsLocally_AndInRemote_BranchIsNotDeletedLocally()
{
// Arrange
var gitOperations = Substitute.For<IGitOperations>();
var gitHubOperations = Substitute.For<IGitHubOperations>();
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 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1", "branch-2"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"]),
new("Stack2", remoteUri, "branch-3", ["branch-4"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToKeep, anotherBranchToKeep]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);
stackConfig.Load().Returns(stacks);

Expand All @@ -78,31 +84,33 @@ public async Task WhenBranchExistsLocally_AndInRemote_BranchIsNotDeletedLocally(
await handler.Handle(CleanupStackCommandInputs.Empty);

// Assert
gitOperations.DidNotReceive().DeleteLocalBranch("branch-2");
gitOperations.GetBranchesThatExistLocally([branchToKeep, anotherBranchToKeep]).Should().BeEquivalentTo([branchToKeep, anotherBranchToKeep]);
}

[Fact]
public async Task WhenConfirmationIsFalse_DoesNotDeleteAnyBranches()
{
// 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"]),
new("Stack2", remoteUri, "branch-3", ["branch-4"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);
stackConfig.Load().Returns(stacks);

Expand All @@ -113,31 +121,33 @@ public async Task WhenConfirmationIsFalse_DoesNotDeleteAnyBranches()
await handler.Handle(CleanupStackCommandInputs.Empty);

// Assert
gitOperations.DidNotReceive().DeleteLocalBranch("branch-2");
gitOperations.GetBranchesThatExistLocally([branchToKeep, branchToCleanup]).Should().BeEquivalentTo([branchToKeep, branchToCleanup]);
}

[Fact]
public async Task WhenStackNameIsProvided_ItIsNotAskedFor()
{
// 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"]),
new("Stack2", remoteUri, "branch-3", ["branch-4"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);
stackConfig.Load().Returns(stacks);

Expand All @@ -154,24 +164,26 @@ public async Task WhenStackNameIsProvided_ItIsNotAskedFor()
public async Task WhenForceIsProvided_ItIsNotAskedFor()
{
// 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"]),
new("Stack2", remoteUri, "branch-3", ["branch-4"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);
stackConfig.Load().Returns(stacks);

Expand All @@ -188,24 +200,26 @@ public async Task WhenForceIsProvided_ItIsNotAskedFor()
public async Task WhenStackNameIsProvided_ButStackDoesNotExist_Throws()
{
// 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"]),
new("Stack2", remoteUri, "branch-3", ["branch-4"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]),
new("Stack2", repo.RemoteUri, sourceBranch, [])
]);
stackConfig.Load().Returns(stacks);

Expand All @@ -223,23 +237,25 @@ await handler.Invoking(async h => await h.Handle(new CleanupStackCommandInputs(i
public async Task WhenOnlyASingleStackExists_StackIsSelectedAutomatically()
{
// 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 CleanupStackCommandHandler(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-2"]);
gitOperations.GetBranchesThatExistInRemote(Arg.Any<string[]>()).Returns(["branch-1"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-2"])
new("Stack1", repo.RemoteUri, sourceBranch, [branchToCleanup, branchToKeep]),
]);
stackConfig.Load().Returns(stacks);

Expand Down
22 changes: 18 additions & 4 deletions src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ namespace Stack.Tests.Helpers;

public class TestGitRepositoryBuilder
{
List<string> branches = [];
List<(string Name, bool PushToRemote)> branches = [];

public TestGitRepositoryBuilder WithBranch(string branch)
{
branches.Add(branch);
branches.Add((branch, false));
return this;
}

public TestGitRepositoryBuilder WithBranch(string branch, bool pushToRemote)
{
branches.Add((branch, pushToRemote));
return this;
}

Expand All @@ -21,7 +27,7 @@ public TestGitRepository Build()
var remoteDirectory = new TemporaryDirectory(remote);
var localDirectory = TemporaryDirectory.Create();

Repository.Init(remoteDirectory.DirectoryPath, true);
var remoteRepo = new Repository(Repository.Init(remoteDirectory.DirectoryPath, true));
var repo = new Repository(Repository.Clone(remote, localDirectory.DirectoryPath));
var defaultBranch = Some.BranchName();
repo.Refs.UpdateTarget("HEAD", "refs/heads/" + defaultBranch);
Expand All @@ -30,7 +36,15 @@ public TestGitRepository Build()

foreach (var branch in branches)
{
repo.CreateBranch(branch);
var newBranch = repo.CreateBranch(branch.Name);

if (branch.PushToRemote)
{
repo.Branches.Update(newBranch,
b => b.Remote = repo.Network.Remotes["origin"].Name,
b => b.UpstreamBranch = newBranch.CanonicalName);
repo.Network.Push(newBranch);
}
}

return new TestGitRepository(localDirectory, remoteDirectory, repo);
Expand Down
Loading