Example #1
0
    public async Task Command_can_throw_a_special_exception_which_exits_with_specified_code_and_message()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) =>
        throw new CommandException(""Something went wrong"", 69);
}
");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            Array.Empty <string>(),
            new Dictionary <string, string>()
            );

        var stdOut = FakeConsole.ReadOutputString();
        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().Be(69);
        stdOut.Should().BeEmpty();
        stdErr.Trim().Should().Be("Something went wrong");
    }
Example #2
0
        public async Task Option_binding_fails_if_an_option_of_scalar_type_has_been_provided_with_multiple_values()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    [CommandOption(""foo"")]
    public string Foo { get; set; }
    
    public ValueTask ExecuteAsync(IConsole console) => default;
}");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--foo", "one", "two", "three" },
                new Dictionary <string, string>()
                );

            var stdErr = FakeConsole.ReadErrorString();

            // Assert
            exitCode.Should().NotBe(0);
            stdErr.Should().Contain("expects a single argument, but provided with multiple");
        }
Example #3
0
    public async Task Default_type_activator_can_initialize_a_type_if_it_has_a_parameterless_constructor()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""foo"");
        return default;
    }
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .UseTypeActivator(new DefaultTypeActivator())
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            Array.Empty <string>(),
            new Dictionary <string, string>()
            );

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Trim().Should().Be("foo");
    }
Example #4
0
    public async Task Parameter_binding_fails_if_a_parameter_of_non_scalar_type_has_not_been_provided_with_at_least_one_value()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    [CommandParameter(0)]
    public string Foo { get; set; }

    [CommandParameter(1)]
    public IReadOnlyList<string> Bar { get; set; }

    public ValueTask ExecuteAsync(IConsole console) => default;
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "one" },
            new Dictionary <string, string>()
            );

        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().NotBe(0);
        stdErr.Should().Contain("Missing required parameter(s)");
    }
Example #5
0
        public async Task Help_text_shows_command_description()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command(Description = ""Description of the default command."")]
public class DefaultCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}
");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--help" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().ContainAllInOrder(
                "DESCRIPTION",
                "Description of the default command."
                );
        }
Example #6
0
    public async Task Parameter_binding_fails_if_one_of_the_provided_parameters_is_unexpected()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    [CommandParameter(0)]
    public string Foo { get; set; }

    [CommandParameter(1)]
    public string Bar { get; set; }

    public ValueTask ExecuteAsync(IConsole console) => default;
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "one", "two", "three" },
            new Dictionary <string, string>()
            );

        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().NotBe(0);
        stdErr.Should().Contain("Unexpected parameter(s)");
    }
Example #7
0
    public async Task Default_type_activator_fails_if_the_type_does_not_have_a_parameterless_constructor()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    public Command(string foo) {}

    public ValueTask ExecuteAsync(IConsole console) => default;
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .UseTypeActivator(new DefaultTypeActivator())
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            Array.Empty <string>(),
            new Dictionary <string, string>()
            );

        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().NotBe(0);
        stdErr.Should().Contain("Failed to create an instance of type");
    }
Example #8
0
        public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class DefaultCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}
");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .SetDescription("This will be in help text")
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--help" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().Contain("This will be in help text");
        }
Example #9
0
        public async Task Option_binding_fails_if_one_of_the_provided_option_names_is_not_recognized()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    [CommandOption(""foo"")]
    public string Foo { get; set; }
    
    public ValueTask ExecuteAsync(IConsole console) => default;
}");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--foo", "one", "--bar", "two" },
                new Dictionary <string, string>()
                );

            var stdErr = FakeConsole.ReadErrorString();

            // Assert
            exitCode.Should().NotBe(0);
            stdErr.Should().Contain("Unrecognized option(s)");
        }
Example #10
0
    public async Task Delegate_type_activator_fails_if_the_underlying_function_returns_null()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""foo"");
        return default;
    }
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .UseTypeActivator(_ => null !)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            Array.Empty <string>(),
            new Dictionary <string, string>()
            );

        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().NotBe(0);
        stdErr.Should().Contain("Failed to create an instance of type");
    }
