Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log messages for Test Results #75084

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -240,4 +240,16 @@
<data name="Project_0_has_unresolved_dependencies" xml:space="preserve">
<value>Project {0} has unresolved dependencies</value>
</data>
<data name="Standard_Output_Messages" xml:space="preserve">
<value>Standard Output Messages</value>
</data>
<data name="Standard_Error_Messages" xml:space="preserve">
<value>Standard Error Messages</value>
</data>
<data name="Debug_Trace_Messages" xml:space="preserve">
<value>Debug Trace Messages</value>
</data>
<data name="Additional_Info_Messages" xml:space="preserve">
<value>Additional Info Messages</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.LanguageServer.Handler.Testing;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
Expand Down Expand Up @@ -138,31 +141,83 @@ private static string CreateTestCaseReportMessage(TestRunChangedEventArgs? testR
{
var messageBuilder = new StringBuilder();
messageBuilder.Append($"[{result.Outcome}] {result.TestCase.DisplayName}");
if (result.ErrorMessage != null || result.ErrorStackTrace != null)
{
messageBuilder.AppendLine();
}

if (!string.IsNullOrWhiteSpace(result.ErrorMessage))
{
messageBuilder.AppendLine();
messageBuilder.AppendLine(IndentString($"{LanguageServerResources.Message}:", 4));
messageBuilder.AppendLine(IndentString(result.ErrorMessage, 8));
}

if (!string.IsNullOrWhiteSpace(result.ErrorStackTrace))
{
messageBuilder.AppendLine();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will append an empty line between the msg and stack trace, which IIRC is why I appended only once above. Maybe not the biggest issue but I believe that was why it was written that way

messageBuilder.AppendLine(value: IndentString($"{LanguageServerResources.Stack_Trace}:", 4));
messageBuilder.AppendLine(IndentString(result.ErrorStackTrace, 8));
}

var standardOutputMessages = GetTestMessages(result.Messages, TestResultMessage.StandardOutCategory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have a screenshot of what it looks like?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the description with some output. Let me see if I can get output and error messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. So just one new line at the top and we squish everything together.

Starting test run
[Failed] Microsoft.CodeAnalysis.Tools.Tests.Analyzers.ThirdPartyAnalyzerFormatterTests.TestLoadingAllAnalyzers_LoadsDependenciesFromAllSearchPaths
    Message:
        System.Exception : This is an error message

    Stack Trace:
           at Microsoft.CodeAnalysis.Tools.Tests.Analyzers.ThirdPartyAnalyzerFormatterTests.TestLoadingAllAnalyzers_LoadsDependenciesFromAllSearchPaths() in c:\Users\jorobich\source\format\tests\Analyzers\ThirdPartyAnalyzerFormatterTests.cs:line 218
        --- End of stack trace from previous location ---

    Standard Output Messages:
          Determining projects to restore...
          All projects are up-to-date for restore.

==== Summary ====
Failed!  - Failed:    1, Passed:    0, Skipped:    0, Total:    1, Duration: 9s

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

if (standardOutputMessages.Length > 0)
{
messageBuilder.AppendLine();
messageBuilder.AppendLine(value: IndentString($"{LanguageServerResources.Standard_Output_Messages}:", 4));
messageBuilder.AppendLine(FormatMessages(standardOutputMessages, 8));
}

var standardErrorMessages = GetTestMessages(result.Messages, TestResultMessage.StandardErrorCategory);
if (standardErrorMessages.Length > 0)
{
messageBuilder.AppendLine();
messageBuilder.AppendLine(value: IndentString($"{LanguageServerResources.Standard_Error_Messages}:", 4));
messageBuilder.AppendLine(FormatMessages(standardErrorMessages, 8));
}

var debugTraceMessages = GetTestMessages(result.Messages, TestResultMessage.DebugTraceCategory);
if (debugTraceMessages.Length > 0)
{
messageBuilder.AppendLine();
messageBuilder.AppendLine(value: IndentString($"{LanguageServerResources.Debug_Trace_Messages}:", 4));
messageBuilder.AppendLine(FormatMessages(debugTraceMessages, 8));
}

var additionalInfoMessages = GetTestMessages(result.Messages, TestResultMessage.AdditionalInfoCategory);
if (additionalInfoMessages.Length > 0)
{
messageBuilder.AppendLine();
messageBuilder.AppendLine(value: IndentString($"{LanguageServerResources.Additional_Info_Messages}:", 4));
messageBuilder.AppendLine(FormatMessages(additionalInfoMessages, 8));
}

return messageBuilder.ToString();
});

return string.Join(Environment.NewLine, results);

static string IndentString(string text, int count)
{
return text.Replace(Environment.NewLine, $"{Environment.NewLine} ").TrimEnd().Insert(0, new string(' ', count));
var indentation = new string(' ', count);
return text.Replace(Environment.NewLine, $"{Environment.NewLine}{indentation}").TrimEnd().Insert(0, indentation);
}

static ImmutableArray<TestResultMessage> GetTestMessages(Collection<TestResultMessage> messages, string requiredCategory)
{
return messages.WhereAsArray(static (msg, category) => msg.Category.Equals(category, StringComparison.OrdinalIgnoreCase), requiredCategory);
}

static string FormatMessages(ImmutableArray<TestResultMessage> messages, int indentation)
{
var builder = new StringBuilder();
foreach (var message in messages)
{
if (message.Text is null)
continue;

var indentedMessage = IndentString(message.Text, indentation);
if (!string.IsNullOrWhiteSpace(indentedMessage))
builder.Append(indentedMessage);
}

return builder.ToString();
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading