Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
125 changes: 48 additions & 77 deletions TUnit.Engine.Tests/GitHubReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TUnit.Engine.Tests;

[NotInParallel]
public class GitHubReporterTests
{
private sealed class MockExtension : IExtension
Expand All @@ -14,63 +15,54 @@ private sealed class MockExtension : IExtension
public string Description => "Mock Extension";
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
}


[After(Test)]
public void CleanupAfterTest()
{
// Reset all environment variables
Environment.SetEnvironmentVariable("TUNIT_DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("TUNIT_GITHUB_REPORTER_STYLE", null);
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", null);
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", null);
}

[Test]
public async Task IsEnabledAsync_Should_Return_False_When_TUNIT_DISABLE_GITHUB_REPORTER_Is_Set()
{
// Arrange
Environment.SetEnvironmentVariable("TUNIT_DISABLE_GITHUB_REPORTER", "true");
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", "true");
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", CreateTempFile());

var extension = new MockExtension();
var reporter = new GitHubReporter(extension);

try
{
// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}
finally
{
// Cleanup
Environment.SetEnvironmentVariable("TUNIT_DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", null);
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", null);
}

// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}

[Test]
public async Task IsEnabledAsync_Should_Return_False_When_DISABLE_GITHUB_REPORTER_Is_Set()
{
// Arrange
Environment.SetEnvironmentVariable("DISABLE_GITHUB_REPORTER", "true");
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", "true");
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", CreateTempFile());

var extension = new MockExtension();
var reporter = new GitHubReporter(extension);

try
{
// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}
finally
{
// Cleanup
Environment.SetEnvironmentVariable("DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", null);
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", null);
}

// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}

[Test]
public async Task IsEnabledAsync_Should_Return_False_When_Both_Environment_Variables_Are_Set()
{
Expand All @@ -79,28 +71,17 @@ public async Task IsEnabledAsync_Should_Return_False_When_Both_Environment_Varia
Environment.SetEnvironmentVariable("DISABLE_GITHUB_REPORTER", "true");
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", "true");
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", CreateTempFile());

var extension = new MockExtension();
var reporter = new GitHubReporter(extension);

try
{
// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}
finally
{
// Cleanup
Environment.SetEnvironmentVariable("TUNIT_DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", null);
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", null);
}

// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}

[Test]
public async Task IsEnabledAsync_Should_Return_False_When_GITHUB_ACTIONS_Is_Not_Set()
{
Expand All @@ -109,29 +90,19 @@ public async Task IsEnabledAsync_Should_Return_False_When_GITHUB_ACTIONS_Is_Not_
Environment.SetEnvironmentVariable("DISABLE_GITHUB_REPORTER", null);
Environment.SetEnvironmentVariable("GITHUB_ACTIONS", null);
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", CreateTempFile());

var extension = new MockExtension();
var reporter = new GitHubReporter(extension);

try
{
// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}
finally
{
// Cleanup
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", null);
}

// Act
var result = await reporter.IsEnabledAsync();

// Assert
result.ShouldBeFalse();
}
private static string CreateTempFile()

private string CreateTempFile()
{
var tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, "test");
return tempFile;
return Path.GetTempFileName();
}
}
}
76 changes: 76 additions & 0 deletions TUnit.Engine/CommandLineProviders/GitHubReporterCommandProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.CommandLine;
using TUnit.Engine.Reporters;

namespace TUnit.Engine.CommandLineProviders;

internal class GitHubReporterCommandProvider(IExtension extension) : ICommandLineOptionsProvider
{
public const string GitHubReporterStyleOption = "github-reporter-style";

public Task<bool> IsEnabledAsync()
{
return extension.IsEnabledAsync();
}

public string Uid => extension.Uid;

public string Version => extension.Version;

public string DisplayName => extension.DisplayName;

public string Description => extension.Description;

public IReadOnlyCollection<CommandLineOption> GetCommandLineOptions()
{
return
[
new CommandLineOption(GitHubReporterStyleOption, "GitHub reporter output style: collapsible (default) or full", ArgumentArity.ExactlyOne, false)
];
}

public Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption commandOption, string[] arguments)
{
if (commandOption.Name == GitHubReporterStyleOption && arguments.Length != 1)
{
return ValidationResult.InvalidTask("A single reporter style must be provided: collapsible or full");
}

if (commandOption.Name == GitHubReporterStyleOption)
{
var style = arguments[0].ToLowerInvariant();
if (!IsValidReporterStyle(style))
{
return ValidationResult.InvalidTask($"Invalid reporter style '{arguments[0]}'. Valid options: collapsible, full");
}
}

return ValidationResult.ValidTask;
}

public Task<ValidationResult> ValidateCommandLineOptionsAsync(ICommandLineOptions commandLineOptions)
{
return ValidationResult.ValidTask;
}

private static bool IsValidReporterStyle(string style)
{
return style is "collapsible" or "full";
}

public static GitHubReporterStyle ParseReporterStyle(string[] arguments)
{
if (arguments.Length == 0)
{
return GitHubReporterStyle.Collapsible;
}

return arguments[0].ToLowerInvariant() switch
{
"collapsible" => GitHubReporterStyle.Collapsible,
"full" => GitHubReporterStyle.Full,
_ => GitHubReporterStyle.Collapsible
};
}
}
17 changes: 16 additions & 1 deletion TUnit.Engine/Extensions/TestApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using TUnit.Engine.CommandLineProviders;
using TUnit.Engine.Framework;
using TUnit.Engine.Reporters;
using Microsoft.Testing.Platform.CommandLine;

#pragma warning disable TPEXP

Expand All @@ -18,6 +19,7 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
TUnitExtension extension = new();

var githubReporter = new GitHubReporter(extension);
var githubReporterCommandProvider = new GitHubReporterCommandProvider(extension);

testApplicationBuilder.RegisterTestFramework(
serviceProvider => new TestFrameworkCapabilities(CreateCapabilities(serviceProvider)),
Expand All @@ -43,7 +45,20 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
// Keep detailed stacktrace option for backward compatibility
testApplicationBuilder.CommandLine.AddProvider(() => new DetailedStacktraceCommandProvider(extension));

testApplicationBuilder.TestHost.AddDataConsumer(_ => githubReporter);
// GitHub reporter configuration
testApplicationBuilder.CommandLine.AddProvider(() => githubReporterCommandProvider);

testApplicationBuilder.TestHost.AddDataConsumer(serviceProvider =>
{
// Apply command-line configuration if provided
var commandLineOptions = serviceProvider.GetRequiredService<ICommandLineOptions>();
if (commandLineOptions.TryGetOptionArgumentList(GitHubReporterCommandProvider.GitHubReporterStyleOption, out var styleArgs))
{
var style = GitHubReporterCommandProvider.ParseReporterStyle(styleArgs);
githubReporter.SetReporterStyle(style);
}
return githubReporter;
});
testApplicationBuilder.TestHost.AddTestHostApplicationLifetime(_ => githubReporter);
}

Expand Down
1 change: 1 addition & 0 deletions TUnit.Engine/Helpers/EnvironmentVariableCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal static class EnvironmentVariableCache
"TUNIT_ADAPTIVE_MAX_PARALLELISM",
"TUNIT_ADAPTIVE_METRICS",
"TUNIT_DISABLE_GITHUB_REPORTER",
"TUNIT_GITHUB_REPORTER_STYLE",

// CI environment detection variables
"CI",
Expand Down
Loading
Loading