From a0431fa33b81706b3af849e58671ed09e0394581 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:07:28 +0100 Subject: [PATCH 1/5] Unit tests for version behaviour --- .../Unit/CommandAppTests.Version.cs | 97 +++++++++++++++---- 1 file changed, 78 insertions(+), 19 deletions(-) diff --git a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs index e0a48ee26..d47670f6e 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs @@ -1,3 +1,5 @@ +using System.Runtime.InteropServices; + namespace Spectre.Console.Tests.Unit.Cli; public sealed partial class CommandAppTests @@ -17,8 +19,10 @@ public void Should_Output_CLI_Version_To_The_Console() result.Output.ShouldStartWith("Spectre.Cli version "); } - [Fact] - public void Should_Output_Application_Version_To_The_Console_With_No_Command() + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Output_Application_Version_To_The_Console_With_No_Command(string versionOption) { // Given var fixture = new CommandAppTester(); @@ -28,14 +32,16 @@ public void Should_Output_Application_Version_To_The_Console_With_No_Command() }); // When - var result = fixture.Run("--version"); + var result = fixture.Run(versionOption); // Then result.Output.ShouldBe("1.0"); } - [Fact] - public void Should_Not_Display_Version_If_Not_Specified() + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Not_Display_Version_If_Not_Specified(string versionOption) { // Given var fixture = new CommandAppTester(); @@ -45,15 +51,17 @@ public void Should_Not_Display_Version_If_Not_Specified() }); // When - var result = fixture.Run("--version"); + var result = fixture.Run(versionOption); // Then result.ExitCode.ShouldNotBe(0); - result.Output.ShouldStartWith("Error: Unexpected option 'version'"); + result.Output.ShouldStartWith($"Error: Unexpected option '{versionOption.Replace("-", "")}'"); } - [Fact] - public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console() + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console(string versionOption) { // Given var fixture = new CommandAppTester(); @@ -64,15 +72,17 @@ public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console }); // When - var result = fixture.Run("empty", "--version"); + var result = fixture.Run("empty", versionOption); // Then result.Output.ShouldBe(string.Empty); - result.Context.ShouldHaveRemainingArgument("--version", new[] { (string)null }); + result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null }); } - [Fact] - public void Should_Execute_Default_Command_Not_Output_Application_Version_To_The_Console() + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Output_Application_Version_To_The_Console_With_Default_Command(string versionOption) { // Given var fixture = new CommandAppTester(); @@ -83,15 +93,16 @@ public void Should_Execute_Default_Command_Not_Output_Application_Version_To_The }); // When - var result = fixture.Run("--version"); + var result = fixture.Run(versionOption); // Then - result.Output.ShouldBe(string.Empty); - result.Context.ShouldHaveRemainingArgument("--version", new[] { (string)null }); + result.Output.ShouldBe("1.0"); } - [Fact] - public void Should_Output_Application_Version_To_The_Console_With_Branch_Default_Command() + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Output_Application_Version_To_The_Console_With_Branch_Default_Command(string versionOption) { // Given var fixture = new CommandAppTester(); @@ -105,10 +116,58 @@ public void Should_Output_Application_Version_To_The_Console_With_Branch_Default }); // When - var result = fixture.Run("--version"); + var result = fixture.Run(versionOption); // Then result.Output.ShouldBe("1.0"); } + + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_Branch_Default_Command_Not_Output_Application_Version_To_The_Console(string versionOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.SetDefaultCommand(); + }); + }); + + // When + var result = fixture.Run("branch", versionOption); + + // Then + result.Output.ShouldBe(string.Empty); + result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null }); + } + + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_Branch_Command_Not_Output_Application_Version_To_The_Console(string versionOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.AddCommand("hello"); + }); + }); + + // When + var result = fixture.Run("branch", "hello", versionOption); + + // Then + result.Output.ShouldBe(string.Empty); + result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null }); + } } } From 962d44d8e118d7ba2f1896c099cc5562703df44a Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:08:13 +0100 Subject: [PATCH 2/5] Removed unnecessary using directive --- .../Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs index d47670f6e..9894946a0 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace Spectre.Console.Tests.Unit.Cli; public sealed partial class CommandAppTests From 015a5dd11192b673480416038ad04467d3db8a15 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:09:01 +0100 Subject: [PATCH 3/5] Unit tests covering when the -v|--version should appear in the help output --- .../Data/Settings/VersionSettings.cs | 1 + .../Unit/CommandAppTests.Version.Help.cs | 242 ++++++++++++++++++ .../Unit/CommandAppTests.Version.cs | 2 +- 3 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs diff --git a/src/Tests/Spectre.Console.Cli.Tests/Data/Settings/VersionSettings.cs b/src/Tests/Spectre.Console.Cli.Tests/Data/Settings/VersionSettings.cs index 2b54e8cd2..e84c9e79e 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Data/Settings/VersionSettings.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Data/Settings/VersionSettings.cs @@ -3,5 +3,6 @@ namespace Spectre.Console.Tests.Data; public sealed class VersionSettings : CommandSettings { [CommandOption("-v|--version")] + [Description("The command version")] public string Version { get; set; } } \ No newline at end of file diff --git a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs new file mode 100644 index 000000000..ddf7b387d --- /dev/null +++ b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs @@ -0,0 +1,242 @@ +namespace Spectre.Console.Tests.Unit.Cli; + +public sealed partial class CommandAppTests +{ + public sealed partial class Version + { + public sealed class Help + { + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Include_Application_Version_Flag_With_No_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + }); + + // When + var result = fixture.Run(helpOption); + + // Then + result.Output.ShouldContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Not_Include_Application_Version_Flag_If_Not_Specified(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.AddCommand("empty"); + }); + + // When + var result = fixture.Run(helpOption); + + // Then + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Not_Include_Application_Version_Flag_For_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddCommand("empty"); + }); + + // When + var result = fixture.Run("empty", helpOption); + + // Then + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Include_Application_Version_Flag_For_Default_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.SetDefaultCommand(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + }); + + // When + var result = fixture.Run(helpOption); + + // Then + result.Output.ShouldContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Not_Include_Application_Version_Flag_For_Branch_Default_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.SetDefaultCommand(); + }); + }); + + // When + var result = fixture.Run("branch", helpOption); + + // Then + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Not_Include_Application_Version_Flag_For_Branch_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.AddCommand("hello"); + }); + }); + + // When + var result = fixture.Run("branch", "hello", helpOption); + + // Then + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Include_Command_Version_Flag_For_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddCommand("empty"); + }); + + // When + var result = fixture.Run("empty", helpOption); + + // Then + result.Output.ShouldContain("-v, --version The command version"); + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + /// + /// When a command with a version flag in the settings is set as the application default command, + /// then override the in-built Application Version flag with the command version flag instead. + /// Rationale: This behaviour makes the most sense because the other flags for the default command + /// will be shown in the help output and the user can set any of these when executing the application. + /// + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Include_Command_Version_Flag_For_Default_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.SetDefaultCommand(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + }); + + // When + var result = fixture.Run(helpOption); + + // Then + result.Output.ShouldContain("-v, --version The command version"); + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Include_Command_Version_Flag_For_Branch_Default_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.SetDefaultCommand(); + }); + }); + + // When + var result = fixture.Run("branch", helpOption); + + // Then + result.Output.ShouldContain("-v, --version The command version"); + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + [Theory] + [InlineData("-?")] + [InlineData("-h")] + [InlineData("--help")] + public void Help_Should_Include_Command_Version_Flag_For_Branch_Command(string helpOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.AddCommand("hello"); + }); + }); + + // When + var result = fixture.Run("branch", "hello", helpOption); + + // Then + result.Output.ShouldContain("-v, --version The command version"); + result.Output.ShouldNotContain("-v, --version Prints version information"); + } + + + } + } +} \ No newline at end of file diff --git a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs index 9894946a0..30835ad7a 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs @@ -2,7 +2,7 @@ namespace Spectre.Console.Tests.Unit.Cli; public sealed partial class CommandAppTests { - public sealed class Version + public sealed partial class Version { [Fact] public void Should_Output_CLI_Version_To_The_Console() From b510bf756dd42f51bf0fcc159c85fa28fbb862dd Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:09:28 +0100 Subject: [PATCH 4/5] Improved unit test coverage using Spectre.Console.Tests.Data.VersionCommand --- .../Unit/CommandAppTests.Version.Help.cs | 50 ++++---- .../Unit/CommandAppTests.Version.cs | 116 +++++++++++++++--- 2 files changed, 123 insertions(+), 43 deletions(-) diff --git a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs index ddf7b387d..b2cdec014 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.Help.cs @@ -34,10 +34,6 @@ public void Help_Should_Not_Include_Application_Version_Flag_If_Not_Specified(st { // Given var fixture = new CommandAppTester(); - fixture.Configure(configurator => - { - configurator.AddCommand("empty"); - }); // When var result = fixture.Run(helpOption); @@ -50,42 +46,42 @@ public void Help_Should_Not_Include_Application_Version_Flag_If_Not_Specified(st [InlineData("-?")] [InlineData("-h")] [InlineData("--help")] - public void Help_Should_Not_Include_Application_Version_Flag_For_Command(string helpOption) + public void Help_Should_Include_Application_Version_Flag_For_Default_Command(string helpOption) { // Given var fixture = new CommandAppTester(); + fixture.SetDefaultCommand(); fixture.Configure(configurator => { configurator.SetApplicationVersion("1.0"); - configurator.AddCommand("empty"); }); // When - var result = fixture.Run("empty", helpOption); + var result = fixture.Run(helpOption); // Then - result.Output.ShouldNotContain("-v, --version Prints version information"); + result.Output.ShouldContain("-v, --version Prints version information"); } [Theory] [InlineData("-?")] [InlineData("-h")] [InlineData("--help")] - public void Help_Should_Include_Application_Version_Flag_For_Default_Command(string helpOption) + public void Help_Should_Not_Include_Application_Version_Flag_For_Command(string helpOption) { // Given var fixture = new CommandAppTester(); - fixture.SetDefaultCommand(); fixture.Configure(configurator => { configurator.SetApplicationVersion("1.0"); + configurator.AddCommand("empty"); }); // When - var result = fixture.Run(helpOption); + var result = fixture.Run("empty", helpOption); // Then - result.Output.ShouldContain("-v, --version Prints version information"); + result.Output.ShouldNotContain("-v, --version Prints version information"); } [Theory] @@ -125,61 +121,61 @@ public void Help_Should_Not_Include_Application_Version_Flag_For_Branch_Command( configurator.SetApplicationVersion("1.0"); configurator.AddBranch("branch", branch => { - branch.AddCommand("hello"); + branch.AddCommand("empty"); }); }); // When - var result = fixture.Run("branch", "hello", helpOption); + var result = fixture.Run("branch", "empty", helpOption); // Then result.Output.ShouldNotContain("-v, --version Prints version information"); } + /// + /// When a command with a version flag in the settings is set as the application default command, + /// then override the in-built Application Version flag with the command version flag instead. + /// Rationale: This behaviour makes the most sense because the other flags for the default command + /// will be shown in the help output and the user can set any of these when executing the application. + /// [Theory] [InlineData("-?")] [InlineData("-h")] [InlineData("--help")] - public void Help_Should_Include_Command_Version_Flag_For_Command(string helpOption) + public void Help_Should_Include_Command_Version_Flag_For_Default_Command(string helpOption) { // Given var fixture = new CommandAppTester(); + fixture.SetDefaultCommand(); fixture.Configure(configurator => { configurator.SetApplicationVersion("1.0"); - configurator.AddCommand("empty"); }); // When - var result = fixture.Run("empty", helpOption); + var result = fixture.Run(helpOption); // Then result.Output.ShouldContain("-v, --version The command version"); result.Output.ShouldNotContain("-v, --version Prints version information"); } - /// - /// When a command with a version flag in the settings is set as the application default command, - /// then override the in-built Application Version flag with the command version flag instead. - /// Rationale: This behaviour makes the most sense because the other flags for the default command - /// will be shown in the help output and the user can set any of these when executing the application. - /// [Theory] [InlineData("-?")] [InlineData("-h")] [InlineData("--help")] - public void Help_Should_Include_Command_Version_Flag_For_Default_Command(string helpOption) + public void Help_Should_Include_Command_Version_Flag_For_Command(string helpOption) { // Given var fixture = new CommandAppTester(); - fixture.SetDefaultCommand(); fixture.Configure(configurator => { configurator.SetApplicationVersion("1.0"); + configurator.AddCommand("hello"); }); // When - var result = fixture.Run(helpOption); + var result = fixture.Run("hello", helpOption); // Then result.Output.ShouldContain("-v, --version The command version"); @@ -235,8 +231,6 @@ public void Help_Should_Include_Command_Version_Flag_For_Branch_Command(string h result.Output.ShouldContain("-v, --version The command version"); result.Output.ShouldNotContain("-v, --version Prints version information"); } - - } } } \ No newline at end of file diff --git a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs index 30835ad7a..d416a89c6 100644 --- a/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs +++ b/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs @@ -43,10 +43,6 @@ public void Should_Not_Display_Version_If_Not_Specified(string versionOption) { // Given var fixture = new CommandAppTester(); - fixture.Configure(configurator => - { - configurator.AddCommand("empty"); - }); // When var result = fixture.Run(versionOption); @@ -59,42 +55,42 @@ public void Should_Not_Display_Version_If_Not_Specified(string versionOption) [Theory] [InlineData("-v")] [InlineData("--version")] - public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console(string versionOption) + public void Should_Output_Application_Version_To_The_Console_With_Default_Command(string versionOption) { // Given var fixture = new CommandAppTester(); + fixture.SetDefaultCommand(); fixture.Configure(configurator => { configurator.SetApplicationVersion("1.0"); - configurator.AddCommand("empty"); }); // When - var result = fixture.Run("empty", versionOption); + var result = fixture.Run(versionOption); // Then - result.Output.ShouldBe(string.Empty); - result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null }); + result.Output.ShouldBe("1.0"); } [Theory] [InlineData("-v")] [InlineData("--version")] - public void Should_Output_Application_Version_To_The_Console_With_Default_Command(string versionOption) + public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console(string versionOption) { // Given var fixture = new CommandAppTester(); - fixture.SetDefaultCommand(); fixture.Configure(configurator => { configurator.SetApplicationVersion("1.0"); + configurator.AddCommand("empty"); }); // When - var result = fixture.Run(versionOption); + var result = fixture.Run("empty", versionOption); // Then - result.Output.ShouldBe("1.0"); + result.Output.ShouldBe(string.Empty); + result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null }); } [Theory] @@ -156,16 +152,106 @@ public void Should_Execute_Branch_Command_Not_Output_Application_Version_To_The_ configurator.SetApplicationVersion("1.0"); configurator.AddBranch("branch", branch => { - branch.AddCommand("hello"); + branch.AddCommand("empty"); }); }); // When - var result = fixture.Run("branch", "hello", versionOption); + var result = fixture.Run("branch", "empty", versionOption); // Then result.Output.ShouldBe(string.Empty); result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null }); } + + /// + /// When a command with a version flag in the settings is set as the application default command, + /// then execute this command instead of displaying the explicitly set Application Version. + /// + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_Default_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.SetDefaultCommand(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + }); + + // When + var result = fixture.Run(versionOption, "X.Y.Z"); + + // Then + result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z"); + } + + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddCommand("hello"); + }); + + // When + var result = fixture.Run("hello", versionOption, "X.Y.Z"); + + // Then + result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z"); + } + + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_Branch_Default_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.SetDefaultCommand(); + }); + }); + + // When + var result = fixture.Run("branch", versionOption, "X.Y.Z"); + + // Then + result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z"); + } + + [Theory] + [InlineData("-v")] + [InlineData("--version")] + public void Should_Execute_Branch_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption) + { + // Given + var fixture = new CommandAppTester(); + fixture.Configure(configurator => + { + configurator.SetApplicationVersion("1.0"); + configurator.AddBranch("branch", branch => + { + branch.AddCommand("hello"); + }); + }); + + // When + var result = fixture.Run("branch", "hello", versionOption, "X.Y.Z"); + + // Then + result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z"); + } } } From acb89d7c8bd3590b99527369354c723a363bc86d Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Sun, 6 Oct 2024 12:59:34 +0100 Subject: [PATCH 5/5] Display the application version (if set) when a version flag is the first argument, even if a default command has been set. --- .../Internal/CommandExecutor.cs | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Spectre.Console.Cli/Internal/CommandExecutor.cs b/src/Spectre.Console.Cli/Internal/CommandExecutor.cs index 22eeb2084..dd1a4a0a0 100644 --- a/src/Spectre.Console.Cli/Internal/CommandExecutor.cs +++ b/src/Spectre.Console.Cli/Internal/CommandExecutor.cs @@ -27,24 +27,20 @@ public async Task Execute(IConfiguration configuration, IEnumerable _registrar.RegisterInstance(typeof(CommandModel), model); _registrar.RegisterDependencies(model); - // No default command? - if (model.DefaultCommand == null) + // Got at least one argument? + var firstArgument = arguments.FirstOrDefault(); + if (firstArgument != null) { - // Got at least one argument? - var firstArgument = arguments.FirstOrDefault(); - if (firstArgument != null) + // Asking for version? Kind of a hack, but it's alright. + // We should probably make this a bit better in the future. + if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) || + firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase)) { - // Asking for version? Kind of a hack, but it's alright. - // We should probably make this a bit better in the future. - if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) || - firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase)) + if (configuration.Settings.ApplicationVersion != null) { - if (configuration.Settings.ApplicationVersion != null) - { - var console = configuration.Settings.Console.GetConsole(); - console.MarkupLine(configuration.Settings.ApplicationVersion); - return 0; - } + var console = configuration.Settings.Console.GetConsole(); + console.MarkupLine(configuration.Settings.ApplicationVersion); + return 0; } } }