From eb38f76a6a988acbda3c955a8f6b72083a622bfb Mon Sep 17 00:00:00 2001 From: BlazeFace Date: Sat, 13 Apr 2024 05:40:12 -0700 Subject: [PATCH] Fixing #1507 AddDelegate uses an abstract type when used in a branch (#1509) --- .../ConfiguratorExtensions.cs | 9 +++- .../Unit/CommandAppTests.cs | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Spectre.Console.Cli/ConfiguratorExtensions.cs b/src/Spectre.Console.Cli/ConfiguratorExtensions.cs index c0f1cd814..eb3f1be19 100644 --- a/src/Spectre.Console.Cli/ConfiguratorExtensions.cs +++ b/src/Spectre.Console.Cli/ConfiguratorExtensions.cs @@ -324,11 +324,16 @@ public static ICommandConfigurator AddAsyncDelegate( /// The delegate to execute as part of command execution. /// A command configurator that can be used to configure the command further. public static ICommandConfigurator AddDelegate( - this IConfigurator configurator, + this IConfigurator? configurator, string name, Func func) - where TSettings : CommandSettings + where TSettings : CommandSettings { + if (typeof(TSettings).IsAbstract) + { + AddDelegate(configurator as IConfigurator, name, func); + } + if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); diff --git a/test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs b/test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs index 76b7581de..7004c8ffe 100644 --- a/test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs +++ b/test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs @@ -1136,6 +1136,55 @@ public void Should_Execute_Delegate_Command_At_Root_Level() 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() {