Пример #1
0
        /// <summary>Run the console in memory and get the results that would be output to the shell</summary>
        public static AppRunnerResult RunInMem(this AppRunner runner,
                                               string[] args,
                                               ILogger logger,
                                               Func <TestConsole, string> onReadLine = null,
                                               IEnumerable <string> pipedInput       = null,
                                               IPromptResponder promptResponder      = null)
        {
            TestToolsLogProvider.InitLogProvider(logger);

            var testConsole = new TestConsole(
                onReadLine,
                pipedInput,
                promptResponder == null
                    ? (Func <TestConsole, ConsoleKeyInfo>)null
                    : promptResponder.OnReadKey);

            runner.Configure(c => c.Console = testConsole);
            var outputs = InjectTestOutputs(runner);

            try
            {
                var exitCode   = runner.Run(args);
                var consoleOut = testConsole.Joined.ToString();

                logger?.WriteLine("\nconsole output:\n");
                logger?.WriteLine(consoleOut);
                return(new AppRunnerResult(exitCode, testConsole, outputs));
            }
            catch (Exception e)
            {
                logger?.WriteLine("\nconsole output:\n");
                logger?.WriteLine(testConsole.Joined.ToString());
                throw;
            }
        }
Пример #2
0
        /// <summary>Run the console in memory and get the results that would be output to the shell</summary>
        public static AppRunnerResult RunInMem(this AppRunner runner,
                                               string[] args,
                                               Action <string> logLine = null,
                                               Func <TestConsole, string> onReadLine = null,
                                               IEnumerable <string> pipedInput       = null,
                                               IPromptResponder promptResponder      = null,
                                               TestConfig config = null)
        {
            logLine = logLine ?? Console.WriteLine;
            config  = config ?? TestConfig.Default;

            IDisposable logProvider = config.PrintCommandDotNetLogs
                ? TestToolsLogProvider.InitLogProvider(logLine)
                : new DisposableAction(() => { });

            using (logProvider)
            {
                var testConsole = new TestConsole(
                    onReadLine,
                    pipedInput,
                    promptResponder == null
                        ? (Func <TestConsole, ConsoleKeyInfo>)null
                        : promptResponder.OnReadKey);
                runner.Configure(c => c.Console = testConsole);

                CommandContext context = null;
                Task <int> CaptureCommandContext(CommandContext commandContext, ExecutionDelegate next)
                {
                    context = commandContext;
                    return(next(commandContext));
                }

                runner.Configure(c => c.UseMiddleware(CaptureCommandContext, MiddlewareStages.PreTokenize));
                var captures = InjectTestCaptures(runner);

                try
                {
                    var exitCode = runner.Run(args);
                    return(new AppRunnerResult(exitCode, runner, context, testConsole, captures, config)
                           .LogResult(logLine));
                }
                catch (Exception e)
                {
                    var result = new AppRunnerResult(1, runner, context, testConsole, captures, config, e);
                    if (config.OnError.CaptureAndReturnResult)
                    {
                        testConsole.Error.WriteLine(e.Message);
                        logLine(e.Message);
                        logLine(e.StackTrace);
                        return(result.LogResult(logLine, onError: true));
                    }

                    result.LogResult(logLine, onError: true);
                    throw;
                }
            }
        }
Пример #3
0
 /// <summary>Run the console in memory and get the results that would be output to the shell</summary>
 public static AppRunnerResult RunInMem(this AppRunner runner,
                                        string args,
                                        Action <string> logLine = null,
                                        Func <TestConsole, string> onReadLine = null,
                                        IEnumerable <string> pipedInput       = null,
                                        IPromptResponder promptResponder      = null,
                                        TestConfig config = null)
 {
     return(runner.RunInMem(args.SplitArgs(), logLine, onReadLine, pipedInput, promptResponder, config));
 }
        /// <summary>Run the console in memory and get the results that would be output to the shell</summary>
        public static AppRunnerResult RunInMem(this AppRunner runner,
                                               string[] args,
                                               ILogger logger,
                                               Func <TestConsole, string> onReadLine = null,
                                               IEnumerable <string> pipedInput       = null,
                                               IPromptResponder promptResponder      = null,
                                               bool returnResultOnError = false)
        {
            using (TestToolsLogProvider.InitLogProvider(logger))
            {
                var testConsole = new TestConsole(
                    onReadLine,
                    pipedInput,
                    promptResponder == null
                        ? (Func <TestConsole, ConsoleKeyInfo>)null
                        : promptResponder.OnReadKey);

                CommandContext context = null;
                runner.CaptureState(ctx => context = ctx, MiddlewareStages.PreTokenize);
                runner.Configure(c => c.Console    = testConsole);
                var outputs = InjectTestOutputs(runner);

                void LogResult()
                {
                    logger.WriteLine("\nconsole output:\n");
                    logger.WriteLine(testConsole.Joined.ToString());
                }

                try
                {
                    var exitCode = runner.Run(args);
                    LogResult();
                    return(new AppRunnerResult(exitCode, testConsole, outputs, context));
                }
                catch (Exception e)
                {
                    if (returnResultOnError)
                    {
                        testConsole.Error.WriteLine(e.Message);
                        logger.WriteLine(e.Message);
                        logger.WriteLine(e.StackTrace);
                        LogResult();
                        return(new AppRunnerResult(1, testConsole, outputs, context));
                    }

                    LogResult();
                    throw;
                }
            }
        }