Skip to content

Commit

Permalink
Introduce and use warning and error output messages (#4217)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink authored Dec 2, 2024
1 parent 70f973c commit d685c63
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task OnTestHostProcessExitedAsync(ITestHostProcessInformation testH
}

ApplicationStateGuard.Ensure(_netCoreCrashDumpGeneratorConfiguration.DumpFileNamePattern is not null);
await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(string.Format(CultureInfo.InvariantCulture, CrashDumpResources.CrashDumpProcessCrashedDumpFileCreated, testHostProcessInformation.PID)));
await _outputDisplay.DisplayAsync(this, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, CrashDumpResources.CrashDumpProcessCrashedDumpFileCreated, testHostProcessInformation.PID)));

string expectedDumpFile = _netCoreCrashDumpGeneratorConfiguration.DumpFileNamePattern.Replace("%p", testHostProcessInformation.PID.ToString(CultureInfo.InvariantCulture));
if (File.Exists(expectedDumpFile))
Expand All @@ -74,7 +74,7 @@ public async Task OnTestHostProcessExitedAsync(ITestHostProcessInformation testH
}
else
{
await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(string.Format(CultureInfo.InvariantCulture, CrashDumpResources.CannotFindExpectedCrashDumpFile, expectedDumpFile)));
await _outputDisplay.DisplayAsync(this, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, CrashDumpResources.CannotFindExpectedCrashDumpFile, expectedDumpFile)));
foreach (string dumpFile in Directory.GetFiles(Path.GetDirectoryName(expectedDumpFile)!, "*.dmp"))
{
await _messageBus.PublishAsync(this, new FileArtifact(new FileInfo(dumpFile), CrashDumpResources.CrashDumpDisplayName, CrashDumpResources.CrashDumpArtifactDescription));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private async Task TakeDumpAsync()
ApplicationStateGuard.Ensure(_dumpType is not null);

await _logger.LogInformationAsync($"Hang dump timeout({_activityTimerValue}) expired.");
await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(string.Format(CultureInfo.InvariantCulture, ExtensionResources.HangDumpTimeoutExpired, _activityTimerValue)));
await _outputDisplay.DisplayAsync(this, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, ExtensionResources.HangDumpTimeoutExpired, _activityTimerValue)));

string finalDumpFileName = _dumpFileNamePattern.Replace("%p", _testHostProcessInformation.PID.ToString(CultureInfo.InvariantCulture));
finalDumpFileName = Path.Combine(_configuration.GetTestResultDirectory(), finalDumpFileName);
Expand All @@ -339,11 +339,11 @@ private async Task TakeDumpAsync()
using (FileStream fs = File.OpenWrite(hangTestsFileName))
using (StreamWriter sw = new(fs))
{
await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(ExtensionResources.RunningTestsWhileDumping));
await _outputDisplay.DisplayAsync(this, new ErrorMessageOutputDeviceData(ExtensionResources.RunningTestsWhileDumping));
foreach ((string testName, int seconds) in tests.Tests)
{
await sw.WriteLineAsync($"[{TimeSpan.FromSeconds(seconds)}] {testName}");
await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText($"[{TimeSpan.FromSeconds(seconds)}] {testName}"));
await _outputDisplay.DisplayAsync(this, new ErrorMessageOutputDeviceData($"[{TimeSpan.FromSeconds(seconds)}] {testName}"));
}
}

Expand All @@ -352,7 +352,7 @@ private async Task TakeDumpAsync()

await _logger.LogInformationAsync($"Creating dump filename {finalDumpFileName}");

await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(string.Format(CultureInfo.InvariantCulture, ExtensionResources.CreatingDumpFile, finalDumpFileName)));
await _outputDisplay.DisplayAsync(this, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, ExtensionResources.CreatingDumpFile, finalDumpFileName)));

#if NETCOREAPP
DiagnosticsClient diagnosticsClient = new(_testHostProcessInformation.PID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public async Task OnTestSessionFinishingAsync(SessionUid sessionUid, Cancellatio
{
if (!_adapterSupportTrxCapability)
{
await _outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateYellowConsoleColorText(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TrxReportFrameworkDoesNotSupportTrxReportCapability, _testFramework.DisplayName, _testFramework.Uid)));
await _outputDisplay.DisplayAsync(this, new WarningMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TrxReportFrameworkDoesNotSupportTrxReportCapability, _testFramework.DisplayName, _testFramework.Uid)));
}

