示例#1
0
        public async Task RunAsync_Cancellation_Test()
        {
            // Arrange
            using var console = new VirtualConsole();

            var application = new CliApplicationBuilder()
                              .AddCommand(typeof(CancellableCommand))
                              .UseConsole(console)
                              .Build();

            var commandLineArguments = new[] { "cancel" };
            var environmentVariables = new Dictionary <string, string>();

            // Act
            console.CancelAfter(TimeSpan.FromSeconds(0.2));

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

            var stdOut = console.ReadOutputString().Trim();
            var stdErr = console.ReadErrorString().Trim();

            // Assert
            exitCode.Should().NotBe(0);
            stdOut.Should().BeNullOrWhiteSpace();
            stdErr.Should().NotBeNullOrWhiteSpace();

            Console.WriteLine(stdErr);
        }
示例#2
0
        public async Task RunAsync_Negative_Test(
            IReadOnlyList <Type> commandTypes,
            IReadOnlyList <string> commandLineArguments,
            IReadOnlyDictionary <string, string> environmentVariables,
            string?expectedStdErr = null,
            int?expectedExitCode  = null)
        {
            // Arrange
            using var console = new VirtualConsole();

            var application = new CliApplicationBuilder()
                              .AddCommands(commandTypes)
                              .UseTitle(TestAppName)
                              .UseExecutableName(TestAppName)
                              .UseVersionText(TestVersionText)
                              .UseConsole(console)
                              .Build();

            // Act
            var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);

            var stdErr = console.ReadErrorString().Trim();

            // Assert
            exitCode.Should().NotBe(0);
            stdErr.Should().NotBeNullOrWhiteSpace();

            if (expectedExitCode != null)
            {
                exitCode.Should().Be(expectedExitCode);
            }

            if (expectedStdErr != null)
            {
                stdErr.Should().Be(expectedStdErr);
            }

            Console.WriteLine(stdErr);
        }