コード例 #1
0
        public static IDisposable BeginSetConsoleOut(ITestOutputHelper helper, LogStack logStack)
        {
            var current = Console.Out;

            Console.SetOut(new TextWriterBridge(helper, logStack));
            return(new Scope(current));
        }
コード例 #2
0
        public async Task SimpleTwoArgsTest()
        {
            {
                var args = "-name foo -repeat 3".Split(' ');
                var log  = new LogStack();
                await new HostBuilder()
                .ConfigureTestLogging(testOutput, log, true)
                .RunConsoleAppFrameworkAsync <SimpleTwoArgs>(args);
                log.InfoLogShouldBe(0, "name:foo");
                log.InfoLogShouldBe(1, "repeat:3");
            }
            {
                //var args = "-repeat 3".Split(' ');
                //var log = new LogStack();
                //using (TextWriterBridge.BeginSetConsoleOut(testOutput, log))
                //{
                //    {
                //        await new HostBuilder()
                //            .ConfigureTestLogging(testOutput, log, true)
                //            .RunConsoleAppFrameworkAsync<SimpleTwoArgs>(args);
                //    }

                //    log.ToStringInfo().Should().Contain("Required parameter \"name\" not found in argument");
                //}
            }
            {
                var log = new LogStack();
                using (TextWriterBridge.BeginSetConsoleOut(testOutput, log))
                {
                    var args = new string[0];
                    await new HostBuilder().RunConsoleAppFrameworkAsync <SimpleTwoArgs>(args);
                    log.ToStringInfo().Should().Contain("Options:"); // ok to show help
                }
            }
        }
コード例 #3
0
        public async Task SimpleTwoArgsTest()
        {
            {
                var args = "-name foo -repeat 3".Split(' ');
                var log  = new LogStack();
                await new HostBuilder()
                .ConfigureTestLogging(testOutput, log, true)
                .RunConsoleAppFrameworkAsync <SimpleTwoArgs>(args);
                log.InfoLogShouldBe(0, "name:foo");
                log.InfoLogShouldBe(1, "repeat:3");
            }
            {
                var args = "-repeat 3".Split(' ');
                var log  = new LogStack();
                var ex   = await Assert.ThrowsAsync <AggregateException>(async() =>
                {
                    await new HostBuilder()
                    .ConfigureTestLogging(testOutput, log, true)
                    .RunConsoleAppFrameworkAsync <SimpleTwoArgs>(args);
                });

                ex.Flatten().InnerException.Should().BeAssignableTo <TestLogException>()
                .Subject.InnerException.Message.Should().Contain("Required parameter \"name\" not found in argument");
            }
            {
                var log = new LogStack();
                using (TextWriterBridge.BeginSetConsoleOut(testOutput, log))
                {
                    var args = new string[0];
                    await new HostBuilder().RunConsoleAppFrameworkAsync <SimpleTwoArgs>(args);
                    log.ToStringInfo().Should().Contain("Options:"); // ok to show help
                }
            }
        }
コード例 #4
0
 public async Task OverrideDefaultCommandTest()
 {
     {
         var args = "list".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <OverrideDefaultCommand>(args);
         log.InfoLogShouldBe(0, "lst");
     }
     {
         var args = "help".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <OverrideDefaultCommand>(args);
         log.InfoLogShouldBe(0, "hlp");
     }
     {
         var args = "h".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <OverrideDefaultCommand>(args);
         log.InfoLogShouldBe(0, "hlp");
     }
 }
コード例 #5
0
 public async Task MultiContained()
 {
     {
         var args = "Multi1.Hello1".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync(args);
         log.InfoLogShouldBe(0, "ok");
     }
     {
         var args = "Multi1.Hello2 -input yeah".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync(args);
         log.InfoLogShouldBe(0, "yeah");
     }
     {
         var args = "Multi2.Hello1 -x 20 -y 30".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync(args);
         log.InfoLogShouldBe(0, "20:30");
     }
     {
         var args = "Multi2.Hello2 -x -y -foo yeah".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync(args);
         log.InfoLogShouldBe(0, "True:True:yeah:999");
     }
 }
コード例 #6
0
 public async Task TwoArgsWithOptionTest()
 {
     {
         var args = "-n foo -r 3".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <TwoArgsWithOption>(args);
         log.InfoLogShouldBe(0, "name:foo");
         log.InfoLogShouldBe(1, "repeat:3");
     }
     {
         var log = new LogStack();
         using (TextWriterBridge.BeginSetConsoleOut(testOutput, log))
         {
             var args = new string[0];
             await new HostBuilder().RunConsoleAppFrameworkAsync <TwoArgsWithOption>(args);
             var strAssertion = log.ToStringInfo().Should();
             strAssertion.Contain("Options:"); // ok to show help
             strAssertion.Contain("-n");
             strAssertion.Contain("name of this");
             strAssertion.Contain("-r");
             strAssertion.Contain("repeat msg");
         }
     }
 }
コード例 #7
0
        public async Task SimpleZeroArgsTest()
        {
            var log = new LogStack();

            await new HostBuilder()
            .ConfigureTestLogging(testOutput, log, true)
            .RunConsoleAppFrameworkAsync <SimpleZeroArgs>(new string[0]);
            log.InfoLogShouldBe(0, "ok");
        }
コード例 #8
0
 public async Task SimpleComplexArgsTest()
 {
     {
         var args = "-person {\"Age\":10,\"Name\":\"foo\"} -repeat 3".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <SimpleComplexArgs>(args);
         log.InfoLogShouldBe(0, "person.Age:10 person.Name:foo");
         log.InfoLogShouldBe(1, "repeat:3");
     }
 }
