From 451b0977bea8d489457bb077836a6daaa1949b6f Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 1 Apr 2026 12:01:22 +0200 Subject: [PATCH 1/2] Avoid IndexOutOfRangeException in command-line parsing --- .../CommandLine/Parser.cs | 2 +- .../CommandLine/ArgumentArityTests.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Platform/Microsoft.Testing.Platform/CommandLine/Parser.cs b/src/Platform/Microsoft.Testing.Platform/CommandLine/Parser.cs index ac84f54e79..58a06e7c63 100644 --- a/src/Platform/Microsoft.Testing.Platform/CommandLine/Parser.cs +++ b/src/Platform/Microsoft.Testing.Platform/CommandLine/Parser.cs @@ -54,7 +54,7 @@ private static CommandLineParseResult Parse(List args, IEnvironment envi // If it's the first argument and it doesn't start with - then it's the tool name // TODO: This won't work correctly if the first argument provided is a response file that contains the tool name. - if (isFirstRealArgument && currentArg[0] != '-') + if (isFirstRealArgument && currentArg.Length > 0 && currentArg[0] != '-') { toolName = currentArg; isFirstRealArgument = false; diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs index 18d10a3281..88673fa0f9 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs @@ -23,6 +23,28 @@ public sealed class ArgumentArityTests new ExtensionCommandLineProviderMockOptionsWithDifferentArity() ]; + [TestMethod] + public async Task ParseAndValidate_EmptyOption_ShouldNotThrowException() + { + // Arrange + string[] args = [string.Empty]; + CommandLineParseResult parseResult = CommandLineParser.Parse(args, new SystemEnvironment()); + + // Act + ValidationResult result = await CommandLineOptionsValidator.ValidateAsync(parseResult, _systemCommandLineOptionsProviders, + _extensionCommandLineOptionsProviders, new Mock().Object); + + // Assert + Assert.IsFalse(result.IsValid); +#pragma warning disable SA1027 // Use tabs correctly + Assert.AreEqual( + """ + Invalid command line arguments: + - Unexpected argument + """, result.ErrorMessage, StringComparer.Ordinal); +#pragma warning restore SA1027 // Use tabs correctly + } + [TestMethod] public async Task ParseAndValidate_WhenOptionWithArityZeroIsCalledWithOneArgument_ReturnsFalse() { From 32a4cc26bda4bf1a111c9bf064c2d2434e304712 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Wed, 1 Apr 2026 12:19:32 +0200 Subject: [PATCH 2/2] Update test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../CommandLine/ArgumentArityTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs index 88673fa0f9..babc83943a 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/ArgumentArityTests.cs @@ -24,7 +24,7 @@ public sealed class ArgumentArityTests ]; [TestMethod] - public async Task ParseAndValidate_EmptyOption_ShouldNotThrowException() + public async Task ParseAndValidate_EmptyArgument_ShouldNotThrowException() { // Arrange string[] args = [string.Empty];