Example #11
0
        public async Task Option_binding_fails_if_a_required_option_has_been_provided_with_an_empty_value()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    [CommandOption(""foo"", IsRequired = true)]
    public string Foo { get; set; }
    
    public ValueTask ExecuteAsync(IConsole console) => default;
}");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--foo" },
                new Dictionary <string, string>()
                );

            var stdErr = FakeConsole.ReadErrorString();

            // Assert
            exitCode.Should().NotBe(0);
            stdErr.Should().Contain("Missing required option(s)");
        }
Example #12
0
        public async Task Help_text_shows_the_implicit_help_and_version_options_on_the_default_command()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}
");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--help" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().ContainAllInOrder(
                "OPTIONS",
                "-h", "--help", "Shows help text",
                "--version", "Shows version information"
                );
        }
Example #13
0
    public async Task Parameter_or_option_value_conversion_fails_if_the_target_type_is_not_supported()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
public class CustomType {}

[Command]
public class Command : ICommand
{
    [CommandOption('f')]
    public CustomType Foo { get; set; }

    public ValueTask ExecuteAsync(IConsole console) => default;
}
");
        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "-f", "xyz" },
            new Dictionary <string, string>()
            );

        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().NotBe(0);
        stdErr.Should().Contain("has an unsupported underlying property type");
    }
Example #14
0
    public async Task Parameter_or_option_value_conversion_fails_if_the_value_cannot_be_converted_to_the_target_type()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    [CommandOption('f')]
    public int Foo { get; set; }

    public ValueTask ExecuteAsync(IConsole console) => default;
}
");
        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "-f", "12.34" },
            new Dictionary <string, string>()
            );

        var stdErr = FakeConsole.ReadErrorString();

        // Assert
        exitCode.Should().NotBe(0);
        stdErr.Should().NotBeNullOrWhiteSpace();
    }
        public void It_passes_ViewModel_owner_correctly_when_null()
        {
            var owner = default(IViewModel);

            var builder = new DynamicCommandBuilder("myCommand", new ActionCommandStrategy(() => { }), owner);;

            builder.ViewModel.Should().BeNull();
        }
        public void It_passes_ViewModel_owner_correctly()
        {
            var owner = new TestViewModel();

            var builder = new DynamicCommandBuilder("myCommand", new ActionCommandStrategy(() => { }), owner);;

            builder.ViewModel.Should().Be(owner);
        }
Example #17
0
        public async Task Help_text_shows_all_immediate_child_commands_of_each_child_command()
        {
            // Arrange
            var commandTypes = DynamicCommandBuilder.CompileMany(
                // language=cs
                @"
[Command(""cmd1"")]
public class FirstCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}

[Command(""cmd1 child1"")]
public class FirstCommandFirstChildCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}

[Command(""cmd2"")]
public class SecondCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}

[Command(""cmd2 child11"")]
public class SecondCommandFirstChildCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}

[Command(""cmd2 child2"")]
public class SecondCommandSecondChildCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}
");

            var application = new CliApplicationBuilder()
                              .AddCommands(commandTypes)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--help" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().ContainAllInOrder(
                "COMMANDS",
                "cmd1", "Subcommands:", "cmd1 child1",
                "cmd2", "Subcommands:", "cmd2 child1", "cmd2 child2"
                );
        }
Example #18
0
    public async Task Parameter_of_non_scalar_type_is_bound_from_remaining_non_option_arguments()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    [CommandParameter(0)]
    public string Foo { get; set; }

    [CommandParameter(1)]
    public string Bar { get; set; }

    [CommandParameter(2)]
    public IReadOnlyList<string> Baz { get; set; }

    [CommandOption(""boo"")]
    public string Boo { get; set; }

    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""Foo = "" + Foo);
        console.Output.WriteLine(""Bar = "" + Bar);

        foreach (var i in Baz)
            console.Output.WriteLine(""Baz = "" + i);

        return default;
    }
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "one", "two", "three", "four", "five", "--boo", "xxx" },
            new Dictionary <string, string>()
            );

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Should().ConsistOfLines(
            "Foo = one",
            "Bar = two",
            "Baz = three",
            "Baz = four",
            "Baz = five"
            );
    }
        public void WithStrategy_properly_adds()
        {
            var builder = new DynamicCommandBuilder("myCommand", new ActionCommandStrategy(() => { }), null);

            builder.Strategies.Should().BeEmpty();

            builder.WithStrategy(new BackgroundCommandStrategy());

            builder.Strategies.Should().ContainSingle();
        }
