Skip to content
Merged
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d88d0ce
Initial plan for issue
Copilot May 27, 2025
1443d5e
Optimize performance of CommandLineOptionsValidator
Copilot May 27, 2025
85ffb88
Remove PerformanceSensitiveAttribute usage
Copilot May 27, 2025
39d6db5
Fix code formatting in CommandLineOptionsValidator.cs
Copilot May 27, 2025
78e96f8
Add blank line after conditional block in CommandLineOptionsValidator.cs
Copilot May 27, 2025
975d2d6
Fix whitespace formatting in CommandLineOptionsValidator.cs
Copilot May 27, 2025
86c987c
Replace var with explicit type in foreach loop at line 114
Copilot Jun 1, 2025
2e2ee02
Replace var with explicit types and fix whitespace formatting
Copilot Jun 1, 2025
07a4700
Fix formatting: move string.Format to separate line with proper inden…
Copilot Jun 1, 2025
6c344de
Replace var with explicit types in ValidateOptionsAreNotDuplicated me…
Copilot Jun 1, 2025
3ba2fb0
Update CommandLineOptionsValidator.cs
Youssef1313 Jun 1, 2025
e1369e7
Update CommandLineOptionsValidator.cs
Youssef1313 Jun 1, 2025
780fd3b
Update CommandLineOptionsValidator.cs
Youssef1313 Jun 1, 2025
fe102f2
Update CommandLineOptionsValidator.cs
Youssef1313 Jun 1, 2025
cae2a1b
Update CommandLineOptionsValidator.cs
Youssef1313 Jun 2, 2025
97c8299
Update CommandLineOptionsValidator.cs
Youssef1313 Jun 2, 2025
63ab4f7
Add more tests
Evangelink Feb 22, 2026
3f0ce4e
Merge branch 'main' into copilot/fix-5651
Youssef1313 Mar 31, 2026
1cc37c3
Update CommandLineOptionsValidator.cs
Youssef1313 Apr 1, 2026
a470f0c
Update CommandLineOptionsValidator.cs
Youssef1313 Apr 15, 2026
78500bf
Merge remote-tracking branch 'origin/main' into copilot/fix-5651
Evangelink Apr 20, 2026
7c7db06
Address review comments and fix issues in CommandLineOptionsValidator
Evangelink Apr 20, 2026
d6777dd
Simplify collection initialization (IDE0028)
Evangelink Apr 20, 2026
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
@@ -1,15 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.CommandLine;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Resources;
using Roslyn.Utilities;

Check failure on line 16 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L16

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(16,7): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Roslyn' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L16

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(16,7): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Roslyn' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L16

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(16,7): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Roslyn' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L16

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(16,7): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Roslyn' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L16

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(16,7): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'Roslyn' could not be found (are you missing a using directive or an assembly reference?)
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated

namespace Microsoft.Testing.Platform.CommandLine;

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L20

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(20,2): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
internal static class CommandLineOptionsValidator
{
[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitive' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs#L23

src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionsValidator.cs(23,6): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'PerformanceSensitiveAttribute' could not be found (are you missing a using directive or an assembly reference?)
public static async Task<ValidationResult> ValidateAsync(
CommandLineParseResult commandLineParseResult,
IEnumerable<ICommandLineOptionsProvider> systemCommandLineOptionsProviders,
Expand Down Expand Up @@ -68,6 +79,7 @@
return await ValidateConfigurationAsync(extensionOptionsByProvider.Keys, systemOptionsByProvider.Keys, commandLineOptions);
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static ValidationResult ValidateExtensionOptionsDoNotContainReservedPrefix(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> extensionOptionsByProvider)
{
Expand Down Expand Up @@ -97,60 +109,110 @@
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static ValidationResult ValidateExtensionOptionsDoNotContainReservedOptions(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> extensionOptionsByProvider,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> systemOptionsByProvider)
{
IEnumerable<string> allExtensionOptions = extensionOptionsByProvider.Values.SelectMany(x => x).Select(x => x.Name).Distinct();
IEnumerable<string> allSystemOptions = systemOptionsByProvider.Values.SelectMany(x => x).Select(x => x.Name).Distinct();

IEnumerable<string> invalidReservedOptions = allSystemOptions.Intersect(allExtensionOptions);
if (invalidReservedOptions.Any())
// Create a HashSet of all system option names for faster lookup
HashSet<string> systemOptionNames = new();
foreach (var provider in systemOptionsByProvider)
Comment thread
Evangelink marked this conversation as resolved.
Outdated
{
var stringBuilder = new StringBuilder();
foreach (string reservedOption in invalidReservedOptions)
foreach (var option in provider.Value)
Comment thread
Evangelink marked this conversation as resolved.
Outdated
{
IEnumerable<string> faultyProviderNames = extensionOptionsByProvider.Where(tuple => tuple.Value.Any(x => x.Name == reservedOption)).Select(tuple => tuple.Key.DisplayName);
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionIsReserved, reservedOption, string.Join("', '", faultyProviderNames)));
systemOptionNames.Add(option.Name);
}
}

return ValidationResult.Invalid(stringBuilder.ToTrimmedString());
StringBuilder? stringBuilder = null;
foreach (var provider in extensionOptionsByProvider)
Comment thread
Evangelink marked this conversation as resolved.
Outdated
{
foreach (var option in provider.Value)
{
if (systemOptionNames.Contains(option.Name))
{
stringBuilder ??= new StringBuilder();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture,
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
PlatformResources.CommandLineOptionIsReserved,
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
option.Name,
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
provider.Key.DisplayName));
}
}
}

return ValidationResult.Valid();
return stringBuilder?.Length > 0
? ValidationResult.Invalid(stringBuilder.ToTrimmedString())
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static ValidationResult ValidateOptionsAreNotDuplicated(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> extensionOptionsByProvider)
{
IEnumerable<string> duplications = extensionOptionsByProvider.Values.SelectMany(x => x)
.Select(x => x.Name)
.GroupBy(x => x)
.Where(x => x.Skip(1).Any())
.Select(x => x.Key);

// Use a dictionary to track option names and their providers
Dictionary<string, List<ICommandLineOptionsProvider>> optionNameToProviders = new();

Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
foreach (var kvp in extensionOptionsByProvider)
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
{
var provider = kvp.Key;
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
foreach (var option in kvp.Value)
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
{
string name = option.Name;
if (!optionNameToProviders.TryGetValue(name, out var providers))
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
{
providers = new List<ICommandLineOptionsProvider>();
optionNameToProviders[name] = providers;
}
Comment thread
Youssef1313 marked this conversation as resolved.
providers.Add(provider);
}
}

Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
// Check for duplications
StringBuilder? stringBuilder = null;
foreach (string duplicatedOption in duplications)
foreach (var kvp in optionNameToProviders)
{
IEnumerable<string> faultyProvidersDisplayNames = extensionOptionsByProvider.Where(tuple => tuple.Value.Any(x => x.Name == duplicatedOption)).Select(tuple => tuple.Key.DisplayName);
stringBuilder ??= new();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionIsDeclaredByMultipleProviders, duplicatedOption, string.Join("', '", faultyProvidersDisplayNames)));
if (kvp.Value.Count > 1)
{
string duplicatedOption = kvp.Key;
stringBuilder ??= new();
IEnumerable<string> faultyProvidersDisplayNames = kvp.Value.Select(p => p.DisplayName);
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionIsDeclaredByMultipleProviders, duplicatedOption, string.Join("', '", faultyProvidersDisplayNames)));
}
}

