Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support '--maximum-failed-tests' to abort test run when failure threshold is reached #4238

Merged
merged 67 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
12520ce
Support '--max-failed-tests' to abort test run when failure threshold…
Youssef1313 Dec 4, 2024
dd480c2
Address review comment
Youssef1313 Dec 4, 2024
e9a4dd8
Remove unused using
Youssef1313 Dec 4, 2024
a9e7d32
Progress
Youssef1313 Dec 4, 2024
f37ea38
Not ideal, and not working even. Keeping in history though
Youssef1313 Dec 4, 2024
ce36d36
Fix build error
Youssef1313 Dec 4, 2024
cb1d50a
Revert
Youssef1313 Dec 5, 2024
0717a76
Use a capability
Youssef1313 Dec 5, 2024
bd63803
Take a different approach
Youssef1313 Dec 5, 2024
46b7577
Progress for policies service
Youssef1313 Dec 5, 2024
be7c6cc
Use GetRequiredService
Youssef1313 Dec 5, 2024
db432ef
Progress
Youssef1313 Dec 5, 2024
c7b1392
Fix warning
Youssef1313 Dec 5, 2024
624f86f
Progress
Youssef1313 Dec 6, 2024
5ef6a17
Progress
Youssef1313 Dec 6, 2024
e9ce6da
Merge branch 'main' into max-failed
Youssef1313 Dec 6, 2024
c53c7d5
Remove done TODO
Youssef1313 Dec 6, 2024
7571531
Cleanup
Youssef1313 Dec 6, 2024
1d245ec
Small fixes
Youssef1313 Dec 6, 2024
993f949
Experimental
Youssef1313 Dec 6, 2024
5496617
Register only when enabled
Youssef1313 Dec 6, 2024
db426ed
Fix build
Youssef1313 Dec 6, 2024
f66be1d
Small progress
Youssef1313 Dec 6, 2024
3240e68
Renames
Youssef1313 Dec 6, 2024
13ea139
Cleanup
Youssef1313 Dec 6, 2024
db0fded
Update tests
Youssef1313 Dec 6, 2024
efa34f1
Bit of progress
Youssef1313 Dec 6, 2024
e3ecc85
Adjust messaging
Youssef1313 Dec 6, 2024
ceae0cc
More progress
Youssef1313 Dec 6, 2024
1634d40
Small fix
Youssef1313 Dec 6, 2024
d2a2c82
Fix test failures
Youssef1313 Dec 6, 2024
60f6e41
Adjust cancellation
Youssef1313 Dec 7, 2024
0a9999a
Adjust for potential race conditions
Youssef1313 Dec 7, 2024
bf81e20
Rename
Youssef1313 Dec 9, 2024
5fb51f4
MSTest side of the feature
Youssef1313 Dec 9, 2024
f928c07
Progress
Youssef1313 Dec 9, 2024
43ba49e
Minor fixes
Youssef1313 Dec 9, 2024
7199f00
Cleanup
Youssef1313 Dec 9, 2024
f57c0a6
TODO:
Youssef1313 Dec 9, 2024
3a4f0ca
Update help tests
Youssef1313 Dec 9, 2024
a249f8f
Fix build
Youssef1313 Dec 9, 2024
6f84743
Fix build errors
Youssef1313 Dec 9, 2024
7b5b396
Fix one more build error
Youssef1313 Dec 9, 2024
8ec5e06
Fix PublicAPI.Unshipped.txt
Youssef1313 Dec 9, 2024
8a23799
Rename capability on MSTest side
Youssef1313 Dec 9, 2024
e0dbaf1
Remove outdated TODO
Youssef1313 Dec 9, 2024
92f3d8d
Rename public method, add test
Youssef1313 Dec 9, 2024
f4732db
Progress
Youssef1313 Dec 9, 2024
ef5bdb9
Doc
Youssef1313 Dec 9, 2024
22d846b
Small progress
Youssef1313 Dec 9, 2024
43d3b21
Fix tests
Youssef1313 Dec 10, 2024
4942254
Remove unused using
Youssef1313 Dec 10, 2024
264eb26
Cleanup
Youssef1313 Dec 10, 2024
f2dfe03
Add comments
Youssef1313 Dec 10, 2024
e7318df
Add test for MSTest
Youssef1313 Dec 10, 2024
f134a13
Address feedback
Youssef1313 Dec 10, 2024
3d35495
Fix build error
Youssef1313 Dec 10, 2024
529c521
Progress
Youssef1313 Dec 10, 2024
7e56af8
Fix test
Youssef1313 Dec 10, 2024
2c1b69a
Fix test
Youssef1313 Dec 10, 2024
612a94c
Fix formatting
Youssef1313 Dec 10, 2024
4f3e7d0
Adjust unit tests per product change
Youssef1313 Dec 10, 2024
17fb777
Try to stabilize test
Youssef1313 Dec 10, 2024
639ba17
Merge branch 'main' into max-failed
Youssef1313 Dec 10, 2024
ffad719
Small fix
Youssef1313 Dec 10, 2024
9e49602
Fix test
Youssef1313 Dec 11, 2024
93edab9
Merge branch 'main' into max-failed
Youssef1313 Dec 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal sealed class PlatformCommandLineProvider : ICommandLineOptionsProvider
public const string TestHostControllerPIDOptionKey = "internal-testhostcontroller-pid";
public const string ExitOnProcessExitOptionKey = "exit-on-process-exit";
public const string ConfigFileOptionKey = "config-file";
public const string MaxFailedTestsOptionKey = "max-failed-tests";

