spectreconsole/spectre.console

Parsed unknown flag is not being added to remaining arguments for a branch (or branch of a branch) with a default command

FrankRay78 opened this issue · 0 comments

Information

  • OS: Windows 10
  • Version: 0.49.1
  • Terminal: N/A (produced in a unit test)

Describe the bug
Parsed unknown flag is not being added to remaining arguments for a branch (or branch of a branch) with a default command.

To Reproduce

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.SetDefaultCommand<EmptyCommand>();
                });
            });

            // When
            var result = app.Run("branch", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddBranch<EmptyCommandSettings>("hello", hello =>
                    {
                        hello.SetDefaultCommand<EmptyCommand>();
                    });
                });
            });

            // When
            var result = app.Run("branch", "hello", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

Error message (showing empty remaining arguments):

 Spectre.Console.Tests.Unit.Cli.CommandAppTests+Remaining.Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command
   Source: CommandAppTests.Remaining.cs line 41
   Duration: 46 ms

  Message: 
Shouldly.ShouldAssertException : context.Remaining.Parsed.Contains(name)
    should be
True
    but was
False

  Stack Trace: 
CommandContextExtensions.ShouldHaveRemainingArgument(CommandContext context, String name, String[] values) line 17
Remaining.Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command() line 58
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Expected behavior
The above tests both pass.
The remaining arguments contain '--felix'.

Additional context
I wrote several unit tests to validate the issue only occurs when a default command has been set on a branch (or branch of a branch), see:

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.SetDefaultCommand<EmptyCommand>();

            // When
            var result = app.Run("--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddCommand<EmptyCommand>("empty");
            });

            // When
            var result = app.Run("empty", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.SetDefaultCommand<EmptyCommand>();
                });
            });

            // When
            var result = app.Run("branch", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddCommand<EmptyCommand>("hello");
                });
            });

            // When
            var result = app.Run("branch", "hello", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddBranch<EmptyCommandSettings>("hello", hello =>
                    {
                        hello.SetDefaultCommand<EmptyCommand>();
                    });
                });
            });

            // When
            var result = app.Run("branch", "hello", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Branch_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddBranch<EmptyCommandSettings>("hello", hello =>
                    {
                        hello.AddCommand<EmptyCommand>("world");
                    });
                });
            });

            // When
            var result = app.Run("branch", "hello", "world", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

image


Please upvote 👍 this issue if you are interested in it.