ApplicationStateGuard.Ensure(_testStartTime is not null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public void SendMessage(TestMessageLevel testMessageLevel, string message)
break;
case TestMessageLevel.Warning:
_logger.LogWarning(message);
_outputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateYellowConsoleColorText(message)).Await();
_outputDevice.DisplayAsync(this, new WarningMessageOutputDeviceData(message)).Await();
break;
case TestMessageLevel.Error:
_logger.LogError(message);
_outputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(message)).Await();
_outputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(message)).Await();
break;
default:
throw new NotSupportedException($"Unsupported logging level '{testMessageLevel}'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public async Task<ITestHost> BuildAsync(
if (!loggingState.CommandLineParseResult.HasTool && !commandLineValidationResult.IsValid)
{
await DisplayBannerIfEnabledAsync(loggingState, platformOutputDevice, testFrameworkCapabilities);
await platformOutputDevice.DisplayAsync(commandLineHandler, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(commandLineValidationResult.ErrorMessage));
await platformOutputDevice.DisplayAsync(commandLineHandler, new ErrorMessageOutputDeviceData(commandLineValidationResult.ErrorMessage));
await commandLineHandler.PrintHelpAsync(platformOutputDevice);
return new InformativeCommandLineTestHost(ExitCodes.InvalidCommandLine, serviceProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected override async Task<int> InternalRunAsync()
displayErrorMessageBuilder.AppendLine(CultureInfo.InvariantCulture, $"Provider '{extension.DisplayName}' (UID: {extension.Uid}) failed with error: {errorMessage}");
}

await platformOutputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(displayErrorMessageBuilder.ToString()));
await platformOutputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(displayErrorMessageBuilder.ToString()));
await _logger.LogErrorAsync(logErrorMessageBuilder.ToString());
return ExitCodes.InvalidPlatformSetup;
}
Expand Down Expand Up @@ -322,7 +322,7 @@ protected override async Task<int> InternalRunAsync()

if (!_testHostGracefullyClosed && !abortRun.IsCancellationRequested)
{
await platformOutputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(string.Format(CultureInfo.InvariantCulture, PlatformResources.TestProcessDidNotExitGracefullyErrorMessage, exitCode)));
await platformOutputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, PlatformResources.TestProcessDidNotExitGracefullyErrorMessage, exitCode)));
}

await _logger.LogInformationAsync($"TestHostControllersTestHost ended with exit code '{exitCode}' (real test host exit code '{testHostProcess.ExitCode}')' in '{consoleRunStarted.Elapsed}'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ public async Task<int> RunAsync()
{
if (UnknownOptions(out string? unknownOptionsError, tool))
{
await _platformOutputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(unknownOptionsError));
await _platformOutputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(unknownOptionsError));
console.WriteLine();
return ExitCodes.InvalidCommandLine;
}

if (ExtensionArgumentArityAreInvalid(out string? arityErrors, tool))
{
await _platformOutputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(arityErrors));
await _platformOutputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(arityErrors));
return ExitCodes.InvalidCommandLine;
}

ValidationResult optionsArgumentsValidationResult = await ValidateOptionsArgumentsAsync(tool);
if (!optionsArgumentsValidationResult.IsValid)
{
await _platformOutputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(optionsArgumentsValidationResult.ErrorMessage));
await _platformOutputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(optionsArgumentsValidationResult.ErrorMessage));
return ExitCodes.InvalidCommandLine;
}

return await tool.RunAsync();
}
}

await _platformOutputDevice.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText($"Tool '{toolNameToRun}' not found in the list of registered tools."));
await _platformOutputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData($"Tool '{toolNameToRun}' not found in the list of registered tools."));
await _commandLineHandler.PrintHelpAsync(_platformOutputDevice);
return ExitCodes.InvalidCommandLine;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Testing.Platform.OutputDevice;

