/// <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;
                }
            }
        }
        /// <summary>
        /// Convenience wrapper for <see cref="CaptureState"/> to capture state from the <see cref="CommandContext"/>
        /// </summary>
        public static T GetFromContext <T>(this AppRunner runner,
                                           string[] args,
                                           ILogger logger,
                                           Func <CommandContext, T> capture,
                                           MiddlewareStages middlewareStage = MiddlewareStages.PostBindValuesPreInvoke,
                                           int?orderWithinStage             = null)
        {
            T state = default;

            runner.CaptureState(
                ctx => state = capture(ctx),
                exitAfterCapture: true,
                middlewareStage: middlewareStage,
                orderWithinStage: orderWithinStage)
            .RunInMem(args, logger);
            return(state);
        }
Пример #3
0
        private Operands GetOperands(string methodName, IDependencyResolver dependencyResolver = null)
        {
            Operands operands  = null;
            var      appRunner = new AppRunner <App>();

            if (dependencyResolver != null)
            {
                appRunner.UseDependencyResolver(dependencyResolver);
            }
            appRunner
            .CaptureState(
                ctx => operands = Operands.FromCommand(ctx.ParseResult.TargetCommand),
                MiddlewareStages.PostParseInputPreBindValues,
                exitAfterCapture: true)
            .RunInMem($"{methodName}", _testOutputHelper);
            return(operands);
        }
Пример #4
0
        /// <summary>
        /// Convenience wrapper for <see cref="CaptureState"/> to capture state from the <see cref="CommandContext"/>
        /// </summary>
        public static T?GetFromContext <T>(this AppRunner runner,
                                           string[] args,
                                           Func <CommandContext, T> capture,
                                           Action <string?>?logLine         = null,
                                           MiddlewareStages middlewareStage = MiddlewareStages.PostBindValuesPreInvoke,
                                           short?orderWithinStage           = null)
            where T : class
        {
            T?state = default;

            runner.CaptureState(
                ctx => state = capture(ctx),
                exitAfterCapture: true,
                middlewareStage: middlewareStage,
                orderWithinStage: orderWithinStage)
            .RunInMem(args, logLine, config: TestConfig.Silent);
            return(state);
        }