From 4ac88b5d3f6d1912406fffc7bf7920143d14ab13 Mon Sep 17 00:00:00 2001 From: Frank Ray <52075808+FrankRay78@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:32:26 +0100 Subject: [PATCH] Help writer unit tests; including coverage of when the -v|--version should (and shouldn't) 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 2b54e8c..e84c9e7 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 0000000..ddf7b38 --- /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 ccb913c..8b2cb02 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 { [Theory] [InlineData(false)]