public sealed class ErrorMessageOutputDeviceData(string message) : IOutputDeviceData
{
public string Message { get; } = message;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -367,43 +367,30 @@ public async Task DisplayAsync(IOutputDeviceDataProducer producer, IOutputDevice
{
switch (data)
{
case FormattedTextOutputDeviceData formattedTextOutputDeviceData:
if (formattedTextOutputDeviceData.ForegroundColor is SystemConsoleColor color)
{
switch (color.ConsoleColor)
{
case ConsoleColor.Red:
_terminalTestReporter.WriteErrorMessage(_assemblyName, _targetFramework, _shortArchitecture, executionId: null, formattedTextOutputDeviceData.Text, formattedTextOutputDeviceData.Padding);
break;
case ConsoleColor.Yellow:
_terminalTestReporter.WriteWarningMessage(_assemblyName, _targetFramework, _shortArchitecture, executionId: null, formattedTextOutputDeviceData.Text, formattedTextOutputDeviceData.Padding);
break;
default:
_terminalTestReporter.WriteMessage(formattedTextOutputDeviceData.Text, color, formattedTextOutputDeviceData.Padding);
break;
}
}
else
{
_terminalTestReporter.WriteMessage(formattedTextOutputDeviceData.Text, padding: formattedTextOutputDeviceData.Padding);
}
case FormattedTextOutputDeviceData formattedTextData:
await LogDebugAsync(formattedTextData.Text);
_terminalTestReporter.WriteMessage(formattedTextData.Text, formattedTextData.ForegroundColor as SystemConsoleColor, formattedTextData.Padding);
break;

case TextOutputDeviceData textData:
await LogDebugAsync(textData.Text);
_terminalTestReporter.WriteMessage(textData.Text);
break;

case TextOutputDeviceData textOutputDeviceData:
{
await LogDebugAsync(textOutputDeviceData.Text);
_terminalTestReporter.WriteMessage(textOutputDeviceData.Text);
break;
}
case WarningMessageOutputDeviceData warningData:
await LogDebugAsync(warningData.Message);
_terminalTestReporter.WriteWarningMessage(_assemblyName, _targetFramework, _shortArchitecture, executionId: null, warningData.Message, null);
break;

case ExceptionOutputDeviceData exceptionOutputDeviceData:
{
await LogDebugAsync(exceptionOutputDeviceData.Exception.ToString());
_terminalTestReporter.WriteErrorMessage(_assemblyName, _targetFramework, _shortArchitecture, executionId: null, exceptionOutputDeviceData.Exception);
case ErrorMessageOutputDeviceData errorData:
await LogDebugAsync(errorData.Message);
_terminalTestReporter.WriteErrorMessage(_assemblyName, _targetFramework, _shortArchitecture, executionId: null, errorData.Message, null);
break;

break;
}
case ExceptionOutputDeviceData exceptionOutputDeviceData:
await LogDebugAsync(exceptionOutputDeviceData.Exception.ToString());
_terminalTestReporter.WriteErrorMessage(_assemblyName, _targetFramework, _shortArchitecture, executionId: null, exceptionOutputDeviceData.Exception);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Testing.Platform.OutputDevice;

public sealed class WarningMessageOutputDeviceData(string message) : IOutputDeviceData
{
public string Message { get; } = message;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#nullable enable
Microsoft.Testing.Platform.Extensions.Messages.TestMetadataProperty.TestMetadataProperty(string! key) -> void
Microsoft.Testing.Platform.OutputDevice.ErrorMessageOutputDeviceData
Microsoft.Testing.Platform.OutputDevice.ErrorMessageOutputDeviceData.ErrorMessageOutputDeviceData(string! message) -> void
Microsoft.Testing.Platform.OutputDevice.ErrorMessageOutputDeviceData.Message.get -> string!
Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData
Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData.Message.get -> string!
Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData.WarningMessageOutputDeviceData(string! message) -> void
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty.StandardOutput.get -> string!
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty.StandardOutput.init -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private async Task HandleTestSessionResultAsync(bool isSuccess, string? warningM
if (warningMessage is not null)
{
IOutputDevice outputDisplay = ServiceProvider.GetOutputDevice();
await outputDisplay.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateYellowConsoleColorText(warningMessage));
await outputDisplay.DisplayAsync(this, new WarningMessageOutputDeviceData(warningMessage));
}

if (!isSuccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task SetTestAdapterTestSessionFailureAsync(string errorMessage)
{
TestAdapterTestSessionFailureErrorMessage = errorMessage;
_testAdapterTestSessionFailure = true;
await _outputService.DisplayAsync(this, FormattedTextOutputDeviceDataBuilder.CreateRedConsoleColorText(errorMessage));
await _outputService.DisplayAsync(this, new ErrorMessageOutputDeviceData(errorMessage));
}

public Statistics GetStatistics()
Expand Down

0 comments on commit d685c63

Please sign in to comment.