From 81d5c5a78c255ab979170941075dc946c7e1f327 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Mon, 16 Dec 2024 09:36:14 +1100 Subject: [PATCH 1/9] Wip adding proper Git tests --- .../Branch/AddBranchCommandHandlerTests.cs | 28 +++--- src/Stack.Tests/Helpers/Some.cs | 4 +- .../Helpers/TestGitRepositoryBuilder.cs | 86 +++++++++++++++++++ src/Stack.Tests/Stack.Tests.csproj | 1 + src/Stack/Commands/Branch/AddBranchCommand.cs | 2 +- 5 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs diff --git a/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs index 8e4544d..96afdaf 100644 --- a/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs @@ -1,5 +1,6 @@ using FluentAssertions; using NSubstitute; +using Spectre.Console; using Stack.Commands; using Stack.Commands.Helpers; using Stack.Config; @@ -15,22 +16,25 @@ public class AddBranchCommandHandlerTests public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_AddsBranchToStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + + var gitOperations = new GitOperations(Substitute.For(), new GitOperationSettings(false, false, repo.LocalDirectory.DirectoryPath)); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -38,7 +42,7 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_AddsBran .Do(ci => stacks = ci.ArgAt>(0)); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); - inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns("branch-5"); + inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToAdd); // Act await handler.Handle(AddBranchCommandInputs.Empty); @@ -46,8 +50,8 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_AddsBran // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, branchToAdd]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); } diff --git a/src/Stack.Tests/Helpers/Some.cs b/src/Stack.Tests/Helpers/Some.cs index 12b1537..1991841 100644 --- a/src/Stack.Tests/Helpers/Some.cs +++ b/src/Stack.Tests/Helpers/Some.cs @@ -5,5 +5,7 @@ namespace Stack.Tests.Helpers; public static class Some { public static string Name() => Guid.NewGuid().ToString("N"); - public static Uri HttpsUri() => new($"https://{Name()}.com"); + public static string ShortName() => Guid.NewGuid().ToString("N").Substring(0, 8); + public static string BranchName() => $"branch-{ShortName()}"; + public static Uri HttpsUri() => new($"https://{ShortName()}.com"); } diff --git a/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs b/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs new file mode 100644 index 0000000..19c0213 --- /dev/null +++ b/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs @@ -0,0 +1,86 @@ +using System; +using LibGit2Sharp; +using Microsoft.VisualBasic; + +namespace Stack.Tests.Helpers; + +public class TestGitRepositoryBuilder +{ + List branches = []; + + public TestGitRepositoryBuilder WithBranch(string branch) + { + branches.Add(branch); + return this; + } + + public TestGitRepository Build() + { + var remote = Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString("N"), ".git"); + var remoteDirectory = new TemporaryDirectory(remote); + var localDirectory = TemporaryDirectory.Create(); + + 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); + + Commit(repo, ("README.md", "Hello, World!")); + + foreach (var branch in branches) + { + repo.CreateBranch(branch); + } + + return new TestGitRepository(localDirectory, remoteDirectory, repo); + } + + private Commit Commit(Repository repository, params (string Name, string? Content)[] files) + { + var message = $"Commit: {Some.Name()}"; + var signature = new Signature(Some.Name(), Some.Name(), DateTimeOffset.Now); + + return repository.Commit(message, signature, signature); + } +} + +public class TestGitRepository(TemporaryDirectory LocalDirectory, TemporaryDirectory RemoteDirectory, Repository Repository) : IDisposable +{ + public TemporaryDirectory LocalDirectory { get; } = LocalDirectory; + public TemporaryDirectory RemoteDirectory { get; } = RemoteDirectory; + public string RemoteUri => RemoteDirectory.DirectoryPath; + + public void Dispose() + { + GC.SuppressFinalize(this); + Repository.Dispose(); + LocalDirectory.Dispose(); + RemoteDirectory.Dispose(); + } +} + +public class TemporaryDirectory(string DirectoryPath) : IDisposable +{ + public string DirectoryPath { get; } = DirectoryPath; + + public void Dispose() + { + GC.SuppressFinalize(this); + + try + { + Directory.Delete(DirectoryPath, true); + } + catch (Exception) + { + // Ignore + } + } + + public static TemporaryDirectory Create() + { + var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(path); + return new TemporaryDirectory(path); + } +} diff --git a/src/Stack.Tests/Stack.Tests.csproj b/src/Stack.Tests/Stack.Tests.csproj index 71e0471..7c449f5 100644 --- a/src/Stack.Tests/Stack.Tests.csproj +++ b/src/Stack.Tests/Stack.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Stack/Commands/Branch/AddBranchCommand.cs b/src/Stack/Commands/Branch/AddBranchCommand.cs index 098453d..c5de92e 100644 --- a/src/Stack/Commands/Branch/AddBranchCommand.cs +++ b/src/Stack/Commands/Branch/AddBranchCommand.cs @@ -56,7 +56,7 @@ public async Task Handle(AddBranchCommandInputs inputs { await Task.CompletedTask; - var defaultBranch = gitOperations.GetDefaultBranch(); + // var defaultBranch = gitOperations.GetDefaultBranch(); var remoteUri = gitOperations.GetRemoteUri(); var currentBranch = gitOperations.GetCurrentBranch(); var branches = gitOperations.GetLocalBranchesOrderedByMostRecentCommitterDate(); From 5789694f75383e1b16c0a6e167700d78e021eaee Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Mon, 16 Dec 2024 17:23:56 +1100 Subject: [PATCH 2/9] Use output provider for Git operations --- src/Stack/Commands/Branch/AddBranchCommand.cs | 5 +++-- src/Stack/Commands/Branch/NewBranchCommand.cs | 5 +++-- .../Commands/Branch/RemoveBranchCommand.cs | 5 +++-- .../PullRequests/CreatePullRequestsCommand.cs | 5 +++-- .../PullRequests/OpenPullRequestsCommand.cs | 5 +++-- .../Commands/Stack/CleanupStackCommand.cs | 6 ++++-- src/Stack/Commands/Stack/DeleteStackCommand.cs | 6 ++++-- src/Stack/Commands/Stack/ListStacksCommand.cs | 4 +++- src/Stack/Commands/Stack/NewStackCommand.cs | 6 ++++-- src/Stack/Commands/Stack/StackStatusCommand.cs | 5 +++-- src/Stack/Commands/Stack/StackSwitchCommand.cs | 3 ++- src/Stack/Commands/Stack/UpdateStackCommand.cs | 6 ++++-- src/Stack/Git/GitOperations.cs | 18 +++++++++--------- 13 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/Stack/Commands/Branch/AddBranchCommand.cs b/src/Stack/Commands/Branch/AddBranchCommand.cs index 098453d..d397038 100644 --- a/src/Stack/Commands/Branch/AddBranchCommand.cs +++ b/src/Stack/Commands/Branch/AddBranchCommand.cs @@ -26,11 +26,12 @@ public override async Task ExecuteAsync(CommandContext context, AddBranchCo await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new AddBranchCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new AddBranchCommandInputs(settings.Stack, settings.Name)); diff --git a/src/Stack/Commands/Branch/NewBranchCommand.cs b/src/Stack/Commands/Branch/NewBranchCommand.cs index 5b241aa..00f9c96 100644 --- a/src/Stack/Commands/Branch/NewBranchCommand.cs +++ b/src/Stack/Commands/Branch/NewBranchCommand.cs @@ -30,11 +30,12 @@ public override async Task ExecuteAsync(CommandContext context, NewBranchCo await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new NewBranchCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new NewBranchCommandInputs(settings.Stack, settings.Name, settings.Force)); diff --git a/src/Stack/Commands/Branch/RemoveBranchCommand.cs b/src/Stack/Commands/Branch/RemoveBranchCommand.cs index 1e1d494..2bbd8da 100644 --- a/src/Stack/Commands/Branch/RemoveBranchCommand.cs +++ b/src/Stack/Commands/Branch/RemoveBranchCommand.cs @@ -30,11 +30,12 @@ public override async Task ExecuteAsync(CommandContext context, RemoveBranc await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new RemoveBranchCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new RemoveBranchCommandInputs(settings.Stack, settings.Name, settings.Force)); diff --git a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs index 5b5b132..f278434 100644 --- a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs @@ -20,11 +20,12 @@ public class CreatePullRequestsCommand : AsyncCommand ExecuteAsync(CommandContext context, CreatePullRequestsCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new CreatePullRequestsCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new GitHubOperations(console, settings.GetGitHubOperationSettings()), new FileOperations(), new StackConfig()); diff --git a/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs b/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs index fddc5a6..a25e686 100644 --- a/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs @@ -20,11 +20,12 @@ public class OpenPullRequestsCommand : AsyncCommand ExecuteAsync(CommandContext context, OpenPullRequestsCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new OpenPullRequestsCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new GitHubOperations(console, settings.GetGitHubOperationSettings()), new StackConfig()); diff --git a/src/Stack/Commands/Stack/CleanupStackCommand.cs b/src/Stack/Commands/Stack/CleanupStackCommand.cs index 8d3b715..29331dc 100644 --- a/src/Stack/Commands/Stack/CleanupStackCommand.cs +++ b/src/Stack/Commands/Stack/CleanupStackCommand.cs @@ -27,10 +27,12 @@ public override async Task ExecuteAsync(CommandContext context, CleanupStac await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new CleanupStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new GitHubOperations(console, settings.GetGitHubOperationSettings()), new StackConfig()); diff --git a/src/Stack/Commands/Stack/DeleteStackCommand.cs b/src/Stack/Commands/Stack/DeleteStackCommand.cs index 7f4133e..6142127 100644 --- a/src/Stack/Commands/Stack/DeleteStackCommand.cs +++ b/src/Stack/Commands/Stack/DeleteStackCommand.cs @@ -27,10 +27,12 @@ public override async Task ExecuteAsync(CommandContext context, DeleteStack await Task.CompletedTask; var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new DeleteStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new GitHubOperations(console, settings.GetGitHubOperationSettings()), new StackConfig()); diff --git a/src/Stack/Commands/Stack/ListStacksCommand.cs b/src/Stack/Commands/Stack/ListStacksCommand.cs index 57cd795..d259a48 100644 --- a/src/Stack/Commands/Stack/ListStacksCommand.cs +++ b/src/Stack/Commands/Stack/ListStacksCommand.cs @@ -3,6 +3,7 @@ using Spectre.Console.Cli; using Stack.Config; using Stack.Git; +using Stack.Infrastructure; namespace Stack.Commands; @@ -14,7 +15,8 @@ public override async Task ExecuteAsync(CommandContext context, ListStacksC { await Task.CompletedTask; var console = AnsiConsole.Console; - var gitOperations = new GitOperations(console, settings.GetGitOperationSettings()); + var outputProvider = new ConsoleOutputProvider(console); + var gitOperations = new GitOperations(outputProvider, settings.GetGitOperationSettings()); var stackConfig = new StackConfig(); var stacks = stackConfig.Load(); diff --git a/src/Stack/Commands/Stack/NewStackCommand.cs b/src/Stack/Commands/Stack/NewStackCommand.cs index c5fd5a5..619b1d8 100644 --- a/src/Stack/Commands/Stack/NewStackCommand.cs +++ b/src/Stack/Commands/Stack/NewStackCommand.cs @@ -40,10 +40,12 @@ public class NewStackCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, NewStackCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new NewStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); var response = await handler.Handle( diff --git a/src/Stack/Commands/Stack/StackStatusCommand.cs b/src/Stack/Commands/Stack/StackStatusCommand.cs index e5e1e88..ce2500c 100644 --- a/src/Stack/Commands/Stack/StackStatusCommand.cs +++ b/src/Stack/Commands/Stack/StackStatusCommand.cs @@ -24,11 +24,12 @@ public class StackStatusCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, StackStatusCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new StackStatusCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new GitHubOperations(console, settings.GetGitHubOperationSettings()), new StackConfig()); diff --git a/src/Stack/Commands/Stack/StackSwitchCommand.cs b/src/Stack/Commands/Stack/StackSwitchCommand.cs index ed206a9..082f808 100644 --- a/src/Stack/Commands/Stack/StackSwitchCommand.cs +++ b/src/Stack/Commands/Stack/StackSwitchCommand.cs @@ -20,10 +20,11 @@ public class StackSwitchCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, StackSwitchCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); var handler = new StackSwitchCommandHandler( new ConsoleInputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new StackConfig()); await handler.Handle(new StackSwitchCommandInputs(settings.Branch)); diff --git a/src/Stack/Commands/Stack/UpdateStackCommand.cs b/src/Stack/Commands/Stack/UpdateStackCommand.cs index 1a0c8c8..abde171 100644 --- a/src/Stack/Commands/Stack/UpdateStackCommand.cs +++ b/src/Stack/Commands/Stack/UpdateStackCommand.cs @@ -25,10 +25,12 @@ public class UpdateStackCommand : AsyncCommand public override async Task ExecuteAsync(CommandContext context, UpdateStackCommandSettings settings) { var console = AnsiConsole.Console; + var outputProvider = new ConsoleOutputProvider(console); + var handler = new UpdateStackCommandHandler( new ConsoleInputProvider(console), - new ConsoleOutputProvider(console), - new GitOperations(console, settings.GetGitOperationSettings()), + outputProvider, + new GitOperations(outputProvider, settings.GetGitOperationSettings()), new GitHubOperations(console, settings.GetGitHubOperationSettings()), new StackConfig()); diff --git a/src/Stack/Git/GitOperations.cs b/src/Stack/Git/GitOperations.cs index e9a2aff..27be14a 100644 --- a/src/Stack/Git/GitOperations.cs +++ b/src/Stack/Git/GitOperations.cs @@ -1,7 +1,7 @@ using System.Diagnostics; using System.Text; using Octopus.Shellfish; -using Spectre.Console; +using Stack.Infrastructure; namespace Stack.Git; @@ -38,7 +38,7 @@ public interface IGitOperations void OpenFileInEditorAndWaitForClose(string path); } -public class GitOperations(IAnsiConsole console, GitOperationSettings settings) : IGitOperations +public class GitOperations(IOutputProvider outputProvider, GitOperationSettings settings) : IGitOperations { public void CreateNewBranch(string branchName, string sourceBranch) { @@ -165,7 +165,7 @@ public void OpenFileInEditorAndWaitForClose(string path) var editor = GetConfiguredEditor(); if (string.IsNullOrWhiteSpace(editor)) { - console.MarkupLine("[red]No editor is configured in git. Please configure an editor using 'git config --global core.editor '.[/]"); + outputProvider.Error("No editor is configured in git. Please configure an editor using 'git config --global core.editor '."); return; } @@ -185,7 +185,7 @@ public void OpenFileInEditorAndWaitForClose(string path) if (process == null) { - console.MarkupLine("[red]Failed to start editor process.[/]"); + outputProvider.Error("Failed to start editor process."); return; } @@ -195,7 +195,7 @@ public void OpenFileInEditorAndWaitForClose(string path) private string ExecuteGitCommandAndReturnOutput(string command) { if (settings.Verbose) - console.MarkupLine($"[grey]git {command}[/]"); + outputProvider.Debug($"git {command}"); var infoBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); @@ -210,13 +210,13 @@ private string ExecuteGitCommandAndReturnOutput(string command) if (result != 0) { - console.MarkupLine($"[red]{errorBuilder}[/]"); + outputProvider.Error($"{errorBuilder}"); throw new Exception("Failed to execute git command."); } if (settings.Verbose && infoBuilder.Length > 0) { - console.MarkupLine($"[grey]{infoBuilder}[/]"); + outputProvider.Debug($"{infoBuilder}"); } return infoBuilder.ToString(); @@ -227,7 +227,7 @@ private void ExecuteGitCommand(string command) if (settings.DryRun) { if (settings.Verbose) - console.MarkupLine($"[grey]git {command}[/]"); + outputProvider.Debug($"git {command}"); } else { @@ -239,7 +239,7 @@ private void ExecuteGitCommand(string command) // changes to the Git repository as the output might be important. // In verbose mode we would have already written the output // of the command so don't write it again. - console.MarkupLine($"[grey]{output}[/]"); + outputProvider.Debug($"{output}"); } } } From 19c5dbeb1db5a4c6c9ba203a121ea5dce4b0d8b1 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Mon, 16 Dec 2024 17:26:43 +1100 Subject: [PATCH 3/9] Use output provider in GitHub operations --- .../PullRequests/CreatePullRequestsCommand.cs | 2 +- .../PullRequests/OpenPullRequestsCommand.cs | 2 +- src/Stack/Commands/Stack/CleanupStackCommand.cs | 2 +- src/Stack/Commands/Stack/DeleteStackCommand.cs | 2 +- src/Stack/Commands/Stack/StackStatusCommand.cs | 2 +- src/Stack/Commands/Stack/UpdateStackCommand.cs | 2 +- src/Stack/Git/GitHubOperations.cs | 15 ++++++++------- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs index f278434..9f28f50 100644 --- a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs @@ -26,7 +26,7 @@ public override async Task ExecuteAsync(CommandContext context, CreatePullR new ConsoleInputProvider(console), outputProvider, new GitOperations(outputProvider, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new FileOperations(), new StackConfig()); diff --git a/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs b/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs index a25e686..f1bf100 100644 --- a/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/OpenPullRequestsCommand.cs @@ -26,7 +26,7 @@ public override async Task ExecuteAsync(CommandContext context, OpenPullReq new ConsoleInputProvider(console), outputProvider, new GitOperations(outputProvider, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new OpenPullRequestsCommandInputs(settings.Name)); diff --git a/src/Stack/Commands/Stack/CleanupStackCommand.cs b/src/Stack/Commands/Stack/CleanupStackCommand.cs index 29331dc..03f684d 100644 --- a/src/Stack/Commands/Stack/CleanupStackCommand.cs +++ b/src/Stack/Commands/Stack/CleanupStackCommand.cs @@ -33,7 +33,7 @@ public override async Task ExecuteAsync(CommandContext context, CleanupStac new ConsoleInputProvider(console), outputProvider, new GitOperations(outputProvider, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new CleanupStackCommandInputs(settings.Name, settings.Force)); diff --git a/src/Stack/Commands/Stack/DeleteStackCommand.cs b/src/Stack/Commands/Stack/DeleteStackCommand.cs index 6142127..1061147 100644 --- a/src/Stack/Commands/Stack/DeleteStackCommand.cs +++ b/src/Stack/Commands/Stack/DeleteStackCommand.cs @@ -33,7 +33,7 @@ public override async Task ExecuteAsync(CommandContext context, DeleteStack new ConsoleInputProvider(console), outputProvider, new GitOperations(outputProvider, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); var response = await handler.Handle(new DeleteStackCommandInputs(settings.Name, settings.Force)); diff --git a/src/Stack/Commands/Stack/StackStatusCommand.cs b/src/Stack/Commands/Stack/StackStatusCommand.cs index ce2500c..8c2b66a 100644 --- a/src/Stack/Commands/Stack/StackStatusCommand.cs +++ b/src/Stack/Commands/Stack/StackStatusCommand.cs @@ -30,7 +30,7 @@ public override async Task ExecuteAsync(CommandContext context, StackStatus new ConsoleInputProvider(console), outputProvider, new GitOperations(outputProvider, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new StackStatusCommandInputs(settings.Name, settings.All)); diff --git a/src/Stack/Commands/Stack/UpdateStackCommand.cs b/src/Stack/Commands/Stack/UpdateStackCommand.cs index abde171..b4ae374 100644 --- a/src/Stack/Commands/Stack/UpdateStackCommand.cs +++ b/src/Stack/Commands/Stack/UpdateStackCommand.cs @@ -31,7 +31,7 @@ public override async Task ExecuteAsync(CommandContext context, UpdateStack new ConsoleInputProvider(console), outputProvider, new GitOperations(outputProvider, settings.GetGitOperationSettings()), - new GitHubOperations(console, settings.GetGitHubOperationSettings()), + new GitHubOperations(outputProvider, settings.GetGitHubOperationSettings()), new StackConfig()); await handler.Handle(new UpdateStackCommandInputs(settings.Name, settings.Force)); diff --git a/src/Stack/Git/GitHubOperations.cs b/src/Stack/Git/GitHubOperations.cs index a927109..e8957d8 100644 --- a/src/Stack/Git/GitHubOperations.cs +++ b/src/Stack/Git/GitHubOperations.cs @@ -1,6 +1,7 @@ using System.Text; using Octopus.Shellfish; using Spectre.Console; +using Stack.Infrastructure; namespace Stack.Git; @@ -48,7 +49,7 @@ public interface IGitHubOperations void OpenPullRequest(GitHubPullRequest pullRequest); } -public class GitHubOperations(IAnsiConsole console, GitHubOperationSettings settings) : IGitHubOperations +public class GitHubOperations(IOutputProvider outputProvider, GitHubOperationSettings settings) : IGitHubOperations { public GitHubPullRequest? GetPullRequest(string branch) { @@ -91,7 +92,7 @@ public void OpenPullRequest(GitHubPullRequest pullRequest) private string ExecuteGitHubCommandAndReturnOutput(string command) { if (settings.Verbose) - console.MarkupLine($"[grey]gh {command}[/]"); + outputProvider.Debug($"gh {command}"); var infoBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); @@ -106,13 +107,13 @@ private string ExecuteGitHubCommandAndReturnOutput(string command) if (result != 0) { - console.MarkupLine($"[red]{errorBuilder}[/]"); + outputProvider.Error($"{errorBuilder}"); throw new Exception("Failed to execute gh command."); } if (settings.Verbose && infoBuilder.Length > 0) { - console.MarkupLine($"[grey]{infoBuilder}[/]"); + outputProvider.Debug($"{infoBuilder}"); } return infoBuilder.ToString(); @@ -121,7 +122,7 @@ private string ExecuteGitHubCommandAndReturnOutput(string command) private void ExecuteGitHubCommand(string command) { if (settings.Verbose) - console.MarkupLine($"[grey]gh {command}[/]"); + outputProvider.Debug($"gh {command}"); if (!settings.DryRun) { @@ -144,14 +145,14 @@ private void ExecuteGitHubCommandInternal(string command) if (result != 0) { - console.MarkupLine($"[red]{errorBuilder}[/]"); + outputProvider.Error($"{errorBuilder}"); throw new Exception("Failed to execute gh command."); } else { if (infoBuilder.Length > 0) { - console.MarkupLine($"[grey]{infoBuilder}[/]"); + outputProvider.Debug($"{infoBuilder}"); } } } From d591429613ef1e3e689e2db10257709d5f6ed5d1 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Mon, 16 Dec 2024 17:41:36 +1100 Subject: [PATCH 4/9] More wip --- .../Commands/Branch/AddBranchCommandHandlerTests.cs | 4 ++-- src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs index 96afdaf..962b52b 100644 --- a/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs @@ -19,16 +19,16 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_AddsBran var sourceBranch = Some.BranchName(); var anotherBranch = Some.BranchName(); var branchToAdd = Some.BranchName(); - var repo = new TestGitRepositoryBuilder() + using var repo = new TestGitRepositoryBuilder() .WithBranch(sourceBranch) .WithBranch(anotherBranch) .WithBranch(branchToAdd) .Build(); - var gitOperations = new GitOperations(Substitute.For(), new GitOperationSettings(false, false, repo.LocalDirectory.DirectoryPath)); var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); var stacks = new List( diff --git a/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs b/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs index 19c0213..7e420b1 100644 --- a/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs +++ b/src/Stack.Tests/Helpers/TestGitRepositoryBuilder.cs @@ -1,6 +1,7 @@ using System; using LibGit2Sharp; using Microsoft.VisualBasic; +using Stack.Git; namespace Stack.Tests.Helpers; @@ -49,6 +50,7 @@ public class TestGitRepository(TemporaryDirectory LocalDirectory, TemporaryDirec public TemporaryDirectory LocalDirectory { get; } = LocalDirectory; public TemporaryDirectory RemoteDirectory { get; } = RemoteDirectory; public string RemoteUri => RemoteDirectory.DirectoryPath; + public GitOperationSettings GitOperationSettings => new GitOperationSettings(false, false, LocalDirectory.DirectoryPath); public void Dispose() { From d92f9975de9be5d27d6e86e3bef3b05846a42384 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Tue, 17 Dec 2024 08:25:31 +1100 Subject: [PATCH 5/9] More add branch tests with proper Git repo --- .../Branch/AddBranchCommandHandlerTests.cs | 172 ++++++++++-------- 1 file changed, 97 insertions(+), 75 deletions(-) diff --git a/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs index 962b52b..3a905fa 100644 --- a/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/AddBranchCommandHandlerTests.cs @@ -59,29 +59,32 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_AddsBran public async Task WhenStackNameProvided_DoesNotAskForStackName_AddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig .WhenForAnyArgs(s => s.Save(Arg.Any>())) .Do(ci => stacks = ci.ArgAt>(0)); - inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns("branch-5"); + inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToAdd); // Act await handler.Handle(new AddBranchCommandInputs("Stack1", null)); @@ -90,8 +93,8 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_AddsBranchFromSta inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, branchToAdd]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); } @@ -99,27 +102,30 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_AddsBranchFromSta public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_AddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]) ]); stackConfig.Load().Returns(stacks); stackConfig .WhenForAnyArgs(s => s.Save(Arg.Any>())) .Do(ci => stacks = ci.ArgAt>(0)); - inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns("branch-5"); + inputProvider.Select(Questions.SelectBranch, Arg.Any()).Returns(branchToAdd); // Act await handler.Handle(new AddBranchCommandInputs(null, null)); @@ -128,7 +134,7 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_AddsBranchFromSt inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, branchToAdd]) }); } @@ -136,21 +142,25 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_AddsBranchFromSt public async Task WhenStackNameProvided_ButStackDoesNotExist_Throws() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); @@ -166,22 +176,25 @@ await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(inva public async Task WhenBranchNameProvided_DoesNotAskForBranchName_AddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -191,13 +204,13 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_AddsBranchFromS inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act - await handler.Handle(new AddBranchCommandInputs(null, "branch-5")); + await handler.Handle(new AddBranchCommandInputs(null, branchToAdd)); // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, branchToAdd]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Select(Questions.SelectBranch, Arg.Any()); } @@ -206,29 +219,32 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_AddsBranchFromS public async Task WhenBranchNameProvided_ButBranchDoesNotExistLocally_Throws() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act and assert - var invalidBranchName = Some.Name(); + var invalidBranchName = Some.BranchName(); await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(null, invalidBranchName))) .Should() .ThrowAsync() @@ -239,54 +255,60 @@ await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(null public async Task WhenBranchNameProvided_ButBranchAlreadyExistsInStack_Throws() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, branchToAdd]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act and assert - await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(null, "branch-5"))) + await handler.Invoking(async h => await h.Handle(new AddBranchCommandInputs(null, branchToAdd))) .Should() .ThrowAsync() - .WithMessage($"Branch 'branch-5' already exists in stack 'Stack1'."); + .WithMessage($"Branch '{branchToAdd}' already exists in stack 'Stack1'."); } [Fact] public async Task WhenAllInputsProvided_DoesNotAskForAnything_AddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var branchToAdd = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .WithBranch(branchToAdd) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new AddBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -294,13 +316,13 @@ public async Task WhenAllInputsProvided_DoesNotAskForAnything_AddsBranchFromStac .Do(ci => stacks = ci.ArgAt>(0)); // Act - await handler.Handle(new AddBranchCommandInputs("Stack1", "branch-5")); + await handler.Handle(new AddBranchCommandInputs("Stack1", branchToAdd)); // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, branchToAdd]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.ReceivedCalls().Should().BeEmpty(); } From 3eddc88999e6c144132d42fcf63a960bf75d53e9 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Tue, 17 Dec 2024 08:28:04 +1100 Subject: [PATCH 6/9] Change single new branch test to use proper Git repo --- .../Branch/NewBranchCommandHandlerTests.cs | 28 ++++++++++--------- src/Stack/Commands/Branch/NewBranchCommand.cs | 1 - 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs index b587912..e4c951e 100644 --- a/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs @@ -15,22 +15,24 @@ public class NewBranchCommandHandlerTests public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_CreatesAndAddsBranchToStackAndSwitchesToBranch() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -38,7 +40,7 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_CreatesA .Do(ci => stacks = ci.ArgAt>(0)); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); - inputProvider.Text(Questions.BranchName, Arg.Any()).Returns("branch-5"); + inputProvider.Text(Questions.BranchName, Arg.Any()).Returns(newBranch); inputProvider.Confirm(Questions.ConfirmSwitchToBranch).Returns(true); // Act @@ -47,10 +49,10 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_CreatesA // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); - gitOperations.Received().ChangeBranch("branch-5"); + gitOperations.GetCurrentBranch().Should().Be(newBranch); } [Fact] diff --git a/src/Stack/Commands/Branch/NewBranchCommand.cs b/src/Stack/Commands/Branch/NewBranchCommand.cs index 00f9c96..d9601b6 100644 --- a/src/Stack/Commands/Branch/NewBranchCommand.cs +++ b/src/Stack/Commands/Branch/NewBranchCommand.cs @@ -61,7 +61,6 @@ public async Task Handle(NewBranchCommandInputs inputs { await Task.CompletedTask; - var defaultBranch = gitOperations.GetDefaultBranch(); var remoteUri = gitOperations.GetRemoteUri(); var currentBranch = gitOperations.GetCurrentBranch(); var branches = gitOperations.GetLocalBranchesOrderedByMostRecentCommitterDate(); From f2956a0dceff727fd97d00fb932090b2c6240f4e Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Tue, 17 Dec 2024 08:28:43 +1100 Subject: [PATCH 7/9] Cleanup --- src/Stack/Commands/Branch/AddBranchCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Stack/Commands/Branch/AddBranchCommand.cs b/src/Stack/Commands/Branch/AddBranchCommand.cs index 57ccf93..51d4371 100644 --- a/src/Stack/Commands/Branch/AddBranchCommand.cs +++ b/src/Stack/Commands/Branch/AddBranchCommand.cs @@ -57,7 +57,6 @@ public async Task Handle(AddBranchCommandInputs inputs { await Task.CompletedTask; - // var defaultBranch = gitOperations.GetDefaultBranch(); var remoteUri = gitOperations.GetRemoteUri(); var currentBranch = gitOperations.GetCurrentBranch(); var branches = gitOperations.GetLocalBranchesOrderedByMostRecentCommitterDate(); From 6530c2864b77b98b1e326e534d3c0b0f9db1577f Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Tue, 17 Dec 2024 08:41:25 +1100 Subject: [PATCH 8/9] More new branch tests with Git repo --- .../Branch/NewBranchCommandHandlerTests.cs | 224 ++++++++++-------- src/Stack/Git/GitOperations.cs | 6 - 2 files changed, 121 insertions(+), 109 deletions(-) diff --git a/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs index e4c951e..8c1ccb3 100644 --- a/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs @@ -56,25 +56,27 @@ public async Task WhenNoInputsProvided_AsksForStackAndBranchAndConfirms_CreatesA } [Fact] - public async Task WhenSwitchBranchIsFalse_CreatsAndAddsBranchToStackButDoesNotSwitchToBranch() + public async Task WhenSwitchBranchIsFalse_CreatesAndAddsBranchToStackButDoesNotSwitchToBranch() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -82,7 +84,7 @@ public async Task WhenSwitchBranchIsFalse_CreatsAndAddsBranchToStackButDoesNotSw .Do(ci => stacks = ci.ArgAt>(0)); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); - inputProvider.Text(Questions.BranchName, Arg.Any()).Returns("branch-5"); + inputProvider.Text(Questions.BranchName, Arg.Any()).Returns(newBranch); inputProvider.Confirm(Questions.ConfirmSwitchToBranch).Returns(false); // Act @@ -91,39 +93,41 @@ public async Task WhenSwitchBranchIsFalse_CreatsAndAddsBranchToStackButDoesNotSw // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); - gitOperations.DidNotReceive().ChangeBranch("branch-5"); + gitOperations.GetCurrentBranch().Should().NotBe(newBranch); } [Fact] public async Task WhenStackNameProvided_DoesNotAskForStackName_CreatesAndAddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig .WhenForAnyArgs(s => s.Save(Arg.Any>())) .Do(ci => stacks = ci.ArgAt>(0)); - inputProvider.Text(Questions.BranchName, Arg.Any()).Returns("branch-5"); + inputProvider.Text(Questions.BranchName, Arg.Any()).Returns(newBranch); // Act await handler.Handle(new NewBranchCommandInputs("Stack1", null, false)); @@ -132,8 +136,8 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_CreatesAndAddsBra inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); } @@ -141,28 +145,30 @@ public async Task WhenStackNameProvided_DoesNotAskForStackName_CreatesAndAddsBra public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_CreatesAndAddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]) ]); stackConfig.Load().Returns(stacks); stackConfig .WhenForAnyArgs(s => s.Save(Arg.Any>())) .Do(ci => stacks = ci.ArgAt>(0)); - inputProvider.Text(Questions.BranchName, Arg.Any()).Returns("branch-5"); + inputProvider.Text(Questions.BranchName, Arg.Any()).Returns(newBranch); // Act await handler.Handle(new NewBranchCommandInputs(null, null, false)); @@ -171,7 +177,7 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_CreatesAndAddsBr inputProvider.DidNotReceive().Select(Questions.SelectStack, Arg.Any()); stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), }); } @@ -179,21 +185,24 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_CreatesAndAddsBr public async Task WhenStackNameProvided_ButStackDoesNotExist_Throws() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); @@ -209,22 +218,24 @@ await handler.Invoking(async h => await h.Handle(new NewBranchCommandInputs(inva public async Task WhenBranchNameProvided_DoesNotAskForBranchName_CreatesAndAddsBranchFromStack() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -234,13 +245,13 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_CreatesAndAddsB inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act - await handler.Handle(new NewBranchCommandInputs(null, "branch-5", false)); + await handler.Handle(new NewBranchCommandInputs(null, newBranch, false)); // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.DidNotReceive().Text(Questions.BranchName); } @@ -249,22 +260,23 @@ public async Task WhenBranchNameProvided_DoesNotAskForBranchName_CreatesAndAddsB public async Task WhenBranchNameProvided_ButBranchAlreadyExistLocally_Throws() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(true); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, []), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); @@ -272,64 +284,68 @@ public async Task WhenBranchNameProvided_ButBranchAlreadyExistLocally_Throws() // Act and assert var invalidBranchName = Some.Name(); - await handler.Invoking(async h => await h.Handle(new NewBranchCommandInputs(null, "branch-5", false))) + await handler.Invoking(async h => await h.Handle(new NewBranchCommandInputs(null, anotherBranch, false))) .Should() .ThrowAsync() - .WithMessage($"Branch 'branch-5' already exists locally."); + .WithMessage($"Branch '{anotherBranch}' already exists locally."); } [Fact] public async Task WhenBranchNameProvided_ButBranchAlreadyExistsInStack_Throws() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("Stack1"); // Act and assert - await handler.Invoking(async h => await h.Handle(new NewBranchCommandInputs(null, "branch-5", false))) + await handler.Invoking(async h => await h.Handle(new NewBranchCommandInputs(null, newBranch, false))) .Should() .ThrowAsync() - .WithMessage($"Branch 'branch-5' already exists in stack 'Stack1'."); + .WithMessage($"Branch '{newBranch}' already exists in stack 'Stack1'."); } [Fact] public async Task WhenAllInputsProvided_DoesNotAskForAnything_CreatesAndAddsBranchFromStackAndChangesToBranch() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("Stack1", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -337,13 +353,13 @@ public async Task WhenAllInputsProvided_DoesNotAskForAnything_CreatesAndAddsBran .Do(ci => stacks = ci.ArgAt>(0)); // Act - await handler.Handle(new NewBranchCommandInputs("Stack1", "branch-5", true)); + await handler.Handle(new NewBranchCommandInputs("Stack1", newBranch, true)); // Assert stacks.Should().BeEquivalentTo(new List { - new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("Stack1", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); inputProvider.ReceivedCalls().Should().BeEmpty(); } @@ -352,22 +368,24 @@ public async Task WhenAllInputsProvided_DoesNotAskForAnything_CreatesAndAddsBran public async Task WhenStackHasANameWithMultipleWords_SuggestsAGoodDefaultNewBranchName() { // Arrange - var gitOperations = Substitute.For(); + var sourceBranch = Some.BranchName(); + var anotherBranch = Some.BranchName(); + var newBranch = Some.BranchName(); + using var repo = new TestGitRepositoryBuilder() + .WithBranch(sourceBranch) + .WithBranch(anotherBranch) + .Build(); + var stackConfig = Substitute.For(); var inputProvider = Substitute.For(); var outputProvider = Substitute.For(); + var gitOperations = new GitOperations(outputProvider, repo.GitOperationSettings); var handler = new NewBranchCommandHandler(inputProvider, outputProvider, gitOperations, stackConfig); - var remoteUri = Some.HttpsUri().ToString(); - - gitOperations.GetRemoteUri().Returns(remoteUri); - gitOperations.GetCurrentBranch().Returns("branch-1"); - gitOperations.DoesLocalBranchExist("branch-5").Returns(false); - var stacks = new List( [ - new("A stack with multiple words", remoteUri, "branch-1", ["branch-3"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("A stack with multiple words", repo.RemoteUri, sourceBranch, [anotherBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) ]); stackConfig.Load().Returns(stacks); stackConfig @@ -375,7 +393,7 @@ public async Task WhenStackHasANameWithMultipleWords_SuggestsAGoodDefaultNewBran .Do(ci => stacks = ci.ArgAt>(0)); inputProvider.Select(Questions.SelectStack, Arg.Any()).Returns("A stack with multiple words"); - inputProvider.Text(Questions.BranchName, "a-stack-with-multiple-words-2").Returns("branch-5"); + inputProvider.Text(Questions.BranchName, "a-stack-with-multiple-words-2").Returns(newBranch); inputProvider.Confirm(Questions.ConfirmSwitchToBranch).Returns(true); // Act @@ -384,9 +402,9 @@ public async Task WhenStackHasANameWithMultipleWords_SuggestsAGoodDefaultNewBran // Assert stacks.Should().BeEquivalentTo(new List { - new("A stack with multiple words", remoteUri, "branch-1", ["branch-3", "branch-5"]), - new("Stack2", remoteUri, "branch-2", ["branch-4"]) + new("A stack with multiple words", repo.RemoteUri, sourceBranch, [anotherBranch, newBranch]), + new("Stack2", repo.RemoteUri, sourceBranch, []) }); - gitOperations.Received().ChangeBranch("branch-5"); + gitOperations.GetCurrentBranch().Should().Be(newBranch); } } diff --git a/src/Stack/Git/GitOperations.cs b/src/Stack/Git/GitOperations.cs index 27be14a..60c9b5a 100644 --- a/src/Stack/Git/GitOperations.cs +++ b/src/Stack/Git/GitOperations.cs @@ -24,7 +24,6 @@ public interface IGitOperations void DeleteLocalBranch(string branchName); void MergeFromLocalSourceBranch(string sourceBranchName); string GetCurrentBranch(); - string GetDefaultBranch(); bool DoesLocalBranchExist(string branchName); bool DoesRemoteBranchExist(string branchName); string[] GetBranchesThatExistLocally(string[] branches); @@ -97,11 +96,6 @@ public string GetCurrentBranch() return ExecuteGitCommandAndReturnOutput("branch --show-current").Trim(); } - public string GetDefaultBranch() - { - return ExecuteGitCommandAndReturnOutput("symbolic-ref refs/remotes/origin/HEAD").Trim().Replace("refs/remotes/origin/", ""); - } - public bool DoesRemoteBranchExist(string branchName) { return ExecuteGitCommandAndReturnOutput($"ls-remote --heads origin {branchName}").Trim().Length > 0; From 38c48e80de7b1a208cb3c501d5d293ad1f54f255 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Wed, 18 Dec 2024 08:57:38 +1100 Subject: [PATCH 9/9] Fix namespace --- src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs b/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs index 8c1ccb3..aec5421 100644 --- a/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs +++ b/src/Stack.Tests/Commands/Branch/NewBranchCommandHandlerTests.cs @@ -7,7 +7,7 @@ using Stack.Infrastructure; using Stack.Tests.Helpers; -namespace Stack.Tests.Commands.Stack; +namespace Stack.Tests.Commands.Branch; public class NewBranchCommandHandlerTests {