Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlatException> flatExceptions = [flatException];
List<Exception?> 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<Exception?> aggregateExceptions = exception switch
IEnumerable<Exception?> 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);
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -380,6 +382,7 @@ private void TestCompleted(
outcome,
duration,
informativeMessage,
customErrorMessage,
exceptions,
expected,
actual,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
{
Expand All @@ -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<FlatException, string?> 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))
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down