コード例 #9
0
        public async Task NotFoundPathTest()
        {
            var args = "run -path -pfx test.pfx -thumbnail 123456 -output output.csproj -allowoverwrite".Split(' ');
            var log  = new LogStack();

            await Assert.ThrowsAnyAsync <Exception>(async() =>
            {
                await new HostBuilder()
                .ConfigureTestLogging(testOutput, log, true)
                .RunConsoleAppFrameworkAsync <NotFoundPath>(args);
            });
        }
コード例 #10
0
        public async Task DictParse()
        {
            var args = @"-q {""Key1"":""Value1*""}".Split(' ');

            var log = new LogStack();

            await new HostBuilder()
            .ConfigureTestLogging(testOutput, log, true)
            .RunConsoleAppFrameworkAsync <DictionaryCheck>(args);

            log.InfoLogShouldBe(0, "Key1:Value1*");
        }
コード例 #11
0
 public async Task AllDefaultParametersTest()
 {
     {
         var args = new string[0];
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <AllDefaultParameters>(args);
         log.InfoLogShouldBe(0, "name:aaa");
         log.InfoLogShouldBe(1, "repeat:100");
         log.InfoLogShouldBe(2, "hoo:");
     }
 }
コード例 #12
0
 public async Task TwoArgsWithDefaultTest()
 {
     {
         var args = "-name foo".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <TwoArgsWithDefault>(args);
         log.InfoLogShouldBe(0, "name:foo");
         log.InfoLogShouldBe(1, "repeat:100");
         log.InfoLogShouldBe(2, "hoo:");
     }
 }
コード例 #13
0
 public async Task TwoSubCommandTest()
 {
     {
         var args = "-d 12345.12345".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <TwoSubCommand>(args);
         log.InfoLogShouldBe(0, "d:12345.12345");
     }
     {
         var args = "run -path foo -pfx bar".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <TwoSubCommand>(args);
         log.InfoLogShouldBe(0, "path:foo");
         log.InfoLogShouldBe(1, "pfx:bar");
     }
     {
         var args = "sum 10 20".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <TwoSubCommand>(args);
         log.InfoLogShouldBe(0, "x:10");
         log.InfoLogShouldBe(1, "y:20");
     }
     {
         var args = "opt foobarbaz -x 10 -y 20".Split(' ');
         var log  = new LogStack();
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <TwoSubCommand>(args);
         log.InfoLogShouldBe(0, "input:foobarbaz");
         log.InfoLogShouldBe(1, "x:10");
         log.InfoLogShouldBe(2, "y:20");
     }
 }
コード例 #14
0
 public async Task BooleanSwitchTest()
 {
     {
         var log  = new LogStack();
         var args = "-x foo -foo -yeah".Split(' ');
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <BooleanSwitch>(args);
         log.InfoLogShouldBe(0, "x:foo");
         log.InfoLogShouldBe(1, "foo:True");
         log.InfoLogShouldBe(2, "yeah:True");
     }
     {
         var log  = new LogStack();
         var args = "-x foo -foo".Split(' ');
         await new HostBuilder()
         .ConfigureTestLogging(testOutput, log, true)
         .RunConsoleAppFrameworkAsync <BooleanSwitch>(args);
         log.InfoLogShouldBe(0, "x:foo");
         log.InfoLogShouldBe(1, "foo:True");
         log.InfoLogShouldBe(2, "yeah:False");
     }
 }
コード例 #15
0
 public async Task AliasCommandTest()
 {
     {
         var collection = new[] {
             "r -path foo -pfx bar".Split(' '),
             "run -path foo -pfx bar".Split(' '),
         };
         foreach (var args in collection)
         {
             var log = new LogStack();
             await new HostBuilder()
             .ConfigureTestLogging(testOutput, log, true)
             .RunConsoleAppFrameworkAsync <AliasCommand>(args);
             log.InfoLogShouldBe(0, "path:foo");
             log.InfoLogShouldBe(1, "pfx:bar");
         }
     }
     {
         {
             var args = "su 10 20".Split(' ');
             var log  = new LogStack();
             await new HostBuilder()
             .ConfigureTestLogging(testOutput, log, true)
             .RunConsoleAppFrameworkAsync <AliasCommand>(args);
             log.InfoLogShouldBe(0, "30");
         }
         {
             var args = "summmm 99 100".Split(' ');
             var log  = new LogStack();
             await new HostBuilder()
             .ConfigureTestLogging(testOutput, log, true)
             .RunConsoleAppFrameworkAsync <AliasCommand>(args);
             log.InfoLogShouldBe(0, "199");
         }
     }
 }
コード例 #16
0
 public XUnitLoggerProvider(ITestOutputHelper testOutput, LogStack logStack, bool throwExceptionOnError)
 {
     logger = new XUnitLogger(testOutput, logStack, throwExceptionOnError);
 }
コード例 #17
0
 public static IHostBuilder ConfigureTestLogging(this IHostBuilder builder, ITestOutputHelper testOutputHelper, LogStack logStack, bool throwExceptionOnError)
 {
     return(builder
            .ConfigureServices(x => x.AddSingleton <LogStack>(logStack))
            .ConfigureLogging(x => x.SetMinimumLevel(LogLevel.Trace).AddProvider(new XUnitLoggerProvider(testOutputHelper, logStack, throwExceptionOnError))));
 }
コード例 #18
0
 public XUnitLogger(ITestOutputHelper testOutput, LogStack logStack, bool throwExceptionOnError)
 {
     this.testOutput            = testOutput;
     this.logStack              = logStack;
     this.throwExceptionOnError = throwExceptionOnError;
 }
コード例 #19
0
 public TextWriterBridge(ITestOutputHelper helper, LogStack logStack)
 {
     this.helper   = helper;
     this.logStack = logStack;
 }