Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed May 29, 2022
1 parent d652661 commit c451406
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
31 changes: 31 additions & 0 deletions GitHubActionsTestLogger.Tests/AnnotationFormatSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ public void Custom_format_can_contain_newlines()

// Assert
var output = commandWriter.ToString().Trim();

output.Should().Contain("foo%0Abar");
}

[Fact]
public void Default_format_references_test_name_and_error_message()
{
// Arrange
using var commandWriter = new StringWriter();

var context = new TestLoggerContext(
new GitHubWorkflow(
commandWriter,
TextWriter.Null
),
TestLoggerOptions.Default
);

// Act
context.SimulateTestRun(
new TestResultBuilder()
.SetDisplayName("Test1")
.SetOutcome(TestOutcome.Failed)
.SetErrorMessage("ErrorMessage")
.Build()
);

// Assert
var output = commandWriter.ToString().Trim();

output.Should().Contain("Test1");
output.Should().Contain("ErrorMessage");
}
}
2 changes: 2 additions & 0 deletions GitHubActionsTestLogger.Tests/AnnotationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void Passed_tests_do_not_get_reported()

// Assert
var output = commandWriter.ToString().Trim();

output.Should().BeEmpty();
}

Expand Down Expand Up @@ -60,6 +61,7 @@ public void Skipped_tests_do_not_get_reported()

// Assert
var output = commandWriter.ToString().Trim();

output.Should().BeEmpty();
}

Expand Down
1 change: 1 addition & 0 deletions GitHubActionsTestLogger.Tests/SummarySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void Test_summary_contains_test_suite_name()

// Assert
var output = summaryWriter.ToString().Trim();

output.Should().Contain("TestProject");
}

Expand Down
8 changes: 5 additions & 3 deletions GitHubActionsTestLogger/TestLoggerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ private string ApplyAnnotationFormat(string format, TestResult testResult)
buffer.Replace("\\n", "\n");

// Name token
buffer.Replace("$test", testResult.TestCase.DisplayName);
buffer.Replace("$test", testResult.TestCase.DisplayName ?? "");

// Traits tokens
foreach (var trait in testResult.Traits.Union(testResult.TestCase.Traits))
buffer.Replace($"$traits.{trait.Name}", trait.Value);

// Error message
buffer.Replace("$error", testResult.ErrorMessage);
buffer.Replace("$error", testResult.ErrorMessage ?? "");

// Error trace
buffer.Replace("$trace", testResult.ErrorStackTrace);
buffer.Replace("$trace", testResult.ErrorStackTrace ?? "");

// Target framework
buffer.Replace("$framework", _testRunCriteria?.TryGetTargetFramework() ?? "");
Expand Down Expand Up @@ -91,6 +91,8 @@ public void HandleTestRunComplete(TestRunCompleteEventArgs args)
{
// TestRunStart event sometimes doesn't fire, which means _testRunCriteria may be null
// https://github.com/microsoft/vstest/issues/3121
// Note: it might be an issue only on this repo, when using coverlet with the logger
// https://twitter.com/Tyrrrz/status/1530141770788610048

var testSuiteName =
_testRunCriteria?.Sources.FirstOrDefault()?.Pipe(Path.GetFileNameWithoutExtension) ??
Expand Down
40 changes: 21 additions & 19 deletions GitHubActionsTestLogger/TestSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public static string Generate(
string targetFrameworkName,
IReadOnlyList<TestResult> testResults)
{
var passedCount = testResults.Count(t => t.Outcome == TestOutcome.Passed);
var failedCount = testResults.Count(t => t.Outcome == TestOutcome.Failed);
var skippedCount = testResults.Count(t => t.Outcome == TestOutcome.Skipped);
var totalCount = testResults.Count;
var totalDuration = TimeSpan.FromMilliseconds(testResults.Sum(t => t.Duration.TotalMilliseconds));
var passedTestCount = testResults.Count(t => t.Outcome == TestOutcome.Passed);
var failedTestCount = testResults.Count(t => t.Outcome == TestOutcome.Failed);
var skippedTestCount = testResults.Count(t => t.Outcome == TestOutcome.Skipped);
var totalTestCount = testResults.Count;
var totalTestDuration = TimeSpan.FromMilliseconds(testResults.Sum(t => t.Duration.TotalMilliseconds));

var buffer = new StringBuilder();

// Header
// Spoiler header
buffer
.Append("<details>")
.Append("<summary>")
.Append(failedCount <= 0 ? "🟢" : "🔴")
.Append(failedTestCount <= 0 ? "🟢" : "🔴")
.Append(" ")
.Append("<b>")
.Append(testSuiteName)
Expand All @@ -38,7 +38,7 @@ public static string Generate(
.Append("</summary>")
.Append("<br/>");

// Overview
// Overview table
buffer
// Table header
.Append("<table>")
Expand Down Expand Up @@ -76,40 +76,40 @@ public static string Generate(
.Append("<tr>")
.Append("<td align=\"center\">")
.Append(
passedCount > 0
? passedCount.ToString()
passedTestCount > 0
? passedTestCount.ToString()
: ""
)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(
failedCount > 0
? failedCount.ToString()
failedTestCount > 0
? failedTestCount.ToString()
: ""
)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(
skippedCount > 0
? skippedCount.ToString()
skippedTestCount > 0
? skippedTestCount.ToString()
: ""
)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(totalCount)
.Append(totalTestCount)
.Append("</td>")
.Append("<td align=\"center\">")
.Append(totalDuration.ToHumanString())
.Append(totalTestDuration.ToHumanString())
.Append("</td>")
.Append("</tr>")
.Append("</table>")
.AppendLine()
.AppendLine();

// Failed tests
// List of failed tests
foreach (var testResult in testResults.Where(r => r.Outcome == TestOutcome.Failed))
{
// Generate permalink
// Generate permalink for the test
var filePath = testResult.TryGetSourceFilePath();
var fileLine = testResult.TryGetSourceLine();
var url = !string.IsNullOrWhiteSpace(filePath)
Expand All @@ -127,12 +127,14 @@ public static string Generate(
.Append(url ?? "#")
.Append(")")
.AppendLine()
.AppendLine("```")
// YAML syntax highlighting works really well for exception messages and stack traces
.AppendLine("```yml")
.AppendLine(testResult.ErrorMessage)
.AppendLine(testResult.ErrorStackTrace)
.AppendLine("```");
}

// Spoiler closing tags
buffer
.Append("</details>")
.AppendLine()
Expand Down

0 comments on commit c451406

Please sign in to comment.