public const string ServerOptionKey = "server";
public const string ClientPortOptionKey = "client-port";
Expand Down Expand Up @@ -61,6 +62,7 @@ internal sealed class PlatformCommandLineProvider : ICommandLineOptionsProvider
new(IgnoreExitCodeOptionKey, PlatformResources.PlatformCommandLineIgnoreExitCodeOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),
new(ExitOnProcessExitOptionKey, PlatformResources.PlatformCommandLineExitOnProcessExitOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),
new(ConfigFileOptionKey, PlatformResources.PlatformCommandLineConfigFileOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),
new(MaxFailedTestsOptionKey, PlatformResources.PlatformCommandLineMaxFailedTestsOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),

// Hidden options
new(HelpOptionQuestionMark, PlatformResources.PlatformCommandLineHelpOptionDescription, ArgumentArity.Zero, true, isBuiltIn: true),
Expand Down Expand Up @@ -141,6 +143,15 @@ public Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption com
}
}

if (commandOption.Name == MaxFailedTestsOptionKey)
{
string arg = arguments[0];
if (!int.TryParse(arg, out int maxFailedTestsResult) || maxFailedTestsResult <= 0)
{
return ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, PlatformResources.MaxFailedTestsMustBePositive, arg));
}
}

// Now validate the minimum expected tests option
return IsMinimumExpectedTestsOptionValidAsync(commandOption, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestHost;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Resources;
using Microsoft.Testing.Platform.Services;

namespace Microsoft.Testing.Platform.Extensions;

internal sealed class AbortForMaxFailedTestsExtension : IDataConsumer
{
private readonly IServiceProvider _serviceProvider;
private readonly int? _maxFailedTests;

private int _failCount;

public AbortForMaxFailedTestsExtension(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
if (serviceProvider.GetCommandLineOptions().TryGetOptionArgumentList(PlatformCommandLineProvider.MaxFailedTestsOptionKey, out string[]? args) &&
int.TryParse(args[0], out int maxFailedTests) &&
maxFailedTests > 0)
{
_maxFailedTests = maxFailedTests;
}
}

public Type[] DataTypesConsumed { get; } = [typeof(TestNodeUpdateMessage)];

/// <inheritdoc />
public string Uid { get; } = nameof(AbortForMaxFailedTestsExtension);

/// <inheritdoc />
public string Version { get; } = AppVersion.DefaultSemVer;

/// <inheritdoc />
public string DisplayName { get; } = nameof(AbortForMaxFailedTestsExtension);

/// <inheritdoc />
public string Description { get; } = PlatformResources.AbortForMaxFailedTestsDescription;

/// <inheritdoc />
public Task<bool> IsEnabledAsync() => Task.FromResult(_maxFailedTests.HasValue);

public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
{
var node = (TestNodeUpdateMessage)value;

// If we are called, the extension is enabled, which means _maxFailedTests.HasValue was true. So null suppression is safe.
int maxFailed = _maxFailedTests!.Value;
if (node.TestNode.Properties.Single<TestNodeStateProperty>() is FailedTestNodeStateProperty)
{
Interlocked.Increment(ref _failCount);
}

if (_failCount > maxFailed)
{
_serviceProvider.GetTestApplicationCancellationTokenSource().Cancel();
}

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ public async Task<ITestHost> BuildAsync(
serviceProvider.TryAddService(proxyOutputDevice);
serviceProvider.TryAddService(proxyOutputDevice.OriginalOutputDevice);

TestHost.AddDataConsumer(serviceProvider => new AbortForMaxFailedTestsExtension(serviceProvider));

// Create the test framework capabilities
ITestFrameworkCapabilities testFrameworkCapabilities = TestFramework.TestFrameworkCapabilitiesFactory(serviceProvider);
if (testFrameworkCapabilities is IAsyncInitializableExtension testFrameworkCapabilitiesAsyncInitializable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,13 @@ Takes one argument as string in the format &lt;value&gt;[h|m|s] where 'value' is
<value>Exception during the cancellation of request id '{0}'</value>
<comment>{0} is the request id</comment>
</data>
<data name="PlatformCommandLineMaxFailedTestsOptionDescription" xml:space="preserve">
<value>Specifies a maximum number of test failures that, when exceeded, will abort the test run.</value>
</data>
<data name="MaxFailedTestsMustBePositive" xml:space="preserve">
<value>The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</value>
</data>
<data name="AbortForMaxFailedTestsDescription" xml:space="preserve">
<value>Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</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="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
<note />
</trans-unit>
<trans-unit id="Aborted">
<source>Aborted</source>
<target state="translated">Přerušeno</target>
Expand Down Expand Up @@ -371,6 +376,11 @@
<target state="translated">Rozhraní ILoggerFactory ještě nebylo sestaveno.</target>
<note />
</trans-unit>
<trans-unit id="MaxFailedTestsMustBePositive">
<source>The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</source>
<target state="new">The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</target>
<note />
</trans-unit>
<trans-unit id="MessageBusNotReady">
<source>The message bus has not been built yet or is no more usable at this stage.</source>
<target state="translated">Sběrnice zpráv ještě nebyla sestavena nebo už není v této fázi použitelná.</target>
Expand Down Expand Up @@ -525,6 +535,11 @@ Dostupné hodnoty jsou Trace, Debug, Information, Warning, Error a Critical.</ta
<target state="translated">Umožňuje zobrazit informace o testovací aplikaci .NET.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMaxFailedTestsOptionDescription">
<source>Specifies a maximum number of test failures that, when exceeded, will abort the test run.</source>
<target state="new">Specifies a maximum number of test failures that, when exceeded, will abort the test run.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMinimumExpectedTestsIncompatibleDiscoverTests">
<source>'--list-tests' and '--minimum-expected-tests' are incompatible options</source>
<target state="translated">--list-tests a --minimum-expected-tests jsou nekompatibilní možnosti.</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="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
<note />
</trans-unit>
<trans-unit id="Aborted">
<source>Aborted</source>
<target state="translated">Abgebrochen</target>
Expand Down Expand Up @@ -371,6 +376,11 @@
<target state="translated">Die ILoggerFactory wurde noch nicht erstellt.</target>
<note />
</trans-unit>
<trans-unit id="MaxFailedTestsMustBePositive">
<source>The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</source>
<target state="new">The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</target>
<note />
</trans-unit>
<trans-unit id="MessageBusNotReady">
<source>The message bus has not been built yet or is no more usable at this stage.</source>
<target state="translated">Der Nachrichtenbus wurde noch nicht erstellt oder kann zu diesem Zeitpunkt nicht mehr verwendet werden.</target>
Expand Down Expand Up @@ -525,6 +535,11 @@ Die verfügbaren Werte sind "Trace", "Debug", "Information", "Warning", "Error"
<target state="translated">Zeigen Sie .NET-Testanwendungsinformationen an.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMaxFailedTestsOptionDescription">
<source>Specifies a maximum number of test failures that, when exceeded, will abort the test run.</source>
<target state="new">Specifies a maximum number of test failures that, when exceeded, will abort the test run.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMinimumExpectedTestsIncompatibleDiscoverTests">
<source>'--list-tests' and '--minimum-expected-tests' are incompatible options</source>
<target state="translated">"--list-tests" und "--minimum-expected-tests" sind inkompatible Optionen.</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="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
<note />
</trans-unit>
<trans-unit id="Aborted">
<source>Aborted</source>
<target state="translated">Anulado</target>
Expand Down Expand Up @@ -371,6 +376,11 @@
<target state="translated">ILoggerFactory aún no se ha compilado.</target>
<note />
</trans-unit>
<trans-unit id="MaxFailedTestsMustBePositive">
<source>The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</source>
<target state="new">The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</target>
<note />
</trans-unit>
<trans-unit id="MessageBusNotReady">
<source>The message bus has not been built yet or is no more usable at this stage.</source>
<target state="translated">El bus de mensajes aún no se ha compilado o ya no se puede usar en esta fase.</target>
Expand Down Expand Up @@ -525,6 +535,11 @@ Los valores disponibles son 'Seguimiento', 'Depurar', 'Información', 'Advertenc
<target state="translated">Muestre la información de la aplicación de prueba de .NET.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMaxFailedTestsOptionDescription">
<source>Specifies a maximum number of test failures that, when exceeded, will abort the test run.</source>
<target state="new">Specifies a maximum number of test failures that, when exceeded, will abort the test run.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMinimumExpectedTestsIncompatibleDiscoverTests">
<source>'--list-tests' and '--minimum-expected-tests' are incompatible options</source>
<target state="translated">“--list-tests” y “--minimum-expected-tests” son opciones incompatibles</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="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
<note />
</trans-unit>
<trans-unit id="Aborted">
<source>Aborted</source>
<target state="translated">Abandonné</target>
Expand Down Expand Up @@ -371,6 +376,11 @@
<target state="translated">ILoggerFactory n’a pas encore été généré.</target>
<note />
</trans-unit>
<trans-unit id="MaxFailedTestsMustBePositive">
<source>The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</source>
<target state="new">The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</target>
<note />
</trans-unit>
<trans-unit id="MessageBusNotReady">
<source>The message bus has not been built yet or is no more usable at this stage.</source>
<target state="translated">Le bus de messages n’a pas encore été généré ou n’est plus utilisable à ce stade.</target>
Expand Down Expand Up @@ -525,6 +535,11 @@ Les valeurs disponibles sont « Trace », « Debug », « Information »,
<target state="translated">Afficher les informations de l’application de test .NET.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMaxFailedTestsOptionDescription">
<source>Specifies a maximum number of test failures that, when exceeded, will abort the test run.</source>
<target state="new">Specifies a maximum number of test failures that, when exceeded, will abort the test run.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMinimumExpectedTestsIncompatibleDiscoverTests">
<source>'--list-tests' and '--minimum-expected-tests' are incompatible options</source>
<target state="translated">« --list-tests » et « --minimum-expected-tests » sont des options incompatibles</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="AbortForMaxFailedTestsDescription">
<source>Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</source>
<target state="new">Extension used to support '--max-failed-tests'. When a given failures threshold is reached, the test run will be aborted.</target>
<note />
</trans-unit>
<trans-unit id="Aborted">
<source>Aborted</source>
<target state="translated">Operazione interrotta</target>
Expand Down Expand Up @@ -371,6 +376,11 @@
<target state="translated">ILoggerFactory non è stato ancora compilato.</target>
<note />
</trans-unit>
<trans-unit id="MaxFailedTestsMustBePositive">
<source>The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</source>
<target state="new">The option '--max-failed-tests' must be a positive integer. The value '{0}' is not valid.</target>
<note />
</trans-unit>
<trans-unit id="MessageBusNotReady">
<source>The message bus has not been built yet or is no more usable at this stage.</source>
<target state="translated">Il bus di messaggi non è stato ancora compilato o non è più utilizzabile in questa fase.</target>
Expand Down Expand Up @@ -525,6 +535,11 @@ I valori disponibili sono 'Trace', 'Debug', 'Information', 'Warning', 'Error' e
<target state="translated">Visualizza le informazioni sull'applicazione di test .NET.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMaxFailedTestsOptionDescription">
<source>Specifies a maximum number of test failures that, when exceeded, will abort the test run.</source>
<target state="new">Specifies a maximum number of test failures that, when exceeded, will abort the test run.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineMinimumExpectedTestsIncompatibleDiscoverTests">
<source>'--list-tests' and '--minimum-expected-tests' are incompatible options</source>
<target state="translated">'--list-tests' e '--minimum-expected-tests' sono opzioni incompatibili</target>
Expand Down
Loading
Loading