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
  • Loading branch information
Youssef1313 committed Dec 11, 2024
1 parent 87c2172 commit 27b8768
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.OutputDevice;
using Microsoft.Testing.Platform.Extensions.TestHost;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Messages;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.Resources;
using Microsoft.Testing.Platform.Services;

namespace Microsoft.Testing.Platform.Extensions;

internal sealed class AbortForMaxFailedTestsExtension : IDataConsumer
internal sealed class AbortForMaxFailedTestsExtension : IDataConsumer, IOutputDeviceDataProducer
{
private readonly int? _maxFailedTests;
private readonly IGracefulStopTestExecutionCapability? _capability;
private readonly IStopPoliciesService _policiesService;
private readonly IOutputDevice _outputDevice;
private readonly ITestApplicationCancellationTokenSource _testApplicationCancellationTokenSource;
private int _failCount;

public AbortForMaxFailedTestsExtension(
ICommandLineOptions commandLineOptions,
IGracefulStopTestExecutionCapability? capability,
IStopPoliciesService policiesService,
IOutputDevice outputDevice,
ITestApplicationCancellationTokenSource testApplicationCancellationTokenSource)
{
if (commandLineOptions.TryGetOptionArgumentList(MaxFailedTestsCommandLineOptionsProvider.MaxFailedTestsOptionKey, out string[]? args) &&
Expand All @@ -35,6 +39,7 @@ public AbortForMaxFailedTestsExtension(

_capability = capability;
_policiesService = policiesService;
_outputDevice = outputDevice;
_testApplicationCancellationTokenSource = testApplicationCancellationTokenSource;
}

Expand All @@ -53,7 +58,21 @@ public AbortForMaxFailedTestsExtension(
public string Description { get; } = PlatformResources.AbortForMaxFailedTestsDescription;

/// <inheritdoc />
public Task<bool> IsEnabledAsync() => Task.FromResult(_maxFailedTests.HasValue && _capability is not null);
public async Task<bool> IsEnabledAsync()
{
if (!_maxFailedTests.HasValue)
{
return false;
}

if (_capability is null)
{
await _outputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(PlatformResources.AbortForMaxFailedTestsCapabilityNotAvailable));
return false;
}

return true;
}

public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ private async Task<ITestFramework> BuildTestFrameworkAsync(TestFrameworkBuilderD
serviceProvider.GetCommandLineOptions(),
serviceProvider.GetTestFrameworkCapabilities().GetCapability<IGracefulStopTestExecutionCapability>(),
serviceProvider.GetRequiredService<IStopPoliciesService>(),
serviceProvider.GetOutputDevice(),
serviceProvider.GetTestApplicationCancellationTokenSource());

if (await abortForMaxFailedTestsExtension.IsEnabledAsync())
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 support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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 support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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="ru" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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="tr" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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="zh-Hans" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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="zh-Hant" original="../PlatformResources.resx">
<body>
<trans-unit id="AbortForMaxFailedTestsCapabilityNotAvailable">
<source>The current test framework does not support '--maximum-failed-tests'.</source>
<target state="new">The current test framework does not support '--maximum-failed-tests'.</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

0 comments on commit 27b8768

Please sign in to comment.