diff --git a/src/Platform/Microsoft.Testing.Platform/CommandLine/PlatformCommandLineProvider.cs b/src/Platform/Microsoft.Testing.Platform/CommandLine/PlatformCommandLineProvider.cs index ddfaab74d2d..3c2e68ef0bd 100644 --- a/src/Platform/Microsoft.Testing.Platform/CommandLine/PlatformCommandLineProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/CommandLine/PlatformCommandLineProvider.cs @@ -123,14 +123,10 @@ public Task ValidateOptionArgumentsAsync(CommandLineOption com return ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, PlatformResources.PlatformCommandLineExitOnProcessExitSingleArgument, ExitOnProcessExitOptionKey)); } - if (commandOption.Name == TimeoutOptionKey) + if (commandOption.Name == TimeoutOptionKey + && !TryParseTimeoutArgument(arguments[0], out _)) { - string arg = arguments[0]; - int size = arg.Length; - if ((char.ToLowerInvariant(arg[size - 1]) != 'h' && char.ToLowerInvariant(arg[size - 1]) != 'm' && char.ToLowerInvariant(arg[size - 1]) != 's') || !float.TryParse(arg[..(size - 1)], NumberStyles.Float, CultureInfo.InvariantCulture, out float _)) - { - return ValidationResult.InvalidTask(PlatformResources.PlatformCommandLineTimeoutArgumentErrorMessage); - } + return ValidationResult.InvalidTask(PlatformResources.PlatformCommandLineTimeoutArgumentErrorMessage); } if (commandOption.Name == ConfigFileOptionKey) @@ -179,6 +175,48 @@ private static Task IsMinimumExpectedTestsOptionValidAsync(Com ? ValidationResult.InvalidTask(PlatformResources.PlatformCommandLineMinimumExpectedTestsOptionSingleArgument) : ValidationResult.ValidTask; + internal static bool TryParseTimeoutArgument(string arg, out TimeSpan timeout) + { + timeout = TimeSpan.Zero; + + // We need at least one digit plus a unit suffix (e.g. "1s"). + if (arg is null || arg.Length < 2) + { + return false; + } + + char unit = char.ToLowerInvariant(arg[arg.Length - 1]); + if (unit is not 'h' and not 'm' and not 's') + { + return false; + } + + if (!float.TryParse(arg[..(arg.Length - 1)], NumberStyles.Float, CultureInfo.InvariantCulture, out float value) + || float.IsNaN(value) + || float.IsInfinity(value) + || value < 0) + { + return false; + } + + try + { + timeout = unit switch + { + 'h' => TimeSpan.FromHours(value), + 'm' => TimeSpan.FromMinutes(value), + 's' => TimeSpan.FromSeconds(value), + _ => throw ApplicationStateGuard.Unreachable(), + }; + } + catch (OverflowException) + { + return false; + } + + return true; + } + public Task ValidateCommandLineOptionsAsync(ICommandLineOptions commandLineOptions) { if (!commandLineOptions.IsOptionSet(DiagnosticOptionKey)) diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.CommonServices.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.CommonServices.cs index 35a796577d2..b17a16627dc 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.CommonServices.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.CommonServices.cs @@ -197,21 +197,11 @@ private async Task SetupCommonServicesAsync( if (commandLineOptions.IsOptionSet(PlatformCommandLineProvider.TimeoutOptionKey) && commandLineOptions.TryGetOptionArgumentList(PlatformCommandLineProvider.TimeoutOptionKey, out string[]? args)) { - string arg = args[0]; - int size = arg.Length; - if (!float.TryParse(arg[..(size - 1)], NumberStyles.Float, CultureInfo.InvariantCulture, out float value)) + if (!PlatformCommandLineProvider.TryParseTimeoutArgument(args[0], out TimeSpan timeout)) { throw ApplicationStateGuard.Unreachable(); } - TimeSpan timeout = char.ToLowerInvariant(arg[size - 1]) switch - { - 'h' => TimeSpan.FromHours(value), - 'm' => TimeSpan.FromMinutes(value), - 's' => TimeSpan.FromSeconds(value), - _ => throw ApplicationStateGuard.Unreachable(), - }; - context.TestApplicationCancellationTokenSource.CancelAfter(timeout); } diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx b/src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx index 475c56ea180..33265d55c92 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx +++ b/src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx @@ -623,7 +623,7 @@ Valid values are 'All', 'Failed', 'None'. Default is 'All'. --show-stdout and --show-stderr expect a single parameter with value 'All', 'Failed', or 'None'. - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration A global test execution timeout. diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf index dc67ce0c918..dc2c3456ade 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf @@ -654,8 +654,8 @@ Výchozí hodnota je TestResults v adresáři, který obsahuje testovací aplika - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - Možnost timeout by měla mít jeden argument jako řetězec ve formátu <value>[h|m|s], kde value je hodnota datového typu float. + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + Možnost timeout by měla mít jeden argument jako řetězec ve formátu <value>[h|m|s], kde value je hodnota datového typu float. diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf index ba194e66e2a..bb05487a4a7 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf @@ -654,8 +654,8 @@ Der Standardwert ist "TestResults" im Verzeichnis, das die Testanwendung enthäl - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - Die Option "timeout" muss ein Argument als Zeichenfolge im Format <value>[h|m|s] aufweisen, wobei "value" auf "float" festgelegt ist. + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + Die Option "timeout" muss ein Argument als Zeichenfolge im Format <value>[h|m|s] aufweisen, wobei "value" auf "float" festgelegt ist. diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf index bd4c3c92146..d3b089e34e4 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf @@ -654,8 +654,8 @@ El valor predeterminado es TestResults en el directorio que contiene la aplicaci - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - La opción 'timeout' debe tener un argumento como cadena con el formato <value>[h|m|s] donde 'value' es float + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + La opción 'timeout' debe tener un argumento como cadena con el formato <value>[h|m|s] donde 'value' es float diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf index b43db09ffa2..62613162aea 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf @@ -654,8 +654,8 @@ La valeur par défaut est TestResults dans le répertoire qui contient l’appli - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - L’option « timeout » doit avoir un argument sous forme de chaîne au format <value>[h|m|s] où « value » est float + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + L’option « timeout » doit avoir un argument sous forme de chaîne au format <value>[h|m|s] où « value » est float diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf index bdfe056496d..aa8507e0215 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf @@ -654,8 +654,8 @@ L’impostazione predefinita è TestResults nella directory che contiene l'appli - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - L'opzione 'timeout' deve contenere un argomento come stringa nel formato <value>[h|m|s] dove 'value' è float + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + L'opzione 'timeout' deve contenere un argomento come stringa nel formato <value>[h|m|s] dove 'value' è float diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf index 97edfe78785..2ea47b47135 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf @@ -655,8 +655,8 @@ The default is TestResults in the directory that contains the test application.< - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - 'timeout' オプションには、1 つの引数を文字列として <value>[h|m|s] の形式で指定する必要があります。この場合、'value' は float です + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + 'timeout' オプションには、1 つの引数を文字列として <value>[h|m|s] の形式で指定する必要があります。この場合、'value' は float です diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf index 3d791db0293..a8d7073019e 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf @@ -654,8 +654,8 @@ The default is TestResults in the directory that contains the test application.< - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - 'timeout' 옵션에는 'value'가 float인 <value>[h|m|s] 형식의 문자열로 인수가 하나 있어야 합니다. + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + 'timeout' 옵션에는 'value'가 float인 <value>[h|m|s] 형식의 문자열로 인수가 하나 있어야 합니다. diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf index 70985a304e3..e90641a1b4d 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf @@ -654,8 +654,8 @@ Wartość domyślna to TestResults w katalogu zawierającym aplikację testową. - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - Opcja „timeout” powinna mieć jeden argument jako ciąg w formacie <value>[h|m|s], gdzie element „value” ma wartość zmiennoprzecinkową + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + Opcja „timeout” powinna mieć jeden argument jako ciąg w formacie <value>[h|m|s], gdzie element „value” ma wartość zmiennoprzecinkową diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf index aa46d0ef8da..d4389c7dc04 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf @@ -654,8 +654,8 @@ O padrão é TestResults no diretório que contém o aplicativo de teste. - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - A opção 'timeout' deve ter um argumento como cadeia de caracteres no formato <value>[h|m|s] onde 'value' é float + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + A opção 'timeout' deve ter um argumento como cadeia de caracteres no formato <value>[h|m|s] onde 'value' é float diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ru.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ru.xlf index 4f5c785313c..0abf840d111 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ru.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ru.xlf @@ -654,8 +654,8 @@ The default is TestResults in the directory that contains the test application.< - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - Параметр "timeout" должен иметь один аргумент в виде строки в формате <value>[h|m|s], где "value" — число с плавающей точкой + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + Параметр "timeout" должен иметь один аргумент в виде строки в формате <value>[h|m|s], где "value" — число с плавающей точкой diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.tr.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.tr.xlf index 906259438c5..4e771ba79a3 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.tr.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.tr.xlf @@ -654,8 +654,8 @@ Varsayılan değer, test uygulamasını içeren dizindeki TestResults'dır. - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - 'timeout' seçeneği, 'value' değerinin kayan olduğu <value>[h|m|s] biçiminde dize olarak bir bağımsız değişkene sahip olmalıdır + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + 'timeout' seçeneği, 'value' değerinin kayan olduğu <value>[h|m|s] biçiminde dize olarak bir bağımsız değişkene sahip olmalıdır diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hans.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hans.xlf index 41afc5ebc9c..41c25dc363f 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hans.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hans.xlf @@ -654,8 +654,8 @@ The default is TestResults in the directory that contains the test application.< - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - "timeout" 选项应有一个字符串形式的参数,格式为 <value>[h|m|s],其中 "value" 为浮点型 + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + "timeout" 选项应有一个字符串形式的参数,格式为 <value>[h|m|s],其中 "value" 为浮点型 diff --git a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hant.xlf b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hant.xlf index b179a83eaba..aa8ce1eb1ff 100644 --- a/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hant.xlf +++ b/src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hant.xlf @@ -654,8 +654,8 @@ The default is TestResults in the directory that contains the test application.< - 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is float - 'timeout' 選項應該有一個引數作為字串,格式為 <value>[h|m|s],其中 'value' 為 float + 'timeout' option should have one argument as string in the format <value>[h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration + 'timeout' 選項應該有一個引數作為字串,格式為 <value>[h|m|s],其中 'value' 為 float diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TimeoutTests.cs b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TimeoutTests.cs index 5f59c7e7dbf..df34df2327f 100644 --- a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TimeoutTests.cs +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TimeoutTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// 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.Acceptance.IntegrationTests; @@ -18,7 +18,7 @@ public async Task TimeoutWithInvalidArg_WithoutLetterSuffix_OutputInvalidMessage TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout 5", cancellationToken: TestContext.CancellationToken); testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); - testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is float"); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); } [TestMethod] @@ -29,7 +29,7 @@ public async Task TimeoutWithInvalidArg_WithInvalidLetterSuffix_OutputInvalidMes TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout 5y", cancellationToken: TestContext.CancellationToken); testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); - testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is float"); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); } [TestMethod] @@ -40,7 +40,7 @@ public async Task TimeoutWithInvalidArg_WithInvalidFormat_OutputInvalidMessage(s TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout 5h6m", cancellationToken: TestContext.CancellationToken); testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); - testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is float"); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); } [TestMethod] diff --git a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TimeoutTests.cs b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TimeoutTests.cs index 5bc3d61ad01..8321283851c 100644 --- a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TimeoutTests.cs +++ b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/TimeoutTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests; @@ -14,7 +14,7 @@ public async Task TimeoutWithInvalidArg_WithoutLetterSuffix_OutputInvalidMessage TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout 5", cancellationToken: TestContext.CancellationToken); testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); - testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is float"); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); } [DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))] @@ -25,7 +25,7 @@ public async Task TimeoutWithInvalidArg_WithInvalidLetterSuffix_OutputInvalidMes TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout 5y", cancellationToken: TestContext.CancellationToken); testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); - testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is float"); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); } [DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))] @@ -36,7 +36,19 @@ public async Task TimeoutWithInvalidArg_WithInvalidFormat_OutputInvalidMessage(s TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout 5h6m", cancellationToken: TestContext.CancellationToken); testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); - testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is float"); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); + } + + [DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))] + [TestMethod] + public async Task TimeoutWithInvalidArg_WithNegativeValue_OutputInvalidMessage(string tfm) + { + var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.NoExtensionTargetAssetPath, TestAssetFixture.AssetName, tfm); + // Use the '=' delimiter so the negative value is not parsed as a short option. + TestHostResult testHostResult = await testHost.ExecuteAsync("--timeout=-1s", cancellationToken: TestContext.CancellationToken); + + testHostResult.AssertExitCodeIs(ExitCode.InvalidCommandLine); + testHostResult.AssertOutputContains("'timeout' option should have one argument as string in the format [h|m|s] where 'value' is a finite, non-negative number that does not exceed the maximum supported duration"); } [DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))] diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/PlatformCommandLineProviderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/PlatformCommandLineProviderTests.cs index e43463a544e..77627040905 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/PlatformCommandLineProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/PlatformCommandLineProviderTests.cs @@ -283,6 +283,8 @@ public async Task IsValid_If_Timeout_Has_CorrectFormat_InvariantCulture(string t [DataRow("invalid")] [DataRow("1.5")] // Missing unit [DataRow("abc.5s")] + [DataRow("")] // Empty value + [DataRow("s")] // Missing numeric value public async Task IsInvalid_If_Timeout_Has_IncorrectFormat(string timeout) { var provider = new PlatformCommandLineProvider(); @@ -293,6 +295,23 @@ public async Task IsInvalid_If_Timeout_Has_IncorrectFormat(string timeout) Assert.AreEqual(PlatformResources.PlatformCommandLineTimeoutArgumentErrorMessage, validateOptionsResult.ErrorMessage); } + [TestMethod] + [DataRow("-1s")] // Negative + [DataRow("-0.5m")] // Negative fractional + [DataRow("NaNs")] // Not a number + [DataRow("Infinitys")] // Positive infinity + [DataRow("-Infinitys")] // Negative infinity + [DataRow("1e30h")] // Overflows TimeSpan + public async Task IsInvalid_If_Timeout_Has_NonFiniteOrOutOfRangeValue(string timeout) + { + var provider = new PlatformCommandLineProvider(); + CommandLineOption option = provider.GetCommandLineOptions().First(x => x.Name == PlatformCommandLineProvider.TimeoutOptionKey); + + ValidationResult validateOptionsResult = await provider.ValidateOptionArgumentsAsync(option, [timeout]); + Assert.IsFalse(validateOptionsResult.IsValid); + Assert.AreEqual(PlatformResources.PlatformCommandLineTimeoutArgumentErrorMessage, validateOptionsResult.ErrorMessage); + } + [TestMethod] public async Task Timeout_Parsing_Uses_InvariantCulture_NotCurrentCulture() {