public void TestWriteWithDebugging() { var mockInput = new Mock <IInput>(); var mockOutput = new Mock <IOutput>(); mockOutput.Setup((o) => o.Options) .Returns(OutputOptions.VerbosityNormal); var ioConsole = new IOConsole(mockInput.Object, mockOutput.Object); var stopwatch = new Stopwatch(); ioConsole.SetDebugging(stopwatch); stopwatch.Start(); ioConsole.Write("hello world", true); mockOutput.Verify((o) => o.Write(It.IsRegex(@"^\[(.*)/(.*)\] hello world$"), true, OutputOptions.VerbosityNormal)); }
/// <inheritdoc /> protected override int DoRun(IInput input, IOutput output) { disablePluginsByDefault = input.HasRawOption("--no-plugins"); var ioConsole = new IOConsole(input, output); io = ioConsole; var commandName = GetCommandName(input); TryGetCommand(commandName, out BaseCommand command); LoadPluginCommands(command); var isProxyCommand = false; if (!string.IsNullOrEmpty(commandName) || command != null) { // Maybe the command is provided by the plugin. If we can't find // us again, we will stop intercepting the exception. if (command == null) { command = Find(commandName); } isProxyCommand = command is Command.BaseCommand baseCommand && baseCommand.IsProxyCommand; } if (!isProxyCommand) { io.WriteError( $"Running {Bucket.GetVersionPretty()} ({Bucket.GetVersion()},{Bucket.GetReleaseDataPretty()}) on {Platform.GetOSInfo()}", verbosity: Verbosities.Debug); if (!(command is CommandSelfUpdate) && Bucket.IsDev && (DateTime.Now - Bucket.GetReleaseData()) > new TimeSpan(60, 0, 0, 0, 0)) { io.WriteError( "<warning>Warning: This development build of bucket is over 60 days old. It is recommended to update it by running \"self-update\" to get the latest version.</warning>"); } } Stopwatch stopWatch = null; try { if (input.HasRawOption("--profile")) { stopWatch = new Stopwatch(); ioConsole.SetDebugging(stopWatch); } stopWatch?.Start(); var exitCode = base.DoRun(input, output); stopWatch?.Stop(); if (stopWatch != null) { var memoryUsage = AbstractHelper.FormatMemory(Environment.WorkingSet); var timeSpent = stopWatch.Elapsed; io.WriteError(string.Empty); io.WriteError($"<info>Memory usage: {memoryUsage}, total time: {timeSpent.TotalSeconds.ToString("0.00")}s</info>"); io.WriteError(string.Empty); } return(exitCode); } catch (ScriptExecutionException ex) { return(ex.ExitCode); } }