return stringBuilder?.Length > 0
? ValidationResult.Invalid(stringBuilder.ToTrimmedString())
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static ValidationResult ValidateNoUnknownOptions(
CommandLineParseResult parseResult,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> extensionOptionsByProvider,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> systemOptionsByProvider)
{
// Create a HashSet of all valid option names for faster lookup
HashSet<string> validOptionNames = new();
foreach (var provider in extensionOptionsByProvider)
{
foreach (var option in provider.Value)
{
validOptionNames.Add(option.Name);
}
}

Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
foreach (var provider in systemOptionsByProvider)
{
foreach (var option in provider.Value)
{
validOptionNames.Add(option.Name);
}
}

StringBuilder? stringBuilder = null;
foreach (CommandLineParseOption optionRecord in parseResult.Options)
{
if (!extensionOptionsByProvider.Union(systemOptionsByProvider).Any(tuple => tuple.Value.Any(x => x.Name == optionRecord.Name)))
if (!validOptionNames.Contains(optionRecord.Name))
{
stringBuilder ??= new();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineUnknownOption, optionRecord.Name));
Expand All @@ -162,11 +224,12 @@
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static ValidationResult ValidateOptionsArgumentArity(
CommandLineParseResult parseResult,
Dictionary<string, (ICommandLineOptionsProvider Provider, CommandLineOption Option)> providerAndOptionByOptionName)
{
StringBuilder stringBuilder = new();
StringBuilder? stringBuilder = null;
foreach (IGrouping<string, CommandLineParseOption> groupedOptions in parseResult.Options.GroupBy(x => x.Name))
{
// getting the arguments count for an option.
Expand All @@ -181,23 +244,27 @@

if (arity > option.Arity.Max && option.Arity.Max == 0)
{
stringBuilder ??= new();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionExpectsNoArguments, optionName, provider.DisplayName, provider.Uid));
}
else if (arity < option.Arity.Min)
{
stringBuilder ??= new();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionExpectsAtLeastArguments, optionName, provider.DisplayName, provider.Uid, option.Arity.Min));
}
else if (arity > option.Arity.Max)
{
stringBuilder ??= new();
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionExpectsAtMostArguments, optionName, provider.DisplayName, provider.Uid, option.Arity.Max));
}
}

return stringBuilder.Length > 0
return stringBuilder?.Length > 0
? ValidationResult.Invalid(stringBuilder.ToTrimmedString())
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static async Task<ValidationResult> ValidateOptionsArgumentsAsync(
CommandLineParseResult parseResult,
Dictionary<string, (ICommandLineOptionsProvider Provider, CommandLineOption Option)> providerAndOptionByOptionName)
Expand All @@ -221,6 +288,7 @@
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static async Task<ValidationResult> ValidateConfigurationAsync(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>>.KeyCollection extensionsProviders,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>>.KeyCollection systemProviders,
Expand All @@ -234,6 +302,7 @@
: ValidationResult.Valid();
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static async Task<StringBuilder?> ValidateConfigurationAsync(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>>.KeyCollection providers,
ICommandLineOptions commandLineOptions,
Expand All @@ -253,8 +322,24 @@
return stringBuilder;
}

[PerformanceSensitive("https://github.com/microsoft/testfx/issues/5651", AllowGenericEnumeration = false)]
private static string ToTrimmedString(this StringBuilder stringBuilder)
#pragma warning disable RS0030 // Do not use banned APIs
=> stringBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());
#pragma warning restore RS0030 // Do not use banned APIs
{
// Use a more efficient approach to trim without creating unnecessary intermediate strings
string result = stringBuilder.ToString();
int end = result.Length;

Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
// Find the last non-whitespace char
while (end > 0)
{
char c = result[end - 1];
if (c != '\r' && c != '\n')
{
break;
}
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
end--;
}

Comment thread
Youssef1313 marked this conversation as resolved.
Outdated
return end == result.Length ? result : result.Substring(0, end);
}
Comment thread
Evangelink marked this conversation as resolved.
}
Loading