コード例 #1
0
        public async Task Application_without_interactive_mode_cannot_execute_interactive_only_commands()
        {
            // Arrange
            var(console, stdOut, stdErr) = VirtualConsole.CreateBuffered();

            // Act
            var app = new CliApplicationBuilder().AddCommand <BenchmarkDefaultCommand>()
                      .AddCommand <NamedInteractiveOnlyCommand>()
                      .UseConsole(console)
                      .Build();

            // Assert
            app.Should().NotBeNull();

            // Act
            int exitCode = await app.RunAsync(new string[] { "named-interactive-only" }, new Dictionary <string, string>());

            // Asert
            exitCode.Should().Be(ExitCodes.Error);
            stdOut.GetString().Should().BeNullOrWhiteSpace();
            stdErr.GetString().Should().NotBeNullOrWhiteSpace();
            stdErr.GetString().Should().Contain("can be executed only in interactive mode, but this application is using CliApplication.");

            _output.WriteLine(stdOut.GetString());
            _output.WriteLine(stdErr.GetString());
        }
コード例 #2
0
        public void Application_in_direct_mode_can_be_created_with_a_custom_configuration_and_middlewares()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AddDirective <DebugDirective>()
                      .AddDirective <PreviewDirective>()
                      .AddDirective(typeof(CustomDirective))
                      .UseMiddleware <ExecutionTimingMiddleware>()
                      .UseMiddleware(typeof(ExitCodeMiddleware))
                      .UseOptionFallbackProvider(typeof(EnvironmentVariableFallbackProvider))
                      .UseTitle("test")
                      .UseExecutableName("test")
                      .UseVersionText("test")
                      .UseDescription("test")
                      .UseConsole(new VirtualConsole(Stream.Null))
                      .UseStartupMessage((metadata) => $"Startup message {metadata.Title} {metadata.VersionText} {metadata.ExecutableName} {metadata.Description}")
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #3
0
        public void Application_can_be_created_with_configuration_action()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .Configure(cfg =>
            {
                cfg.AddCommand <DefaultCommand>()
                .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                .AddCommands(new[] { typeof(DefaultCommand) })
                .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                .AddCommandsFromThisAssembly()
                .UseExceptionHandler <DefaultExceptionHandler>()
                .AddDirective <DebugDirective>()
                .AddDirective <PreviewDirective>()
                .AddDirective <CustomInteractiveModeOnlyDirective>()
                .AddDirective <CustomDirective>();

                cfg.UseStartupMessage((metadata) => $"Startup message {metadata.Title} {metadata.VersionText} {metadata.ExecutableName} {metadata.Description}");
            })
                      .Configure(cfg =>
            {
                cfg.UseTitle("test")
                .UseExecutableName("test")
                .UseVersionText("test")
                .UseDescription("test")
                .UseConsole <SystemConsole>();
            })
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #4
0
        public void Application_can_be_created_with_VirtualConsole_CreateBuffered()
        {
            // Arrange
            var(console, _, _) = VirtualConsole.CreateBuffered(isInputRedirected: false);

            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .UseExceptionHandler(typeof(DefaultExceptionHandler))
                      .AddDirective <DebugDirective>()
                      .AddDirective <PreviewDirective>()
                      .AddDirective <CustomInteractiveModeOnlyDirective>()
                      .AddDirective <CustomDirective>()
                      .UseTitle("test")
                      .UseExecutableName("test")
                      .UseVersionText("test")
                      .UseDescription("test")
                      .UseConsole(console)
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #5
0
        public void Application_in_interactive_mode_can_be_created_with_a_custom_configuration()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AddDirectivesFrom(typeof(DefaultCommand).Assembly)
                      .UseTitle("test")
                      .UseExecutableName("test")
                      .UseVersionText("test")
                      .UseDescription("test")
                      .UseDirectMode(true)
                      .UseInteractiveMode()
                      .RegisterMode <ValidCustomMode>()
                      .RegisterMode(typeof(ValidCustomMode))
                      .UseInteractiveMode()
                      .UseStartupMessage((metadata, console) => { console.Output.WriteLine($"Startup message {metadata.Title} {metadata.VersionText} {metadata.ExecutableName} {metadata.Description})"); })
                      .UseConsole(new VirtualConsole(Stream.Null))
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #6
0
ファイル: MiddlewareTests.cs プロジェクト: hollow87/Typin
        public async Task Middleware_pipeline_should_be_executed()
        {
            // Arrange
            var(console, stdOut, stdErr) = VirtualConsole.CreateBuffered();

            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommand <PipelineCommand>()
                      .UseConsole(console)
                      .UseMiddleware <ExecutionTimingMiddleware>()
                      .UseMiddleware(typeof(ExitCodeMiddleware))
                      .Build();

            // Assert
            app.Should().NotBeNull();

            // Act
            int exitCode = await app.RunAsync(new string[] { }, new Dictionary <string, string>());

            // Asert
            exitCode.Should().Be(0);
            stdOut.GetString().Should().NotBeNullOrWhiteSpace();
            stdErr.GetString().Should().BeNullOrWhiteSpace();

            stdOut.GetString().Should().ContainAll(
                ExecutionTimingMiddleware.ExpectedOutput0,
                ExecutionTimingMiddleware.ExpectedOutput1,
                ExitCodeMiddleware.ExpectedOutput,
                DefaultCommand.ExpectedOutputText);

            _output.WriteLine(stdOut.GetString());
        }
コード例 #7
0
ファイル: MiddlewareTests.cs プロジェクト: hollow87/Typin
        public async Task Middleware_types_collection_should_contain_all_user_defined_middlewares()
        {
            // Arrange
            var(console, stdOut, stdErr) = VirtualConsole.CreateBuffered();

            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommand <PipelineCommand>()
                      .UseConsole(console)
                      .UseMiddleware <ExecutionTimingMiddleware>()
                      .UseMiddleware(typeof(ExitCodeMiddleware))
                      .Build();

            // Assert
            app.Should().NotBeNull();

            // Act
            int exitCode = await app.RunAsync(new string[] { "pipeline" }, new Dictionary <string, string>());

            // Asert
            exitCode.Should().Be(0);
            stdOut.GetString().Should().NotBeNullOrWhiteSpace();
            stdErr.GetString().Should().BeNullOrWhiteSpace();

            stdOut.GetString().Should().ContainAll(
                typeof(ExecutionTimingMiddleware).AssemblyQualifiedName,
                typeof(ExitCodeMiddleware).AssemblyQualifiedName,
                PipelineCommand.PipelineTermination,
                "Typin.Internal.Pipeline");

            _output.WriteLine(stdOut.GetString());
        }
コード例 #8
0
        public void Application_in_interactive_mode_with_params_can_be_created_with_a_custom_configuration()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AddDirective <DebugDirective>()
                      .AddDirective <PreviewDirective>()
                      .AddDirectivesFromThisAssembly()
                      .UseTitle("test")
                      .UseExecutableName("test")
                      .UseVersionText("test")
                      .UseDescription("test")
                      .UseInteractiveMode(false, false)
                      .UsePromptForeground(ConsoleColor.Magenta)
                      .UseStartupMessage("Startup message {{title}} {title} {version} {executable} {description}")
                      .UseConsole <SystemConsole>()
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #9
0
ファイル: ApplicationSpecs.cs プロジェクト: mrs2020/CliFx
        public void Application_can_be_created_with_a_default_configuration()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .AddCommandsFromThisAssembly()
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #10
0
        public void Application_can_be_created_with_startup_class()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .UseStartup <CustomStartupClass>()
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #11
0
        public async Task Should_singleton_services_be_resolved()
        {
            // Arrange
            var(console, stdOut, _) = VirtualConsole.CreateBuffered();

            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommand <WithDependenciesCommand>()
                      .UseConsole(console)
                      .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                      .ConfigureContainer <ContainerBuilder>(builder =>
            {
                // Use and configure Autofac
                builder.RegisterType <DependencyA>().SingleInstance();
                builder.RegisterType <DependencyB>().SingleInstance();
                builder.RegisterType <DependencyC>().SingleInstance();
            })
                      .ConfigureServices(services =>
            {
                //services.AddSingleton<DependencyA>();
                //services.AddSingleton<DependencyB>();
                //services.AddSingleton<DependencyC>();
            })
                      .Build();

            // Assert
            app.Should().NotBeNull();

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

            // Assert
            exitCode.Should().Be(ExitCodes.Success);
            stdOut.GetString().Should().NotBeNullOrWhiteSpace();

            string[] output = stdOut.GetString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
            output.Should().HaveCount(3);

            char[] chars = output[0].Split('|')
                           .Select(x => char.Parse(x))
                           .ToArray();

            Guid[] guids = output[1].Split('|')
                           .Select(x => Guid.Parse(x))
                           .ToArray();

            Guid depGuid = Guid.Parse(output[2]);

            chars.Should().HaveCount(3);
            chars.Should().Equal(new char[] { 'A', 'B', 'B' });

            guids.Should().HaveCount(3);
            depGuid.Should().Be(guids[1]);
        }
コード例 #12
0
        public async Task Application_can_be_created_and_executed_with_benchmark_commands()
        {
            // Arrange
            var(console, stdOut, stdErr) = VirtualConsole.CreateBuffered();

            // Act
            var app = new CliApplicationBuilder().AddCommand <BenchmarkDefaultCommand>()
                      .UseConsole(console)
                      .Build();

            // Assert
            app.Should().NotBeNull();

            // Act
            int exitCode = await app.RunAsync(new string[] { "--str", "hello world", "-i", "-13", "-b" }, new Dictionary <string, string>());

            // Assert
            exitCode.Should().Be(0);
            stdOut.GetString().Should().ContainEquivalentOf("{\"StrOption\":\"hello world\",\"IntOption\":-13,\"BoolOption\":true}");
            stdErr.GetString().Should().BeNullOrWhiteSpace();
        }
コード例 #13
0
ファイル: ApplicationSpecs.cs プロジェクト: mrs2020/CliFx
        public void Application_can_be_created_with_a_custom_configuration()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand(typeof(ValidCommand))
                      .AddCommandsFrom(typeof(ValidCommand).Assembly)
                      .AddCommands(new[] { typeof(ValidCommand) })
                      .AddCommandsFrom(new[] { typeof(ValidCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AllowDebugMode()
                      .AllowPreviewMode()
                      .UseTitle("test")
                      .UseExecutableName("test")
                      .UseVersionText("test")
                      .UseDescription("test")
                      .UseConsole(new VirtualConsole(Stream.Null))
                      .UseTypeActivator(Activator.CreateInstance)
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #14
0
        public void Application_can_be_created_with_VirtualConsole_MemoryStreamWriter()
        {
            // Arrange
            var(console, _, _) = VirtualConsole.CreateBuffered(isInputRedirected: false, isOutputRedirected: true);

            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AddDirectivesFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddDirective <DebugDirective>()
                      .AddDirective <PreviewDirective>()
                      .AddDirective <CustomInteractiveModeOnlyDirective>()
                      .AddDirective <CustomDirective>()
                      .UseConsole(console)
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #15
0
        public void Application_in_direct_mode_can_be_created_with_a_custom_configuration()
        {
            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AddDirective <DebugDirective>()
                      .AddDirective <PreviewDirective>()
                      .AddDirective <CustomDirective>()
                      .UseOptionFallbackProvider <EnvironmentVariableFallbackProvider>()
                      .UseTitle("test")
                      .UseExecutableName("test")
                      .UseVersionText("test")
                      .UseDescription("test")
                      .UseConsole(new VirtualConsole(Stream.Null))
                      .UseStartupMessage($"Startup message")
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }
コード例 #16
0
        public void Application_can_be_created_with_VirtualConsole_MemoryStreamWriter()
        {
            // Arrange
            IConsole console = new VirtualConsole(output: new MemoryStreamWriter(), isOutputRedirected: false,
                                                  error: new MemoryStreamWriter(Encoding.UTF8), isErrorRedirected: true);

            // Act
            var app = new CliApplicationBuilder()
                      .AddCommand <DefaultCommand>()
                      .AddCommandsFrom(typeof(DefaultCommand).Assembly)
                      .AddCommands(new[] { typeof(DefaultCommand) })
                      .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddCommandsFromThisAssembly()
                      .AddDirectivesFrom(new[] { typeof(DefaultCommand).Assembly })
                      .AddDirective <DebugDirective>()
                      .AddDirective <PreviewDirective>()
                      .AddDirective <CustomInteractiveModeOnlyDirective>()
                      .AddDirective <CustomDirective>()
                      .UseConsole(console)
                      .Build();

            // Assert
            app.Should().NotBeNull();
        }