-
Notifications
You must be signed in to change notification settings - Fork 854
[CI] Generate simple test summary #9223
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
edfca6e
[CI] Add a tools/GenerateTestSummary.csproj for use with github actions
radical ef704aa
Address feedback from @ davidfowler and add link to the logs
radical 827200d
Update tools/GenerateTestSummary/TestSummaryGenerator.cs
radical 524dd08
cleanup
radical File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="System.CommandLine" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text; | ||
| using System.CommandLine; | ||
| using Aspire.TestTools; | ||
|
|
||
| // Usage: dotnet tools run GenerateTestSummary --dirPathOrTrxFilePath <path> [--output <output>] [--combined] | ||
| // Generate a summary report from trx files. | ||
| // And write to $GITHUB_STEP_SUMMARY if running in GitHub Actions. | ||
|
|
||
| var dirPathOrTrxFilePathArgument = new Argument<string>("dirPathOrTrxFilePath"); | ||
| var outputOption = new Option<string>("--output", "-o") { Description = "Output file path" }; | ||
| var combinedSummaryOption = new Option<bool>("--combined", "-c") { Description = "Generate combined summary report" }; | ||
| var urlOption = new Option<string>("--url", "-u") { Description = "URL for test links" }; | ||
|
|
||
| var rootCommand = new RootCommand | ||
| { | ||
| dirPathOrTrxFilePathArgument, | ||
| outputOption, | ||
| combinedSummaryOption, | ||
| urlOption | ||
| }; | ||
|
|
||
| rootCommand.SetAction(result => | ||
| { | ||
| var dirPathOrTrxFilePath = result.GetValue<string>(dirPathOrTrxFilePathArgument); | ||
| if (string.IsNullOrEmpty(dirPathOrTrxFilePath)) | ||
| { | ||
| Console.WriteLine("Please provide a directory path with trx files or a trx file path."); | ||
| return; | ||
| } | ||
|
|
||
| var combinedSummary = result.GetValue<bool>(combinedSummaryOption); | ||
|
|
||
| string report; | ||
| if (combinedSummary) | ||
| { | ||
| report = TestSummaryGenerator.CreateCombinedTestSummaryReport(dirPathOrTrxFilePath); | ||
| } | ||
| else | ||
| { | ||
| var reportBuilder = new StringBuilder(); | ||
| if (Directory.Exists(dirPathOrTrxFilePath)) | ||
| { | ||
| var trxFiles = Directory.EnumerateFiles(dirPathOrTrxFilePath, "*.trx", SearchOption.AllDirectories); | ||
| foreach (var trxFile in trxFiles) | ||
| { | ||
| TestSummaryGenerator.CreateSingleTestSummaryReport(trxFile, reportBuilder); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| TestSummaryGenerator.CreateSingleTestSummaryReport(dirPathOrTrxFilePath, reportBuilder, result.GetValue<string>(urlOption)); | ||
| } | ||
|
|
||
| report = reportBuilder.ToString(); | ||
| } | ||
|
|
||
| var outputFilePath = result.GetValue<string>(outputOption); | ||
| if (outputFilePath is not null) | ||
| { | ||
| File.WriteAllText(outputFilePath, report); | ||
| Console.WriteLine($"Report written to {outputFilePath}"); | ||
| } | ||
|
|
||
| if (report.Length > 0 | ||
| && Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true" | ||
| && Environment.GetEnvironmentVariable("GITHUB_STEP_SUMMARY") is string summaryPath | ||
| && !string.IsNullOrEmpty(summaryPath)) | ||
| { | ||
| File.WriteAllText(summaryPath, report); | ||
| } | ||
| }); | ||
|
|
||
| return rootCommand.Parse(args).Invoke(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text; | ||
| using System.Globalization; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Aspire.TestTools; | ||
|
|
||
| sealed partial class TestSummaryGenerator | ||
| { | ||
| public static string CreateCombinedTestSummaryReport(string basePath) | ||
| { | ||
| if (!Directory.Exists(basePath)) | ||
| { | ||
| throw new DirectoryNotFoundException($"The directory '{basePath}' does not exist."); | ||
| } | ||
|
|
||
| var trxFiles = System.IO.Directory.EnumerateFiles(basePath, "*.trx", System.IO.SearchOption.AllDirectories); | ||
|
|
||
| int overallTotalTestCount = 0; | ||
| int overallPassedTestCount = 0; | ||
| int overallFailedTestCount = 0; | ||
| int overallSkippedTestCount = 0; | ||
|
|
||
| // Update to use markdown tables instead of HTML | ||
| var tableBuilder = new StringBuilder(); | ||
| tableBuilder.AppendLine("| Name | Passed | Failed | Skipped | Total |"); | ||
| tableBuilder.AppendLine("|------|--------|--------|---------|-------|"); | ||
|
|
||
| foreach (var file in trxFiles.OrderBy(f => Path.GetFileName(f))) | ||
| { | ||
| TestRun? testRun; | ||
| try | ||
| { | ||
| testRun = TrxReader.DeserializeTrxFile(file); | ||
| if (testRun == null || testRun.ResultSummary?.Counters == null) | ||
| { | ||
| Console.WriteLine($"Failed to deserialize or find results in file: {file}, tr: {testRun}"); | ||
| continue; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"Failed to deserialize file: {file}, exception: {ex}"); | ||
| continue; | ||
| } | ||
|
|
||
| // emit row for each trx file | ||
| var counters = testRun.ResultSummary.Counters; | ||
| int total = counters.Total; | ||
| int passed = counters.Passed; | ||
| int failed = counters.Failed; | ||
| int skipped = counters.NotExecuted; | ||
|
|
||
| overallTotalTestCount += total; | ||
| overallPassedTestCount += passed; | ||
| overallFailedTestCount += failed; | ||
| overallSkippedTestCount += skipped; | ||
|
|
||
| tableBuilder.AppendLine(CultureInfo.InvariantCulture, $"| {(failed > 0 ? "❌" : "✅")} {GetTestTitle(file)} | {passed} | {failed} | {skipped} | {total} |"); | ||
| } | ||
|
|
||
| var overallTableBuilder = new StringBuilder(); | ||
| overallTableBuilder.AppendLine("## Overall Summary"); | ||
|
|
||
| overallTableBuilder.AppendLine("| Passed | Failed | Skipped | Total |"); | ||
| overallTableBuilder.AppendLine("|--------|--------|---------|-------|"); | ||
| overallTableBuilder.AppendLine(CultureInfo.InvariantCulture, $"| {overallPassedTestCount} | {overallFailedTestCount} | {overallSkippedTestCount} | {overallTotalTestCount} |"); | ||
|
|
||
| overallTableBuilder.AppendLine(); | ||
| overallTableBuilder.Append(tableBuilder); | ||
|
|
||
| return overallTableBuilder.ToString(); | ||
| } | ||
|
|
||
| public static void CreateSingleTestSummaryReport(string trxFilePath, StringBuilder reportBuilder, string? url = null) | ||
| { | ||
| if (!File.Exists(trxFilePath)) | ||
| { | ||
| throw new FileNotFoundException($"The file '{trxFilePath}' does not exist."); | ||
| } | ||
|
|
||
| TestRun? testRun; | ||
| try | ||
| { | ||
| testRun = TrxReader.DeserializeTrxFile(trxFilePath); | ||
| if (testRun == null || testRun.ResultSummary?.Counters == null) | ||
| { | ||
| throw new InvalidOperationException($"Failed to deserialize or find results in file: {trxFilePath}"); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| throw new InvalidOperationException($"Failed to process file: {trxFilePath}", ex); | ||
| } | ||
|
|
||
| var counters = testRun.ResultSummary.Counters; | ||
| var failed = counters.Failed; | ||
| if (failed == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var total = counters.Total; | ||
| var passed = counters.Passed; | ||
| var skipped = counters.NotExecuted; | ||
|
|
||
| reportBuilder.AppendLine(CultureInfo.InvariantCulture, $"### {GetTestTitle(trxFilePath)}"); | ||
| reportBuilder.AppendLine("| Passed | Failed | Skipped | Total |"); | ||
| reportBuilder.AppendLine("|--------|--------|---------|-------|"); | ||
| reportBuilder.AppendLine(CultureInfo.InvariantCulture, $"| {passed} | {failed} | {skipped} | {total} |"); | ||
|
|
||
| reportBuilder.AppendLine(); | ||
| if (testRun.Results?.UnitTestResults is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var failedTests = testRun.Results.UnitTestResults.Where(r => r.Outcome == "Failed"); | ||
| if (failedTests.Any()) | ||
| { | ||
| foreach (var test in failedTests) | ||
| { | ||
| var title = string.IsNullOrEmpty(url) | ||
| ? $"🔴 <b>{test.TestName}</b>" | ||
| : $"🔴 <a href=\"{url}\">{test.TestName}</a>"; | ||
|
|
||
| reportBuilder.AppendLine("<div>"); | ||
| reportBuilder.AppendLine(CultureInfo.InvariantCulture, $""" | ||
| <details><summary>{title}</summary> | ||
| """); | ||
|
|
||
| var errorMsgBuilder = new StringBuilder(); | ||
| errorMsgBuilder.AppendLine(test.Output?.ErrorInfo?.InnerText ?? string.Empty); | ||
| errorMsgBuilder.AppendLine(test.Output?.StdOut ?? string.Empty); | ||
|
|
||
| // Truncate long error messages for readability | ||
radical marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var errorMsgTruncated = TruncateTheStart(errorMsgBuilder.ToString(), 50_000); | ||
|
|
||
| reportBuilder.AppendLine(); | ||
| reportBuilder.AppendLine("```yml"); | ||
| reportBuilder.AppendLine(errorMsgTruncated); | ||
| reportBuilder.AppendLine("```"); | ||
| reportBuilder.AppendLine(); | ||
| reportBuilder.AppendLine("</div>"); | ||
| } | ||
| } | ||
| reportBuilder.AppendLine(); | ||
| } | ||
|
|
||
| public static string GetTestTitle(string trxFileName) | ||
| { | ||
| var filename = Path.GetFileNameWithoutExtension(trxFileName); | ||
| var match = TestNameFromTrxFileNameRegex().Match(filename); | ||
| if (match.Success) | ||
| { | ||
| return $"{match.Groups["testName"].Value} ({match.Groups["tfm"].Value})"; | ||
| } | ||
|
|
||
| return filename; | ||
| } | ||
|
|
||
| [GeneratedRegex(@"(?<testName>.*)_(?<tfm>net\d+\.0)_.*")] | ||
| private static partial Regex TestNameFromTrxFileNameRegex(); | ||
|
|
||
| private static string? TruncateTheStart(string? s, int maxLength) | ||
| => s is null || s.Length <= maxLength | ||
| ? s | ||
| : "... (truncated) " + s[^maxLength..]; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm struggling to understand all use-cases for this argument; more comments would be really helpful.
I understand when this points to a folder where *.trx files are, but what does it mean when it is a file? Why would I pass a file?
What does the following combination means?