Skip to content

Commit

Permalink
Display an error when '--maximum-failed-tests' is used with a framewo…
Browse files Browse the repository at this point in the history
…rk not implementing the required capability (#4321)
  • Loading branch information
Youssef1313 authored Dec 12, 2024
1 parent 0e55e43 commit b097a45
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@ namespace Microsoft.Testing.Platform.CommandLine;

internal sealed class CommandLineManager(IRuntimeFeature runtimeFeature, ITestApplicationModuleInfo testApplicationModuleInfo) : ICommandLineManager
{
private readonly List<Func<ICommandLineOptionsProvider>> _commandLineProviderFactory = [];
private readonly List<Func<IServiceProvider, ICommandLineOptionsProvider>> _commandLineProviderFactory = [];
private readonly IRuntimeFeature _runtimeFeature = runtimeFeature;
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;

public void AddProvider(Func<ICommandLineOptionsProvider> commandLineProviderFactory)
{
Guard.NotNull(commandLineProviderFactory);
_commandLineProviderFactory.Add(_ => commandLineProviderFactory());
}

public void AddProvider(Func<IServiceProvider, ICommandLineOptionsProvider> commandLineProviderFactory)
{
Guard.NotNull(commandLineProviderFactory);
_commandLineProviderFactory.Add(commandLineProviderFactory);
}

internal async Task<CommandLineHandler> BuildAsync(CommandLineParseResult parseResult)
internal async Task<CommandLineHandler> BuildAsync(CommandLineParseResult parseResult, IServiceProvider serviceProvider)
{
List<ICommandLineOptionsProvider> commandLineOptionsProviders = [];
foreach (Func<ICommandLineOptionsProvider> commandLineProviderFactory in _commandLineProviderFactory)
foreach (Func<IServiceProvider, ICommandLineOptionsProvider> commandLineProviderFactory in _commandLineProviderFactory)
{
ICommandLineOptionsProvider commandLineOptionsProvider = commandLineProviderFactory();
ICommandLineOptionsProvider commandLineOptionsProvider = commandLineProviderFactory(serviceProvider);
if (!await commandLineOptionsProvider.IsEnabledAsync())
{
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;

using Microsoft.Testing.Platform.Extensions.CommandLine;

namespace Microsoft.Testing.Platform.CommandLine;
Expand All @@ -15,4 +17,11 @@ public interface ICommandLineManager
/// </summary>
/// <param name="commandLineProviderFactory">The factory method for creating the command line options provider.</param>
void AddProvider(Func<ICommandLineOptionsProvider> commandLineProviderFactory);

/// <summary>
/// Adds a command line options provider.
/// </summary>
/// <param name="commandLineProviderFactory">The factory method for creating the command line options provider, given a service provider.</param>
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")]
void AddProvider(Func<IServiceProvider, ICommandLineOptionsProvider> commandLineProviderFactory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

using System.Globalization;

using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.CommandLine;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Resources;
using Microsoft.Testing.Platform.Services;

namespace Microsoft.Testing.Platform.CommandLine;

internal sealed class MaxFailedTestsCommandLineOptionsProvider(IExtension extension) : ICommandLineOptionsProvider
internal sealed class MaxFailedTestsCommandLineOptionsProvider(IExtension extension, IServiceProvider serviceProvider) : ICommandLineOptionsProvider
{
internal const string MaxFailedTestsOptionKey = "maximum-failed-tests";

Expand Down Expand Up @@ -44,10 +46,15 @@ public Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption com
// The idea is that we stop the execution when we *reach* the max failed tests, not when *exceed*.
// So the value 1 means, stop execution on the first failure.
return int.TryParse(arg, out int maxFailedTestsResult) && maxFailedTestsResult > 0
? ValidationResult.ValidTask
? ValidateCapabilityAsync()
: ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, PlatformResources.MaxFailedTestsMustBePositive, arg));
}

throw ApplicationStateGuard.Unreachable();
}

private Task<ValidationResult> ValidateCapabilityAsync()
=> serviceProvider.GetTestFrameworkCapabilities().Capabilities.OfType<IGracefulStopTestExecutionCapability>().Any()
? ValidationResult.ValidTask
: ValidationResult.InvalidTask(PlatformResources.AbortForMaxFailedTestsCapabilityNotAvailable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public static void AddTreeNodeFilterService(this ITestApplicationBuilder testApp
/// <param name="builder">The test application builder.</param>
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")]
public static void AddMaximumFailedTestsService(this ITestApplicationBuilder builder, IExtension extension)
=> builder.CommandLine.AddProvider(() => new MaxFailedTestsCommandLineOptionsProvider(extension));
=> builder.CommandLine.AddProvider(serviceProvider => new MaxFailedTestsCommandLineOptionsProvider(extension, serviceProvider));
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public async Task<ITestHost> BuildAsync(

// Build the command line service - we need special treatment because is possible that an extension query it during the creation.
// Add Retry default argument commandlines
CommandLineHandler commandLineHandler = await ((CommandLineManager)CommandLine).BuildAsync(loggingState.CommandLineParseResult);
CommandLineHandler commandLineHandler = await ((CommandLineManager)CommandLine).BuildAsync(loggingState.CommandLineParseResult, serviceProvider);

// Set the concrete command line options to the proxy.
commandLineOptionsProxy.SetCommandLineOptions(commandLineHandler);
Expand Down Expand Up @@ -247,16 +247,16 @@ public async Task<ITestHost> BuildAsync(
await testFrameworkCapabilitiesAsyncInitializable.InitializeAsync();
}

// Register the test framework capabilities to be used by services
serviceProvider.AddService(testFrameworkCapabilities);

// If command line is not valid we return immediately.
ValidationResult commandLineValidationResult = await CommandLineOptionsValidator.ValidateAsync(
loggingState.CommandLineParseResult,
commandLineHandler.SystemCommandLineOptionsProviders,
commandLineHandler.ExtensionsCommandLineOptionsProviders,
commandLineHandler);

// Register the test framework capabilities to be used by services
serviceProvider.AddService(testFrameworkCapabilities);

if (!loggingState.CommandLineParseResult.HasTool && !commandLineValidationResult.IsValid)
{
await DisplayBannerIfEnabledAsync(loggingState, proxyOutputDevice, testFrameworkCapabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData.Message.g
Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData.WarningMessageOutputDeviceData(string! message) -> void
[TPEXP]Microsoft.Testing.Platform.Capabilities.TestFramework.IGracefulStopTestExecutionCapability
[TPEXP]Microsoft.Testing.Platform.Capabilities.TestFramework.IGracefulStopTestExecutionCapability.StopTestExecutionAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
[TPEXP]Microsoft.Testing.Platform.CommandLine.ICommandLineManager.AddProvider(System.Func<System.IServiceProvider!, Microsoft.Testing.Platform.Extensions.CommandLine.ICommandLineOptionsProvider!>! commandLineProviderFactory) -> void
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty.StandardOutput.get -> string!
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty.StandardOutput.init -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,7 @@ Takes one argument as string in the format &lt;value&gt;[h|m|s] where 'value' is
<value>Test session is aborting due to reaching failures ('{0}') specified by the '--maximum-failed-tests' option.</value>
<comment>{0} is the number of max failed tests.</comment>
</data>
</root>
<data name="AbortForMaxFailedTestsCapabilityNotAvailable" xml:space="preserve">
<value>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
<note />
</trans-unit>
<trans-unit id="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
Expand Down
Loading

0 comments on commit b097a45

Please sign in to comment.