/// <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; } }
/// <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; } } }
/// <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>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 <ITestConsole, string>?onReadLine = null, IEnumerable <string>?pipedInput = null, IPromptResponder?promptResponder = null, TestConfig?config = null) { logLine ??= Console.WriteLine; config ??= TestConfig.Default; IDisposable appInfo = config.AppInfoOverride is null ? new DisposableAction(() => { }) : AppInfo.SetResolver(() => config.AppInfoOverride); IDisposable logProvider = config.PrintCommandDotNetLogs ? TestToolsLogProvider.InitLogProvider(logLine) : new DisposableAction(() => { }); using (appInfo) using (logProvider) { ITestConsole testConsole = null !; runner.Configure(c => { c.Console = testConsole = c.Console as ITestConsole ?? c.Services.GetOrDefault <ITestConsole>() ?? new TestConsole(!config.SkipTrimEndOfConsoleOutputs); }); if (onReadLine != null) { testConsole.Mock(onReadLine); } if (pipedInput != null) { testConsole.Mock(pipedInput); } if (promptResponder != null) { testConsole.Mock(promptResponder.OnReadKey); } CommandContext?context = null; Task <int> CaptureCommandContext(CommandContext commandContext, ExecutionDelegate next) { context = commandContext; return(next(commandContext)); } runner.Configure(c => c.UseMiddleware(CaptureCommandContext, MiddlewareSteps.DebugDirective - 100)); try { var exitCode = runner.Run(args); return(new AppRunnerResult(exitCode, runner, context !, testConsole, config) .LogResult(logLine) .VerifyAfterRunAssertions()); } catch (Exception e) { var result = new AppRunnerResult(1, runner, context !, testConsole, config, e); if (config.OnError.CaptureAndReturnResult) { testConsole.Error.WriteLine(e.Message); logLine(e.Message); logLine(e.StackTrace); } result .LogResult(logLine, onError: true) .VerifyAfterRunAssertions(); throw; } } }