Example #20
0
        public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
        {
            // Arrange
            var commandTypes = DynamicCommandBuilder.CompileMany(
                // language=cs
                @"
[Command]
public class DefaultCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""default"");
        return default;
    }
}

[Command(""cmd"")]
public class NamedCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""cmd"");
        return default;
    }
}

[Command(""cmd child"")]
public class NamedChildCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""cmd child"");
        return default;
    }
}
");

            var application = new CliApplicationBuilder()
                              .AddCommands(commandTypes)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "cmd" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Trim().Should().Be("cmd");
        }
Example #21
0
    public async Task Fake_console_does_not_leak_to_system_console()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{   
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.ResetColor();
        console.ForegroundColor = ConsoleColor.DarkMagenta;
        console.BackgroundColor = ConsoleColor.DarkMagenta;
        console.CursorLeft = 42;
        console.CursorTop = 24;

        console.Output.WriteLine(""Hello "");
        console.Error.WriteLine(""world!"");

        return default;
    }
}
");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            Array.Empty <string>(),
            new Dictionary <string, string>()
            );

        // Assert
        exitCode.Should().Be(0);

        Console.OpenStandardInput().Should().NotBeSameAs(FakeConsole.Input.BaseStream);
        Console.OpenStandardOutput().Should().NotBeSameAs(FakeConsole.Output.BaseStream);
        Console.OpenStandardError().Should().NotBeSameAs(FakeConsole.Error.BaseStream);

        Console.ForegroundColor.Should().NotBe(ConsoleColor.DarkMagenta);
        Console.BackgroundColor.Should().NotBe(ConsoleColor.DarkMagenta);

        // This fails because tests don't spawn a console window
        //Console.CursorLeft.Should().NotBe(42);
        //Console.CursorTop.Should().NotBe(24);

        FakeConsole.IsInputRedirected.Should().BeTrue();
        FakeConsole.IsOutputRedirected.Should().BeTrue();
        FakeConsole.IsErrorRedirected.Should().BeTrue();
    }
Example #22
0
        public async Task Help_text_shows_all_immediate_child_commands()
        {
            // Arrange
            var commandTypes = DynamicCommandBuilder.CompileMany(
                // language=cs
                @"
[Command(""cmd1"", Description = ""Description of one command."")]
public class FirstCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}

[Command(""cmd2"", Description = ""Description of another command."")]
public class SecondCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}

[Command(""cmd2 child"", Description = ""Description of another command's child command."")]
public class SecondCommandChildCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console) => default;
}
");

            var application = new CliApplicationBuilder()
                              .AddCommands(commandTypes)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--help" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().ContainAllInOrder(
                "COMMANDS",
                "cmd1", "Description of one command.",
                "cmd2", "Description of another command."
                );

            // `cmd2 child` will still appear in the list of `cmd2` subcommands,
            // but its description will not be seen.
            stdOut.Should().NotContain(
                "Description of another command's child command."
                );
        }
        public void WithoutStrategy_properly_removes()
        {
            var builder = new DynamicCommandBuilder("myCommand", new ActionCommandStrategy(() => { }), null)
                          .OnBackgroundThread()
                          .Locked()
                          .DisableWhileExecuting();

            builder.Strategies.Should().HaveCount(3);

            builder.WithoutStrategy <BackgroundCommandStrategy>();

            builder.Strategies.Should().HaveCount(2);
        }
        public void ClearStrategies_properly_clears()
        {
            var builder = new DynamicCommandBuilder("myCommand", new ActionCommandStrategy(() => { }), null)
                          .OnBackgroundThread()
                          .Locked()
                          .DisableWhileExecuting();


            builder.Strategies.Should().NotBeEmpty();

            builder.ClearStrategies();

            builder.Strategies.Should().BeEmpty();
        }
Example #25
0
    public async Task Help_text_shows_usage_format_which_lists_all_parameters_in_specified_order()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
// Base members appear last in reflection order
public abstract class CommandBase : ICommand
{
    [CommandParameter(0)]
    public string Foo { get; set; }

    public abstract ValueTask ExecuteAsync(IConsole console);
}

[Command]
public class Command : CommandBase
{
    [CommandParameter(2)]
    public IReadOnlyList<string> Baz { get; set; }

