Skip to content

Commit

Permalink
Add compact summary mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed May 19, 2023
1 parent da3cb8a commit 7ed6f2e
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 147 deletions.
2 changes: 2 additions & 0 deletions GitHubActionsTestLogger.Tests/InitializationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void I_can_use_the_logger_with_a_custom_configuration()
{
["annotations.titleFormat"] = "TitleFormat",
["annotations.messageFormat"] = "MessageFormat",
["summary.compactLayout"] = "true",
["summary.includePassedTests"] = "true",
["summary.includeSkippedTests"] = "true"
};
Expand All @@ -45,6 +46,7 @@ public void I_can_use_the_logger_with_a_custom_configuration()
logger.Context.Should().NotBeNull();
logger.Context?.Options.AnnotationTitleFormat.Should().Be("TitleFormat");
logger.Context?.Options.AnnotationMessageFormat.Should().Be("MessageFormat");
logger.Context?.Options.SummaryCompactLayout.Should().BeTrue();
logger.Context?.Options.SummaryIncludePassedTests.Should().BeTrue();
logger.Context?.Options.SummaryIncludeSkippedTests.Should().BeTrue();
}
Expand Down
2 changes: 1 addition & 1 deletion GitHubActionsTestLogger/GitHubWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void CreateSummary(string content)
{
// Other steps may have reported summaries that contain HTML tags,
// which can screw up markdown parsing, so we need to make sure
// there's at least two newlines before our summary, to be safe.
// there's at least two newlines before our summary to be safe.
// https://github.com/Tyrrrz/GitHubActionsTestLogger/issues/22
_summaryWriter.WriteLine();
_summaryWriter.WriteLine();
Expand Down
45 changes: 45 additions & 0 deletions GitHubActionsTestLogger/Templates/MarkdownRazorTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using RazorBlade;

namespace GitHubActionsTestLogger.Templates;

internal abstract class MarkdownRazorTemplate<T> : PlainTextTemplate<T>
{
protected MarkdownRazorTemplate(T model)
: base(model)
{
}

// In order to produce HTML that's also valid Markdown, we need to
// remove some whitespace inside literals.
public new void WriteLiteral(string? literal)
{
if (!string.IsNullOrEmpty(literal))
{
base.WriteLiteral(
literal
// Remove indentation
.Replace(" ", "")
// Remove linebreaks
.Replace("\r", "").Replace("\n", "")
);
}
else
{
base.WriteLiteral(literal);
}
}

// Using params here to write multiple lines as a workaround
// for the fact that Razor does not support raw string literals.
protected void WriteMarkdown(params string?[] lines)
{
// Two line breaks are required to separate markdown from HTML
base.WriteLiteral("\n\n");

foreach (var line in lines)
{
base.WriteLiteral(line);
base.WriteLiteral("\n");
}
}
}
12 changes: 12 additions & 0 deletions GitHubActionsTestLogger/Templates/TestSummaryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace GitHubActionsTestLogger.Templates;

internal class TestSummaryContext
{
public required TestLoggerOptions Options { get; init; }

public required string TestSuite { get; init; }

public required string TargetFramework { get; init; }

public required TestRunResult TestRunResult { get; init; }
}
11 changes: 11 additions & 0 deletions GitHubActionsTestLogger/Templates/TestSummaryDetailsTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GitHubActionsTestLogger.Templates;

// Workaround for:
// https://github.com/ltrzesniewski/RazorBlade/issues/10
internal partial class TestSummaryDetailsTemplate
{
public TestSummaryDetailsTemplate(TestSummaryContext context)
: base(context)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,19 @@
@using System
@using System.Collections.Generic
@using System.Linq
@using GitHubActionsTestLogger.Utils.Extensions
@using Microsoft.VisualStudio.TestPlatform.ObjectModel

@inherits RazorBlade.PlainTextTemplate

@functions
{
public required string TestSuite { get; init; }
public required string TargetFramework { get; init; }
public required TestRunStatistics TestRunStatistics { get; init; }
public required IReadOnlyList<TestResult> TestResults { get; init; }
}

@functions
{
// In order to generate HTML that's also valid Markdown, we need to
// remove some whitespace inside literals.
public new void WriteLiteral(string? literal)
{
if (!string.IsNullOrEmpty(literal))
{
base.WriteLiteral(
literal
// Remove indentation
.Replace(" ", "")
// Remove linebreaks
.Replace("\r", "").Replace("\n", "")
);
}
else
{
base.WriteLiteral(literal);
}
}

// Using params here to write multiple lines as a workaround
// for the fact that Razor does not support raw string literals.
private void WriteMarkdown(params string?[] lines)
{
// Two line breaks are required to separate markdown from HTML
base.WriteLiteral("\n\n");

foreach (var line in lines)
{
base.WriteLiteral(line);
base.WriteLiteral("\n");
}
}
}
@inherits MarkdownRazorTemplate<TestSummaryContext>

@{
static string FormatTestOutcome(TestOutcome outcome, bool isCircle) => outcome switch
string FormatTestOutcome(TestOutcome outcome) => outcome switch
{
TestOutcome.Passed => isCircle ? "🟢" : "🟩",
TestOutcome.Failed => isCircle ? "🔴" : "🟥",
_ => isCircle ? "🟡" : "🟨"
TestOutcome.Passed => "🟩",
TestOutcome.Failed => "🟥",
_ => "🟨"
};
}

<h2>
@FormatTestOutcome(TestRunStatistics.OverallOutcome, true) @TestSuite <sub><sup>(@TargetFramework)</sup></sub>
</h2>

<table>
<th width="99999">✓&nbsp;&nbsp;Passed</th>
<th width="99999">✘&nbsp;&nbsp;Failed</th>
Expand All @@ -72,31 +22,40 @@
<th width="99999">⧗&nbsp;&nbsp;Elapsed</th>
<tr>
<td align="center">
@(TestRunStatistics.PassedTestCount > 0
? TestRunStatistics.PassedTestCount.ToString()
@(Model.TestRunResult.PassedTestCount > 0
? Model.TestRunResult.PassedTestCount.ToString()
: "")
</td>
<td align="center">
@(TestRunStatistics.FailedTestCount > 0
? TestRunStatistics.FailedTestCount.ToString()
@(Model.TestRunResult.FailedTestCount > 0
? Model.TestRunResult.FailedTestCount.ToString()
: "")
</td>
<td align="center">
@(TestRunStatistics.SkippedTestCount > 0
? TestRunStatistics.SkippedTestCount.ToString()
@(Model.TestRunResult.SkippedTestCount > 0
? Model.TestRunResult.SkippedTestCount.ToString()
: "")
</td>
<td align="center">
@TestRunStatistics.TotalTestCount
@Model.TestRunResult.TotalTestCount
</td>
<td align="center">
@TestRunStatistics.ElapsedDuration.ToHumanString()
@Model.TestRunResult.OverallDuration.ToHumanString()
</td>
</tr>
</table>

@{
var testResultGroups = TestResults
var testResults = Model
.TestRunResult
.TestResults
.Where(r =>
r.Outcome == TestOutcome.Failed ||
r.Outcome == TestOutcome.Passed && Model.Options.SummaryIncludePassedTests ||
r.Outcome == TestOutcome.Skipped && Model.Options.SummaryIncludeSkippedTests
);

var testResultGroups = testResults
.GroupBy(r => r.TestCase.GetTypeFullyQualifiedName(), StringComparer.Ordinal)
.Select(g => new
{
Expand All @@ -117,7 +76,7 @@
{
var failedTestCount = testResultGroup.TestResults.Count(r => r.Outcome == TestOutcome.Failed);

<details>
<details @(Model.Options.SummaryCompactLayout ? "open" : null)>
<summary>
<b>@testResultGroup.TypeName</b>

Expand All @@ -127,7 +86,7 @@
}
</summary>

@* This adds some margin which is smaller than <br> *@
@* This adds a margin that is smaller than <br> *@
<p></p>

<ul>
Expand All @@ -148,7 +107,7 @@
var url = filePath?.Pipe(p => GitHubWorkflow.TryGenerateFilePermalink(p, fileLine));

<li>
@FormatTestOutcome(testResult.Outcome, false)
@FormatTestOutcome(testResult.Outcome)

@if (!string.IsNullOrWhiteSpace(url))
{
Expand All @@ -172,6 +131,4 @@
}
</ul>
</details>
}

<hr />
}
11 changes: 11 additions & 0 deletions GitHubActionsTestLogger/Templates/TestSummaryTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GitHubActionsTestLogger.Templates;

// Workaround for:
// https://github.com/ltrzesniewski/RazorBlade/issues/10
internal partial class TestSummaryTemplate
{
public TestSummaryTemplate(TestSummaryContext context)
: base(context)
{
}
}
37 changes: 37 additions & 0 deletions GitHubActionsTestLogger/Templates/TestSummaryTemplate.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@using Microsoft.VisualStudio.TestPlatform.ObjectModel

@inherits MarkdownRazorTemplate<TestSummaryContext>

@{
string FormatTestOutcome(TestOutcome outcome) => outcome switch
{
TestOutcome.Passed => "🟢",
TestOutcome.Failed => "🔴",
_ => "🟡"
};
}

@if (Model.Options.SummaryCompactLayout)
{
// Have to yield raw HTML
<details>
<summary>
<b>@FormatTestOutcome(Model.TestRunResult.OverallOutcome) @Model.TestSuite</b> (@Model.TargetFramework)
</summary>

@* This adds a margin that is smaller than <br> *@
<p></p>

@(new TestSummaryDetailsTemplate(Model))
</details>
}
else
{
<h2>
@FormatTestOutcome(Model.TestRunResult.OverallOutcome) @Model.TestSuite <sub><sup>(@Model.TargetFramework)</sup></sub>
</h2>

@(new TestSummaryDetailsTemplate(Model))

<hr />
}
48 changes: 11 additions & 37 deletions GitHubActionsTestLogger/TestLoggerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Text;
using GitHubActionsTestLogger.Templates;
using GitHubActionsTestLogger.Utils.Extensions;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
Expand Down Expand Up @@ -89,49 +90,22 @@ public void HandleTestRunComplete(TestRunCompleteEventArgs args)
{
lock (_lock)
{
var testSuite =
_testRunCriteria?.Sources?.FirstOrDefault()?.Pipe(Path.GetFileNameWithoutExtension) ??
"Unknown Test Suite";

var targetFramework =
_testRunCriteria?.TryGetTargetFramework() ??
"Unknown Target Framework";

var testRunStatistics = new TestRunStatistics
var context = new TestSummaryContext
{
PassedTestCount =
args.TestRunStatistics?[TestOutcome.Passed] ??
_testResults.Count(r => r.Outcome == TestOutcome.Passed),
Options = Options,

FailedTestCount =
args.TestRunStatistics?[TestOutcome.Failed] ??
_testResults.Count(r => r.Outcome == TestOutcome.Failed),
TestSuite =
_testRunCriteria?.Sources?.FirstOrDefault()?.Pipe(Path.GetFileNameWithoutExtension) ??
"Unknown Test Suite",

SkippedTestCount =
args.TestRunStatistics?[TestOutcome.Skipped] ??
_testResults.Count(r => r.Outcome == TestOutcome.Skipped),
TargetFramework =
_testRunCriteria?.TryGetTargetFramework() ??
"Unknown Target Framework",

TotalTestCount = args.TestRunStatistics?.ExecutedTests ?? _testResults.Count,
ElapsedDuration = args.ElapsedTimeInRunningTests
TestRunResult = new TestRunResult(_testResults, args.ElapsedTimeInRunningTests)
};

var testResults = _testResults
.Where(r =>
r.Outcome == TestOutcome.Failed ||
r.Outcome == TestOutcome.Passed && Options.SummaryIncludePassedTests ||
r.Outcome == TestOutcome.Skipped && Options.SummaryIncludeSkippedTests
)
.ToArray();

_github.CreateSummary(
new TestSummaryTemplate
{
TestSuite = testSuite,
TargetFramework = targetFramework,
TestRunStatistics = testRunStatistics,
TestResults = testResults
}.Render()
);
_github.CreateSummary(new TestSummaryTemplate(context).Render());
}
}
}
Loading

0 comments on commit 7ed6f2e

Please sign in to comment.