Skip to content

Commit b87769f

Browse files
Display an error when '--maximum-failed-tests' is used with a framewo… (#4326)
Co-authored-by: Youssef Victor <[email protected]>
1 parent 33f94f8 commit b87769f

21 files changed

+127
-13
lines changed

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineManager.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@ namespace Microsoft.Testing.Platform.CommandLine;
1111

1212
internal sealed class CommandLineManager(IRuntimeFeature runtimeFeature, ITestApplicationModuleInfo testApplicationModuleInfo) : ICommandLineManager
1313
{
14-
private readonly List<Func<ICommandLineOptionsProvider>> _commandLineProviderFactory = [];
14+
private readonly List<Func<IServiceProvider, ICommandLineOptionsProvider>> _commandLineProviderFactory = [];
1515
private readonly IRuntimeFeature _runtimeFeature = runtimeFeature;
1616
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;
1717

1818
public void AddProvider(Func<ICommandLineOptionsProvider> commandLineProviderFactory)
19+
{
20+
Guard.NotNull(commandLineProviderFactory);
21+
_commandLineProviderFactory.Add(_ => commandLineProviderFactory());
22+
}
23+
24+
public void AddProvider(Func<IServiceProvider, ICommandLineOptionsProvider> commandLineProviderFactory)
1925
{
2026
Guard.NotNull(commandLineProviderFactory);
2127
_commandLineProviderFactory.Add(commandLineProviderFactory);
2228
}
2329

24-
internal async Task<CommandLineHandler> BuildAsync(CommandLineParseResult parseResult)
30+
internal async Task<CommandLineHandler> BuildAsync(CommandLineParseResult parseResult, IServiceProvider serviceProvider)
2531
{
2632
List<ICommandLineOptionsProvider> commandLineOptionsProviders = [];
27-
foreach (Func<ICommandLineOptionsProvider> commandLineProviderFactory in _commandLineProviderFactory)
33+
foreach (Func<IServiceProvider, ICommandLineOptionsProvider> commandLineProviderFactory in _commandLineProviderFactory)
2834
{
29-
ICommandLineOptionsProvider commandLineOptionsProvider = commandLineProviderFactory();
35+
ICommandLineOptionsProvider commandLineOptionsProvider = commandLineProviderFactory(serviceProvider);
3036
if (!await commandLineOptionsProvider.IsEnabledAsync())
3137
{
3238
continue;

src/Platform/Microsoft.Testing.Platform/CommandLine/ICommandLineManager.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
using Microsoft.Testing.Platform.Extensions.CommandLine;
57

68
namespace Microsoft.Testing.Platform.CommandLine;
@@ -15,4 +17,11 @@ public interface ICommandLineManager
1517
/// </summary>
1618
/// <param name="commandLineProviderFactory">The factory method for creating the command line options provider.</param>
1719
void AddProvider(Func<ICommandLineOptionsProvider> commandLineProviderFactory);
20+
21+
/// <summary>
22+
/// Adds a command line options provider.
23+
/// </summary>
24+
/// <param name="commandLineProviderFactory">The factory method for creating the command line options provider, given a service provider.</param>
25+
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")]
26+
void AddProvider(Func<IServiceProvider, ICommandLineOptionsProvider> commandLineProviderFactory);
1827
}

src/Platform/Microsoft.Testing.Platform/CommandLine/MaxFailedTestsCommandLineOptionsProvider.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
using System.Globalization;
55

6+
using Microsoft.Testing.Platform.Capabilities.TestFramework;
67
using Microsoft.Testing.Platform.Extensions;
78
using Microsoft.Testing.Platform.Extensions.CommandLine;
89
using Microsoft.Testing.Platform.Helpers;
910
using Microsoft.Testing.Platform.Resources;
11+
using Microsoft.Testing.Platform.Services;
1012

1113
namespace Microsoft.Testing.Platform.CommandLine;
1214

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

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

5153
throw ApplicationStateGuard.Unreachable();
5254
}
55+
56+
private Task<ValidationResult> ValidateCapabilityAsync()
57+
=> serviceProvider.GetTestFrameworkCapabilities().Capabilities.OfType<IGracefulStopTestExecutionCapability>().Any()
58+
? ValidationResult.ValidTask
59+
: ValidationResult.InvalidTask(PlatformResources.AbortForMaxFailedTestsCapabilityNotAvailable);
5360
}

src/Platform/Microsoft.Testing.Platform/Helpers/TestApplicationBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public static void AddTreeNodeFilterService(this ITestApplicationBuilder testApp
2222
/// <param name="builder">The test application builder.</param>
2323
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")]
2424
public static void AddMaximumFailedTestsService(this ITestApplicationBuilder builder, IExtension extension)
25-
=> builder.CommandLine.AddProvider(() => new MaxFailedTestsCommandLineOptionsProvider(extension));
25+
=> builder.CommandLine.AddProvider(serviceProvider => new MaxFailedTestsCommandLineOptionsProvider(extension, serviceProvider));
2626
}

src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public async Task<ITestHost> BuildAsync(
201201

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

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

250+
// Register the test framework capabilities to be used by services
251+
serviceProvider.AddService(testFrameworkCapabilities);
252+
250253
// If command line is not valid we return immediately.
251254
ValidationResult commandLineValidationResult = await CommandLineOptionsValidator.ValidateAsync(
252255
loggingState.CommandLineParseResult,
253256
commandLineHandler.SystemCommandLineOptionsProviders,
254257
commandLineHandler.ExtensionsCommandLineOptionsProviders,
255258
commandLineHandler);
256259

257-
// Register the test framework capabilities to be used by services
258-
serviceProvider.AddService(testFrameworkCapabilities);
259-
260260
if (!loggingState.CommandLineParseResult.HasTool && !commandLineValidationResult.IsValid)
261261
{
262262
await DisplayBannerIfEnabledAsync(loggingState, proxyOutputDevice, testFrameworkCapabilities);

src/Platform/Microsoft.Testing.Platform/PublicAPI/PublicAPI.Unshipped.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData.Message.g
88
Microsoft.Testing.Platform.OutputDevice.WarningMessageOutputDeviceData.WarningMessageOutputDeviceData(string! message) -> void
99
[TPEXP]Microsoft.Testing.Platform.Capabilities.TestFramework.IGracefulStopTestExecutionCapability
1010
[TPEXP]Microsoft.Testing.Platform.Capabilities.TestFramework.IGracefulStopTestExecutionCapability.StopTestExecutionAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
11+
[TPEXP]Microsoft.Testing.Platform.CommandLine.ICommandLineManager.AddProvider(System.Func<System.IServiceProvider!, Microsoft.Testing.Platform.Extensions.CommandLine.ICommandLineOptionsProvider!>! commandLineProviderFactory) -> void
1112
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty
1213
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty.StandardOutput.get -> string!
1314
[TPEXP]Microsoft.Testing.Platform.Extensions.Messages.StandardOutputProperty.StandardOutput.init -> void

src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,7 @@ Takes one argument as string in the format &lt;value&gt;[h|m|s] where 'value' is
713713
<value>Test session is aborting due to reaching failures ('{0}') specified by the '--maximum-failed-tests' option.</value>
714714
<comment>{0} is the number of max failed tests.</comment>
715715
</data>
716-
</root>
716+
<data name="AbortForMaxFailedTestsCapabilityNotAvailable" xml:space="preserve">
717+
<value>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</value>
718+
</data>
719+
</root>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="cs" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="de" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="es" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="fr" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="it" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="ja" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="ko" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="pl" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<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">
33
<file datatype="xml" source-language="en" target-language="pt-BR" original="../PlatformResources.resx">
44
<body>
5+
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
6+
<source>The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</source>
7+
<target state="new">The current test framework does not implement 'IGracefulStopTestExecutionCapability' which is required for '--maximum-failed-tests' feature.</target>
8+
<note />
9+
</trans-unit>
510
<trans-unit id="AbortForMaxFailedTestsDescription">
611
<source>Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
712
<target state="new">Extension used to support '--maximum-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>

0 commit comments

Comments
 (0)