Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 2 additions & 0 deletions TUnit.Engine/Extensions/TestApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
var htmlReporter = new Reporters.Html.HtmlReporter(extension);
var htmlReporterCommandProvider = new HtmlReporterCommandProvider(extension);

htmlReporter.SetGitHubReporter(githubReporter);

testApplicationBuilder.RegisterTestFramework(
serviceProvider => new TestFrameworkCapabilities(CreateCapabilities(serviceProvider)),
(capabilities, serviceProvider) => new TUnitTestFramework(extension, serviceProvider, capabilities));
Expand Down
22 changes: 21 additions & 1 deletion TUnit.Engine/Reporters/GitHubReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,17 @@ public Task AfterRunAsync(int exitCode, CancellationToken cancellation)
x.Value.TestNode.Properties.AsEnumerable().Any(p => p is InProgressTestNodeStateProperty)).ToArray();

var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"### {Assembly.GetEntryAssembly()?.GetName().Name} ({targetFramework})");

var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name;

if (!string.IsNullOrEmpty(ArtifactUrl))
{
stringBuilder.AppendLine($"### {assemblyName} ({targetFramework}) [(View Report)]({ArtifactUrl})");
}
else
{
stringBuilder.AppendLine($"### {assemblyName} ({targetFramework})");
}

if (!string.IsNullOrEmpty(Filter))
{
Expand Down Expand Up @@ -167,6 +177,12 @@ public Task AfterRunAsync(int exitCode, CancellationToken cancellation)
stringBuilder.AppendLine($"| {inProgress.Length} | In Progress (never completed) |");
}

if (ShowArtifactUploadTip)
{
stringBuilder.AppendLine();
stringBuilder.AppendLine("> **Tip:** You can have HTML reports uploaded automatically as artifacts. [Learn more](https://tunit.dev/docs/guides/html-report#enabling-automatic-artifact-upload)");
}

if (passedCount == last.Count)
{
return WriteFile(stringBuilder.ToString());
Expand Down Expand Up @@ -370,6 +386,10 @@ private static string GetStatus(IProperty? stateProperty)

public string? Filter { get; set; }

// Set by HtmlReporter during OnTestSessionFinishingAsync, which MTP invokes before AfterRunAsync.
internal string? ArtifactUrl { get; set; }
internal bool ShowArtifactUploadTip { get; set; }

internal void SetReporterStyle(GitHubReporterStyle style)
{
_reporterStyle = style;
Expand Down
39 changes: 13 additions & 26 deletions TUnit.Engine/Reporters/Html/HtmlReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal sealed class HtmlReporter(IExtension extension) : IDataConsumer, IDataP
private IMessageBus? _messageBus;
private string _resultsDirectory = "TestResults";
private readonly ConcurrentDictionary<string, ConcurrentQueue<TestNodeUpdateMessage>> _updates = [];
private GitHubReporter? _githubReporter;

#if NET
private ActivityCollector? _activityCollector;
Expand Down Expand Up @@ -179,6 +180,11 @@ internal void SetMessageBus(IMessageBus? messageBus)
_messageBus = messageBus;
}

internal void SetGitHubReporter(GitHubReporter githubReporter)
{
_githubReporter = githubReporter;
}

// Called by the AddTestSessionLifetimeHandler factory at startup, before any session events fire,
// so _resultsDirectory is guaranteed to be set before OnTestSessionFinishingAsync is invoked.
internal void SetResultsDirectory(string path)
Expand Down Expand Up @@ -648,14 +654,13 @@ private static bool IsFileLocked(IOException exception)
exception.Message.Contains("access denied", StringComparison.OrdinalIgnoreCase);
}

private static async Task TryGitHubIntegrationAsync(string filePath, CancellationToken cancellationToken)
private async Task TryGitHubIntegrationAsync(string filePath, CancellationToken cancellationToken)
{
if (Environment.GetEnvironmentVariable(EnvironmentConstants.GitHubActions) is not "true")
{
return;
}

var summaryPath = Environment.GetEnvironmentVariable(EnvironmentConstants.GitHubStepSummary);
var repo = Environment.GetEnvironmentVariable(EnvironmentConstants.GitHubRepository);
var runId = Environment.GetEnvironmentVariable(EnvironmentConstants.GitHubRunId);

Expand All @@ -669,8 +674,7 @@ private static async Task TryGitHubIntegrationAsync(string filePath, Cancellatio
{
Console.WriteLine("Tip: To enable automatic HTML report artifact upload, see https://tunit.dev/docs/guides/html-report#enabling-automatic-artifact-upload");
}

if (hasRuntimeToken)
else
{
try
{
Expand All @@ -687,32 +691,15 @@ private static async Task TryGitHubIntegrationAsync(string filePath, Cancellatio
}
}

// Write to step summary
if (!string.IsNullOrEmpty(summaryPath))
if (_githubReporter is not null)
{
try
if (!hasRuntimeToken)
{
var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name ?? Path.GetFileNameWithoutExtension(filePath);
string line;

if (artifactId is not null && !string.IsNullOrEmpty(repo) && !string.IsNullOrEmpty(runId))
{
line = $"\n\ud83d\udcca [{assemblyName} — View HTML Report](https://github.com/{repo}/actions/runs/{runId}/artifacts/{artifactId})\n";
}
else
{
line = $"\n\ud83d\udcca **{assemblyName}** HTML report was generated — [Enable automatic artifact upload](https://tunit.dev/docs/guides/html-report#enabling-automatic-artifact-upload)\n";
}

#if NET
await File.AppendAllTextAsync(summaryPath, line, cancellationToken);
#else
File.AppendAllText(summaryPath, line);
#endif
_githubReporter.ShowArtifactUploadTip = true;
}
catch (Exception ex)
else if (artifactId is not null && !string.IsNullOrEmpty(repo) && !string.IsNullOrEmpty(runId))
{
Console.WriteLine($"Warning: Failed to write GitHub step summary: {ex.Message}");
_githubReporter.ArtifactUrl = $"https://github.com/{repo}/actions/runs/{runId}/artifacts/{artifactId}";
}
}
}
Expand Down
Loading