Esempio n. 1
0
        public static async ValueTask <(int exitCode, MemoryStreamWriter stdOut, MemoryStreamWriter stdErr)> BuildAndRunTestAsync(this CliApplicationBuilder applicationBuilder,
                                                                                                                                  ITestOutputHelper testOutput,
                                                                                                                                  IReadOnlyList <string> commandLineArguments,
                                                                                                                                  IReadOnlyDictionary <string, string> environmentVariables,
                                                                                                                                  bool isInputRedirected = true)
        {
            var(console, stdOut, stdErr) = VirtualConsole.CreateBuffered(isInputRedirected: isInputRedirected);

            CliApplication application = applicationBuilder.UseConsole(console)
                                         .Build();

            int exitCode = await application.RunAsync(commandLineArguments, environmentVariables);

            testOutput.WriteLine("Exit Code: {0}", exitCode);
            testOutput.Print(stdOut, stdErr);

            return(exitCode, stdOut, stdErr);
        }
Esempio n. 2
0
 public static async ValueTask <(int exitCode, MemoryStreamWriter stdOut, MemoryStreamWriter stdErr)> BuildAndRunTestAsync(this CliApplicationBuilder applicationBuilder,
                                                                                                                           ITestOutputHelper testOutput,
                                                                                                                           IReadOnlyList <string> commandLineArguments,
                                                                                                                           bool isInputRedirected = true)
 {
     return(await applicationBuilder.BuildAndRunTestAsync(testOutput,
                                                          commandLineArguments,
                                                          new Dictionary <string, string>(),
                                                          isInputRedirected));
 }
Esempio n. 3
0
    public async Task Parameter_or_option_value_can_be_converted_to_a_type_that_has_a_static_parse_method()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
public class CustomTypeA
{
    public string Value { get; }

    private CustomTypeA(string value) => Value = value;

    public static CustomTypeA Parse(string value) => 
        new CustomTypeA(value);
}

public class CustomTypeB
{
    public string Value { get; }

    private CustomTypeB(string value) => Value = value;

    public static CustomTypeB Parse(string value, IFormatProvider formatProvider) =>
        new CustomTypeB(value);
}

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

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

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

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

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

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Should().ConsistOfLines(
            "Foo = hello",
            "Bar = world"
            );
    }
Esempio n. 4
0
 public static async ValueTask <(int exitCode, MemoryStreamWriter stdOut, MemoryStreamWriter stdErr)> BuildAndRunTestAsync(this CliApplicationBuilder applicationBuilder,
                                                                                                                           ITestOutputHelper testOutput,
                                                                                                                           bool isInputRedirected = true)
 {
     return(await applicationBuilder.BuildAndRunTestAsync(testOutput,
                                                          Array.Empty <string>(),
                                                          new Dictionary <string, string>(),
                                                          isInputRedirected));
 }
Esempio n. 5
0
    public async Task Option_binding_supports_multiple_inheritance_through_default_interface_members()
    {
        // Arrange
        var commandType = DynamicCommandBuilder.Compile(
            // language=cs
            @"
public static class SharedContext
{
    public static int Foo { get; set; }

    public static bool Bar { get; set; }
}

public interface IHasFoo : ICommand
{
    [CommandOption(""foo"")]
    public int Foo
    {
        get => SharedContext.Foo;
        set => SharedContext.Foo = value;
    }
}

public interface IHasBar : ICommand
{
    [CommandOption(""bar"")]
    public bool Bar
    {
        get => SharedContext.Bar;
        set => SharedContext.Bar = value;
    }
}

public interface IHasBaz : ICommand
{
    public string Baz { get; set; }
}

[Command]
public class Command : IHasFoo, IHasBar, IHasBaz
{
    [CommandOption(""baz"")]
    public string Baz { get; set; }

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

        return default;
    }
}
");

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

        // Act
        var exitCode = await application.RunAsync(
            new[] { "--foo", "42", "--bar", "--baz", "xyz" }
            );

        var stdOut = FakeConsole.ReadOutputString();

        // Assert
        exitCode.Should().Be(0);
        stdOut.Should().ConsistOfLines(
            "Foo = 42",
            "Bar = True",
            "Baz = xyz"
            );
    }