diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/ExceptionFlattener.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/ExceptionFlattener.cs index 929a2d6ed41..14975d7380b 100644 --- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/ExceptionFlattener.cs +++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/ExceptionFlattener.cs @@ -5,45 +5,42 @@ namespace Microsoft.Testing.Platform.OutputDevice.Terminal; internal sealed class ExceptionFlattener { - internal static FlatException[] Flatten(string? errorMessage, Exception? exception) + internal static (string? CustomErrorMessage, Exception?[] Exceptions) Flatten(string? errorMessage, Exception? exception) { if (errorMessage is null && exception is null) { - return []; + return (null, []); } - string? message = !RoslynString.IsNullOrWhiteSpace(errorMessage) ? errorMessage : exception?.Message; - string? type = exception?.GetType().FullName; - string? stackTrace = exception?.StackTrace; - var flatException = new FlatException(message, type, stackTrace); + if (exception is null) + { + // Only have an error message, no exception + return (errorMessage, []); + } - List flatExceptions = [flatException]; + List exceptions = [exception]; // Add all inner exceptions. This will flatten top level AggregateExceptions, // and all AggregateExceptions that are directly in AggregateExceptions, but won't expand // AggregateExceptions that are in non-aggregate exception inner exceptions. - IEnumerable aggregateExceptions = exception switch + IEnumerable innerExceptionsToProcess = exception switch { AggregateException aggregate => aggregate.Flatten().InnerExceptions, - _ => [exception?.InnerException], + _ => [exception.InnerException], }; - foreach (Exception? aggregate in aggregateExceptions) + foreach (Exception? innerException in innerExceptionsToProcess) { - Exception? currentException = aggregate; + Exception? currentException = innerException; while (currentException is not null) { - flatExceptions.Add(new FlatException( - aggregate?.Message, - aggregate?.GetType().FullName, - aggregate?.StackTrace)); - + exceptions.Add(currentException); currentException = currentException.InnerException; } } - return [.. flatExceptions]; + // Use custom error message if provided and not whitespace, otherwise null + string? customMessage = !RoslynString.IsNullOrWhiteSpace(errorMessage) ? errorMessage : null; + return (customMessage, [.. exceptions]); } } - -internal sealed record FlatException(string? ErrorMessage, string? ErrorType, string? StackTrace); diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cs index f128d74c42f..6b08083f6fa 100644 --- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cs +++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporter.cs @@ -314,14 +314,15 @@ internal void TestCompleted( string? standardOutput, string? errorOutput) { - FlatException[] flatExceptions = ExceptionFlattener.Flatten(errorMessage, exception); + (string? customErrorMessage, Exception?[] exceptions) = ExceptionFlattener.Flatten(errorMessage, exception); TestCompleted( testNodeUid, displayName, outcome, duration, informativeMessage, - flatExceptions, + customErrorMessage, + exceptions, expected, actual, standardOutput, @@ -334,7 +335,8 @@ private void TestCompleted( TestOutcome outcome, TimeSpan duration, string? informativeMessage, - FlatException[] exceptions, + string? customErrorMessage, + Exception?[] exceptions, string? expected, string? actual, string? standardOutput, @@ -380,6 +382,7 @@ private void TestCompleted( outcome, duration, informativeMessage, + customErrorMessage, exceptions, expected, actual, @@ -400,7 +403,8 @@ private void RenderTestCompleted( TestOutcome outcome, TimeSpan duration, string? informativeMessage, - FlatException[] flatExceptions, + string? customErrorMessage, + Exception?[] exceptions, string? expected, string? actual, string? standardOutput, @@ -439,14 +443,14 @@ private void RenderTestCompleted( terminal.AppendLine(); AppendIndentedLine(terminal, informativeMessage, SingleIndentation); - FormatErrorMessage(terminal, flatExceptions, outcome, 0); + FormatErrorMessage(terminal, customErrorMessage, exceptions, outcome, 0); FormatExpectedAndActual(terminal, expected, actual); - FormatStackTrace(terminal, flatExceptions, 0); - FormatInnerExceptions(terminal, flatExceptions); + FormatStackTrace(terminal, exceptions, 0); + FormatInnerExceptions(terminal, exceptions); FormatStandardAndErrorOutput(terminal, standardOutput, errorOutput); } - private static void FormatInnerExceptions(ITerminal terminal, FlatException[] exceptions) + private static void FormatInnerExceptions(ITerminal terminal, Exception?[] exceptions) { if (exceptions.Length == 0) { @@ -458,43 +462,44 @@ private static void FormatInnerExceptions(ITerminal terminal, FlatException[] ex terminal.SetColor(TerminalColor.DarkRed); terminal.Append(SingleIndentation); terminal.Append("--->"); - FormatErrorMessage(terminal, exceptions, TestOutcome.Error, i); + FormatErrorMessage(terminal, customErrorMessage: null, exceptions, TestOutcome.Error, i); FormatStackTrace(terminal, exceptions, i); } } - private static void FormatErrorMessage(ITerminal terminal, FlatException[] exceptions, TestOutcome outcome, int index) + private static void FormatErrorMessage(ITerminal terminal, string? customErrorMessage, Exception?[] exceptions, TestOutcome outcome, int index) { - string? firstErrorMessage = GetStringFromIndexOrDefault(exceptions, e => e.ErrorMessage, index); - string? firstErrorType = GetStringFromIndexOrDefault(exceptions, e => e.ErrorType, index); - string? firstStackTrace = GetStringFromIndexOrDefault(exceptions, e => e.StackTrace, index); - if (RoslynString.IsNullOrWhiteSpace(firstErrorMessage) && RoslynString.IsNullOrWhiteSpace(firstErrorType) && RoslynString.IsNullOrWhiteSpace(firstStackTrace)) + Exception? exception = index < exceptions.Length ? exceptions[index] : null; + // For the first exception (index 0), use custom error message if provided, otherwise use exception message + string? errorMessage = index == 0 && customErrorMessage is not null + ? customErrorMessage + : exception?.Message; + string? errorType = exception?.GetType().FullName; + string? stackTrace = exception?.StackTrace; + if (RoslynString.IsNullOrWhiteSpace(errorMessage) && RoslynString.IsNullOrWhiteSpace(errorType) && RoslynString.IsNullOrWhiteSpace(stackTrace)) { return; } terminal.SetColor(TerminalColor.DarkRed); - if (firstStackTrace is null) + if (stackTrace is null) { - AppendIndentedLine(terminal, firstErrorMessage, SingleIndentation); + AppendIndentedLine(terminal, errorMessage, SingleIndentation); } else if (outcome == TestOutcome.Fail) { // For failed tests, we don't prefix the message with the exception type because it is most likely an assertion specific exception like AssertionFailedException, and we prefer to show that without the exception type to avoid additional noise. - AppendIndentedLine(terminal, firstErrorMessage, SingleIndentation); + AppendIndentedLine(terminal, errorMessage, SingleIndentation); } else { - AppendIndentedLine(terminal, $"{firstErrorType}: {firstErrorMessage}", SingleIndentation); + AppendIndentedLine(terminal, $"{errorType}: {errorMessage}", SingleIndentation); } terminal.ResetColor(); } - private static string? GetStringFromIndexOrDefault(FlatException[] exceptions, Func property, int index) => - exceptions.Length >= index + 1 ? property(exceptions[index]) : null; - private static void FormatExpectedAndActual(ITerminal terminal, string? expected, string? actual) { if (RoslynString.IsNullOrWhiteSpace(expected) && RoslynString.IsNullOrWhiteSpace(actual)) @@ -512,9 +517,10 @@ private static void FormatExpectedAndActual(ITerminal terminal, string? expected terminal.ResetColor(); } - private static void FormatStackTrace(ITerminal terminal, FlatException[] exceptions, int index) + private static void FormatStackTrace(ITerminal terminal, Exception?[] exceptions, int index) { - string? stackTrace = GetStringFromIndexOrDefault(exceptions, e => e.StackTrace, index); + Exception? exception = index < exceptions.Length ? exceptions[index] : null; + string? stackTrace = exception?.StackTrace; if (RoslynString.IsNullOrWhiteSpace(stackTrace)) { return; diff --git a/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/DotnetTestDataConsumer.cs b/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/DotnetTestDataConsumer.cs index 48cbb723f08..f94f35eee68 100644 --- a/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/DotnetTestDataConsumer.cs +++ b/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/DotnetTestDataConsumer.cs @@ -262,12 +262,23 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella return null; } - FlatException[] exceptions = ExceptionFlattener.Flatten(errorMessage, exception); + (string? customErrorMessage, Exception?[] exceptions) = ExceptionFlattener.Flatten(errorMessage, exception); + + // If we only have a custom error message with no exceptions, create a single ExceptionMessage + if (exceptions.Length == 0) + { + return [new ExceptionMessage(customErrorMessage, null, null)]; + } var exceptionMessages = new ExceptionMessage[exceptions.Length]; for (int i = 0; i < exceptions.Length; i++) { - exceptionMessages[i] = new ExceptionMessage(exceptions[i].ErrorMessage, exceptions[i].ErrorType, exceptions[i].StackTrace); + Exception? ex = exceptions[i]; + // For the first exception, use custom error message if provided, otherwise use exception message + string? message = i == 0 && customErrorMessage is not null + ? customErrorMessage + : ex?.Message; + exceptionMessages[i] = new ExceptionMessage(message, ex?.GetType().FullName, ex?.StackTrace); } return exceptionMessages;