Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache CommandLineOption from the providers #2680

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -39,7 +39,7 @@ internal async Task<CommandLineHandler> BuildAsync(string[] args, IPlatformOutpu
await async.InitializeAsync();
}

commandLineOptionsProviders.Add(serviceInstance);
commandLineOptionsProviders.Add(new CommandLineOptionsProviderCache(serviceInstance));
}

ICommandLineOptionsProvider[] systemCommandLineOptionsProviders = new[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.Extensions;
using Microsoft.Testing.Platform.Extensions.CommandLine;

namespace Microsoft.Testing.Platform.CommandLine;

internal struct CommandLineOptionsProviderCache(ICommandLineOptionsProvider commandLineOptionsProvider) : ICommandLineOptionsProvider
{
private readonly ICommandLineOptionsProvider _commandLineOptionsProvider = commandLineOptionsProvider;
private CommandLineOption[]? _commandLineOptions;

public readonly string Uid => _commandLineOptionsProvider.Uid;

public readonly string Version => _commandLineOptionsProvider.Version;

public readonly string DisplayName => _commandLineOptionsProvider.DisplayName;

public readonly string Description => _commandLineOptionsProvider.Description;

public IReadOnlyCollection<CommandLineOption> GetCommandLineOptions()
{
_commandLineOptions ??= _commandLineOptionsProvider.GetCommandLineOptions().ToArray();

return _commandLineOptions;
}

public readonly Task<bool> IsEnabledAsync()
=> _commandLineOptionsProvider.IsEnabledAsync();

public readonly Task<ValidationResult> ValidateCommandLineOptionsAsync(ICommandLineOptions commandLineOptions)
=> _commandLineOptionsProvider.ValidateCommandLineOptionsAsync(commandLineOptions);

public readonly Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption commandOption, string[] arguments)
=> _commandLineOptionsProvider.ValidateOptionArgumentsAsync(commandOption, arguments);
}