Fixing #1507 AddDelegate uses an abstract type when used in a branch (#1509)

This commit is contained in:
BlazeFace 2024-04-13 05:40:12 -07:00 committed by GitHub
parent fc0b553a4a
commit eb38f76a6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 2 deletions

View File

@ -324,11 +324,16 @@ public static class ConfiguratorExtensions
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddDelegate<TSettings>(
this IConfigurator<TSettings> configurator,
this IConfigurator<TSettings>? configurator,
string name,
Func<CommandContext, int> func)
where TSettings : CommandSettings
where TSettings : CommandSettings
{
if (typeof(TSettings).IsAbstract)
{
AddDelegate(configurator as IConfigurator<EmptyCommandSettings>, name, func);
}
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));

View File

@ -1136,6 +1136,55 @@ public sealed partial class CommandAppTests
data.ShouldBe(2);
}
[Fact]
public void Should_Execute_Nested_Delegate_Empty_Command()
{
// Given
var app = new CommandAppTester();
app.Configure(cfg =>
{
cfg.AddBranch("a", d =>
{
d.AddDelegate("b", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});
});
// When
var result = app.Run([
"a", "b"
]);
// Then
result.ExitCode.ShouldBe(0);
}
[Fact]
public void Should_Execute_Delegate_Empty_Command_At_Root_Level()
{
// Given
var app = new CommandAppTester();
app.Configure(cfg =>
{
cfg.AddDelegate("a", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});
// When
var result = app.Run([
"a"
]);
// Then
result.ExitCode.ShouldBe(0);
}
[Fact]
public async void Should_Execute_Async_Delegate_Command_At_Root_Level()
{