    [CommandParameter(1)]
    public string Bar { get; set; }

    public override ValueTask ExecuteAsync(IConsole console) => default;
}
");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "--help" },
            new Dictionary <string, string>()
            );

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Should().ContainAllInOrder(
            "USAGE",
            "<foo>", "<bar>", "<baz...>"
            );
    }
Example #26
0
        public async Task Command_can_register_to_receive_a_cancellation_signal_from_the_console()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    public async ValueTask ExecuteAsync(IConsole console)
    {
        try
        {
            await Task.Delay(
                TimeSpan.FromSeconds(3),
                console.RegisterCancellationHandler()
            );

            console.Output.WriteLine(""Completed successfully"");
        }
        catch (OperationCanceledException)
        {
            console.Output.WriteLine(""Cancelled"");
            throw;
        }
    }
}");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            FakeConsole.RequestCancellation(TimeSpan.FromSeconds(0.2));

            var exitCode = await application.RunAsync(
                Array.Empty <string>(),
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().NotBe(0);
            stdOut.Trim().Should().Be("Cancelled");
        }
Example #27
0
    public async Task Parameter_or_option_value_can_be_converted_to_a_nullable_enum()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
public enum CustomEnum { One = 1, Two = 2, Three = 3 }

[Command]
public class Command : ICommand
{
    [CommandOption('f')]
    public CustomEnum? Foo { get; set; }

    [CommandOption('b')]
    public CustomEnum? Bar { get; set; }

    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""Foo = "" + (int?) Foo);
        console.Output.WriteLine(""Bar = "" + (int?) Bar);

        return default;
    }
}
");
        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "-b", "two" },
            new Dictionary <string, string>()
            );

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Should().ConsistOfLines(
            "Foo = ",
            "Bar = 2"
            );
    }
Example #28
0
        public async Task Option_of_non_scalar_type_can_receive_multiple_values_from_an_environment_variable()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    [CommandOption(""foo"", EnvironmentVariable = ""ENV_FOO"")]
    public IReadOnlyList<string> Foo { get; set; }
    
    public ValueTask ExecuteAsync(IConsole console)
    {
        foreach (var i in Foo)
            console.Output.WriteLine(i);
        
        return default;
    }
}
");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                Array.Empty <string>(),
                new Dictionary <string, string>
            {
                ["ENV_FOO"] = $"bar{Path.PathSeparator}baz"
            }
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().ConsistOfLines(
                "bar",
                "baz"
                );
        }
Example #29
0
        public async Task Option_is_not_bound_if_there_are_no_arguments_matching_its_name_or_short_name()
        {
            // Arrange
            var commandType = DynamicCommandBuilder.Compile(
                // language=cs
                @"
[Command]
public class Command : ICommand
{
    [CommandOption(""foo"")]
    public string Foo { get; set; }
    
    [CommandOption(""bar"")]
    public string Bar { get; set; } = ""hello"";
    
    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""Foo = "" + Foo);
        console.Output.WriteLine(""Bar = "" + Bar);

        return default;
    }
}");

            var application = new CliApplicationBuilder()
                              .AddCommand(commandType)
                              .UseConsole(FakeConsole)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(
                new[] { "--foo", "one" },
                new Dictionary <string, string>()
                );

            var stdOut = FakeConsole.ReadOutputString();

            // Assert
            exitCode.Should().Be(0);
            stdOut.Should().ConsistOfLines(
                "Foo = one",
                "Bar = hello"
                );
        }
Example #30
0
    public async Task Parameter_is_bound_from_an_argument_matching_its_order()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
[Command]
public class Command : ICommand
{
    [CommandParameter(0)]
    public string Foo { get; set; }

    [CommandParameter(1)]
    public string Bar { get; set; }

    public ValueTask ExecuteAsync(IConsole console)
    {
        console.Output.WriteLine(""Foo = "" + Foo);
        console.Output.WriteLine(""Bar = "" + Bar);

        return default;
    }
}");

        var application = new CliApplicationBuilder()
                          .AddCommand(commandType)
                          .UseConsole(FakeConsole)
                          .Build();

        // Act
        var exitCode = await application.RunAsync(
            new[] { "one", "two" },
            new Dictionary <string, string>()
            );

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Should().ConsistOfLines(
            "Foo = one",
            "Bar = two"
            );
    }