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..babc83943a 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_EmptyArgument_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() {