Skip to content

Commit

Permalink
Refactor with C# 12 features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Dec 10, 2023
1 parent 8749b17 commit 7a9a9bd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 75 deletions.
24 changes: 10 additions & 14 deletions GitHubActionsTestLogger.Tests/AnnotationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

namespace GitHubActionsTestLogger.Tests;

public class AnnotationSpecs
public class AnnotationSpecs(ITestOutputHelper testOutput)
{
private readonly ITestOutputHelper _testOutput;

public AnnotationSpecs(ITestOutputHelper testOutput) => _testOutput = testOutput;

[Fact]
public void I_can_use_the_logger_to_produce_annotations_for_failed_tests()
{
Expand Down Expand Up @@ -46,7 +42,7 @@ public void I_can_use_the_logger_to_produce_annotations_for_failed_tests()
output.Should().NotContain("Test2");
output.Should().NotContain("Test3");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -96,7 +92,7 @@ at CliWrap.Tests.CancellationSpecs.I_can_execute_a_command_with_buffering_and_ca
output.Should().Contain("I can execute a command with buffering and cancel it immediately");
output.Should().Contain("ErrorMessage");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -150,7 +146,7 @@ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotifi
output.Should().Contain("SendEnvelopeAsync_ItemRateLimit_DropsItem");
output.Should().Contain("ErrorMessage");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -179,7 +175,7 @@ public void I_can_use_the_logger_to_produce_annotations_that_include_the_test_na
output.Should().Contain("<Test1>");
output.Should().Contain("[Test1]");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -213,7 +209,7 @@ public void I_can_use_the_logger_to_produce_annotations_that_include_test_traits
output.Should().Contain("<UI Test -> Test1>");
output.Should().Contain("[UI Test -> Test1]");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -246,7 +242,7 @@ public void I_can_use_the_logger_to_produce_annotations_that_include_the_error_m
output.Should().Contain("<Test1: ErrorMessage>");
output.Should().Contain("[Test1: ErrorMessage]");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -279,7 +275,7 @@ public void I_can_use_the_logger_to_produce_annotations_that_include_the_error_s
output.Should().Contain("<Test1: ErrorStackTrace>");
output.Should().Contain("[Test1: ErrorStackTrace]");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -314,7 +310,7 @@ public void I_can_use_the_logger_to_produce_annotations_that_include_the_target_
output.Should().Contain("<Test1 (FakeTargetFramework)>");
output.Should().Contain("[Test1 (FakeTargetFramework)]");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand All @@ -338,6 +334,6 @@ public void I_can_use_the_logger_to_produce_annotations_that_include_line_breaks

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

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}
}
14 changes: 5 additions & 9 deletions GitHubActionsTestLogger.Tests/SummarySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

namespace GitHubActionsTestLogger.Tests;

public class SummarySpecs
public class SummarySpecs(ITestOutputHelper testOutput)
{
private readonly ITestOutputHelper _testOutput;

public SummarySpecs(ITestOutputHelper testOutput) => _testOutput = testOutput;

[Fact]
public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_test_suite_name()
{
Expand All @@ -33,7 +29,7 @@ public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_test_sui

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

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -80,7 +76,7 @@ public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_list_of_
output.Should().Contain("Test3");
output.Should().Contain("Test4");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -127,7 +123,7 @@ public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_list_of_
output.Should().Contain("Test3");
output.Should().Contain("Test4");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}

[Fact]
Expand Down Expand Up @@ -186,6 +182,6 @@ public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_list_of_
output.Should().NotContain("Test4");
output.Should().NotContain("Test5");

_testOutput.WriteLine(output);
testOutput.WriteLine(output);
}
}
27 changes: 9 additions & 18 deletions GitHubActionsTestLogger/GitHubWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@
namespace GitHubActionsTestLogger;

// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
public partial class GitHubWorkflow
public partial class GitHubWorkflow(TextWriter commandWriter, TextWriter summaryWriter)
{
private readonly TextWriter _commandWriter;
private readonly TextWriter _summaryWriter;

public GitHubWorkflow(TextWriter commandWriter, TextWriter summaryWriter)
{
_commandWriter = commandWriter;
_summaryWriter = summaryWriter;
}

private void InvokeCommand(
string command,
string message,
Expand All @@ -38,14 +29,14 @@ static string Escape(string value) =>
// to make sure there is no preceding text.
// Preceding text may sometimes appear if the .NET CLI is running with
// ANSI color codes enabled.
_commandWriter.WriteLine();
commandWriter.WriteLine();

_commandWriter.WriteLine($"::{command} {formattedOptions}::{Escape(message)}");
commandWriter.WriteLine($"::{command} {formattedOptions}::{Escape(message)}");

// This newline is just for symmetry
_commandWriter.WriteLine();
commandWriter.WriteLine();

_commandWriter.Flush();
commandWriter.Flush();
}

public void CreateErrorAnnotation(
Expand Down Expand Up @@ -76,11 +67,11 @@ public void CreateSummary(string content)
// which can screw up markdown parsing, so we need to make sure
// there's at least two newlines before our summary to be safe.
// https://github.com/Tyrrrz/GitHubActionsTestLogger/issues/22
_summaryWriter.WriteLine();
_summaryWriter.WriteLine();
summaryWriter.WriteLine();
summaryWriter.WriteLine();

_summaryWriter.WriteLine(content);
_summaryWriter.Flush();
summaryWriter.WriteLine(content);
summaryWriter.Flush();
}
}

Expand Down
16 changes: 4 additions & 12 deletions GitHubActionsTestLogger/TestLoggerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@

namespace GitHubActionsTestLogger;

public class TestLoggerContext
public class TestLoggerContext(GitHubWorkflow github, TestLoggerOptions options)
{
private readonly GitHubWorkflow _github;

private readonly object _lock = new();
private TestRunCriteria? _testRunCriteria;
private readonly List<TestResult> _testResults = new();

public TestLoggerOptions Options { get; }

public TestLoggerContext(GitHubWorkflow github, TestLoggerOptions options)
{
_github = github;
Options = options;
}
public TestLoggerOptions Options { get; } = options;

private string FormatAnnotation(string format, TestResult testResult)
{
Expand Down Expand Up @@ -89,7 +81,7 @@ public void HandleTestResult(TestResultEventArgs args)
// Report failed test results to job annotations
if (args.Result.Outcome == TestOutcome.Failed)
{
_github.CreateErrorAnnotation(
github.CreateErrorAnnotation(
FormatAnnotationTitle(args.Result),
FormatAnnotationMessage(args.Result),
args.Result.TryGetSourceFilePath(),
Expand Down Expand Up @@ -141,7 +133,7 @@ public void HandleTestRunComplete(TestRunCompleteEventArgs args)
TestResults = testResults
};

_github.CreateSummary(template.Render());
github.CreateSummary(template.Render());
}
}
}
13 changes: 2 additions & 11 deletions GitHubActionsTestLogger/Utils/ContentionTolerantWriteFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

namespace GitHubActionsTestLogger.Utils;

internal class ContentionTolerantWriteFileStream : Stream
internal class ContentionTolerantWriteFileStream(string filePath, FileMode fileMode) : Stream
{
private readonly string _filePath;
private readonly FileMode _fileMode;

private readonly List<byte> _buffer = new(1024);

[ExcludeFromCodeCoverage]
Expand All @@ -29,20 +26,14 @@ internal class ContentionTolerantWriteFileStream : Stream
[ExcludeFromCodeCoverage]
public override long Position { get; set; }

public ContentionTolerantWriteFileStream(string filePath, FileMode fileMode)
{
_filePath = filePath;
_fileMode = fileMode;
}

// Backoff and retry if the file is locked
private FileStream CreateInnerStream()
{
for (var retriesRemaining = 10; ; retriesRemaining--)
{
try
{
return new FileStream(_filePath, _fileMode);
return new FileStream(filePath, fileMode);
}
catch (IOException) when (retriesRemaining > 0)
{
Expand Down
15 changes: 4 additions & 11 deletions GitHubActionsTestLogger/Utils/StackFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
namespace GitHubActionsTestLogger.Utils;

// Adapted from https://github.com/atifaziz/StackTraceParser
internal partial class StackFrame
internal partial class StackFrame(string methodCall, string? filePath, int? line)
{
public string MethodCall { get; }

public string? FilePath { get; }
public string MethodCall { get; } = methodCall;

public int? Line { get; }
public string? FilePath { get; } = filePath;

public StackFrame(string methodCall, string? filePath, int? line)
{
MethodCall = methodCall;
FilePath = filePath;
Line = line;
}
public int? Line { get; } = line;
}

internal partial class StackFrame
Expand Down

0 comments on commit 7a9a9bd

Please sign in to comment.