Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI version behaviour bug fixes (DRAFT) #1661

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/Spectre.Console.Cli/Internal/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,20 @@ public async Task<int> Execute(IConfiguration configuration, IEnumerable<string>
_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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
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();

// 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_Include_Application_Version_Flag_For_Default_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<EmptyCommand>();
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_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<EmptyCommand>("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_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<EmptyCommandSettings>("branch", branch =>
{
branch.SetDefaultCommand<EmptyCommand>();
});
});

// 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<EmptyCommandSettings>("branch", branch =>
{
branch.AddCommand<EmptyCommand>("empty");
});
});

// When
var result = fixture.Run("branch", "empty", helpOption);

// Then
result.Output.ShouldNotContain("-v, --version Prints version information");
}

/// <summary>
/// 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.
/// </summary>
[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<Spectre.Console.Tests.Data.VersionCommand>();
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_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<Spectre.Console.Tests.Data.VersionCommand>("hello");
});

// When
var result = fixture.Run("hello", 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<VersionSettings>("branch", branch =>
{
branch.SetDefaultCommand<Spectre.Console.Tests.Data.VersionCommand>();
});
});

// 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<VersionSettings>("branch", branch =>
{
branch.AddCommand<Spectre.Console.Tests.Data.VersionCommand>("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");
}
}
}
}
Loading
Loading