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
56 changes: 56 additions & 0 deletions TUnit.Engine/CommandLineProviders/HtmlReporterCommandProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.CommandLine;

namespace TUnit.Engine.CommandLineProviders;

internal class HtmlReporterCommandProvider(IExtension extension) : ICommandLineOptionsProvider
{
public const string ReportHtml = "report-html";
public const string ReportHtmlFilename = "report-html-filename";

public Task<bool> IsEnabledAsync() => 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(
ReportHtml,
"Generate an HTML test report",
ArgumentArity.Zero,
false),
new CommandLineOption(
ReportHtmlFilename,
"Path for the HTML test report file (default: TestResults/{AssemblyName}-report.html)",
ArgumentArity.ExactlyOne,
false)
];
}

public Task<ValidationResult> ValidateOptionArgumentsAsync(
CommandLineOption commandOption,
string[] arguments)
{
if (commandOption.Name == ReportHtmlFilename && arguments.Length != 1)
{
return ValidationResult.InvalidTask("A single output path must be provided for the HTML report");
}

return ValidationResult.ValidTask;
}

public Task<ValidationResult> ValidateCommandLineOptionsAsync(
ICommandLineOptions commandLineOptions)
{
return ValidationResult.ValidTask;
}
}
26 changes: 26 additions & 0 deletions TUnit.Engine/Extensions/TestApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
var junitReporter = new JUnitReporter(extension);
var junitReporterCommandProvider = new JUnitReporterCommandProvider(extension);

var htmlReporter = new HtmlReporter(extension);
var htmlReporterCommandProvider = new HtmlReporterCommandProvider(extension);

testApplicationBuilder.RegisterTestFramework(
serviceProvider => new TestFrameworkCapabilities(CreateCapabilities(serviceProvider)),
(capabilities, serviceProvider) => new TUnitTestFramework(extension, serviceProvider, capabilities));
Expand Down Expand Up @@ -52,6 +55,9 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
// JUnit reporter configuration
testApplicationBuilder.CommandLine.AddProvider(() => junitReporterCommandProvider);

// HTML reporter configuration
testApplicationBuilder.CommandLine.AddProvider(() => htmlReporterCommandProvider);

testApplicationBuilder.TestHost.AddDataConsumer(serviceProvider =>
{
// Apply command-line configuration if provided
Expand All @@ -76,6 +82,26 @@ public static void AddTUnit(this ITestApplicationBuilder testApplicationBuilder)
return junitReporter;
});
testApplicationBuilder.TestHost.AddTestHostApplicationLifetime(_ => junitReporter);

testApplicationBuilder.TestHost.AddDataConsumer(serviceProvider =>
{
var commandLineOptions = serviceProvider.GetRequiredService<ICommandLineOptions>();

// Enable if --report-html flag is set or --report-html-filename is provided
if (commandLineOptions.IsOptionSet(HtmlReporterCommandProvider.ReportHtml)
|| commandLineOptions.TryGetOptionArgumentList(HtmlReporterCommandProvider.ReportHtmlFilename, out _))
{
htmlReporter.Enable();
}

if (commandLineOptions.TryGetOptionArgumentList(HtmlReporterCommandProvider.ReportHtmlFilename, out var pathArgs))
{
htmlReporter.SetOutputPath(pathArgs[0]);
}

return htmlReporter;
});
testApplicationBuilder.TestHost.AddTestHostApplicationLifetime(_ => htmlReporter);
}

private static IReadOnlyCollection<ITestFrameworkCapability> CreateCapabilities(IServiceProvider serviceProvider)
Expand Down
Loading
Loading