Repository navigation
Expand file tree
/
Copy pathToolsTestHost.cs
More file actions
190 lines (160 loc) · 8.01 KB
/
Copy pathToolsTestHost.cs
File metadata and controls
190 lines (160 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.CommandLine;
using Microsoft.Testing.Platform.Extensions.OutputDevice;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.Services;
using Microsoft.Testing.Platform.Tools;
namespace Microsoft.Testing.Platform.Hosts;
internal sealed class ToolsHost(
IReadOnlyList<ITool> toolsInformation,
ServiceProvider serviceProvider,
CommandLineHandler commandLineHandler,
IOutputDevice outputDevice) : IHost, IOutputDeviceDataProducer
{
private readonly IReadOnlyList<ITool> _toolsInformation = toolsInformation;
private readonly ServiceProvider _serviceProvider = serviceProvider;
private readonly CommandLineHandler _commandLineHandler = commandLineHandler;
private readonly IOutputDevice _outputDevice = outputDevice;
/// <inheritdoc />
public string Uid => nameof(ToolsHost);
/// <inheritdoc />
public string Version => PlatformVersion.Version;
/// <inheritdoc />
public string DisplayName => string.Empty;
/// <inheritdoc />
public string Description => string.Empty;
/// <inheritdoc />
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
public async Task<int> RunAsync()
{
CancellationToken cancellationToken = _serviceProvider.GetTestApplicationCancellationTokenSource().CancellationToken;
IConsole console = _serviceProvider.GetConsole();
if (_commandLineHandler.ParseResult.ToolName is null)
{
throw new InvalidOperationException("Tool name is null.");
}
string toolNameToRun = _commandLineHandler.ParseResult.ToolName;
// It is unclear whether the override should be applied or simply not supported for Tools.
// Reserved tool names may also need to be verified.
_toolsInformation.GroupBy(x => x.Name).Where(x => x.Count() > 1).ToList()
.ForEach(x => throw new InvalidOperationException($"Tool '{x.Key}' is registered more than once."));
foreach (ITool tool in _toolsInformation)
{
if (tool.Name == toolNameToRun)
{
if (UnknownOptions(out string? unknownOptionsError, tool))
{
await _outputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(unknownOptionsError), cancellationToken).ConfigureAwait(false);
console.WriteLine();
return (int)ExitCode.InvalidCommandLine;
}
if (ExtensionArgumentArityAreInvalid(out string? arityErrors, tool))
{
await _outputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(arityErrors), cancellationToken).ConfigureAwait(false);
return (int)ExitCode.InvalidCommandLine;
}
ValidationResult optionsArgumentsValidationResult = await ValidateOptionsArgumentsAsync(tool).ConfigureAwait(false);
if (!optionsArgumentsValidationResult.IsValid)
{
await _outputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData(optionsArgumentsValidationResult.ErrorMessage), cancellationToken).ConfigureAwait(false);
return (int)ExitCode.InvalidCommandLine;
}
return await tool.RunAsync(cancellationToken).ConfigureAwait(false);
}
}
await _outputDevice.DisplayAsync(this, new ErrorMessageOutputDeviceData($"Tool '{toolNameToRun}' not found in the list of registered tools."), cancellationToken).ConfigureAwait(false);
await _commandLineHandler.PrintHelpAsync(_outputDevice, null, cancellationToken).ConfigureAwait(false);
return (int)ExitCode.InvalidCommandLine;
}
private bool UnknownOptions([NotNullWhen(true)] out string? error, ITool tool)
{
error = null;
// This is unexpected
if (_commandLineHandler.ParseResult is null)
{
return false;
}
StringBuilder stringBuilder = new();
foreach (CommandLineParseOption optionRecord in _commandLineHandler.ParseResult.Options)
{
if (!GetAllCommandLineOptionsProviderByOptionName(optionRecord.Name).Any())
{
stringBuilder.AppendLine(
CultureInfo.InvariantCulture,
$"Unknown option '{optionRecord.Name}' for tool '{tool.DisplayName}'.");
}
}
if (stringBuilder.Length > 0)
{
error = stringBuilder.ToString();
return true;
}
return false;
}
private bool ExtensionArgumentArityAreInvalid([NotNullWhen(true)] out string? error, ITool tool)
{
error = null;
StringBuilder stringBuilder = new();
foreach (IGrouping<string, CommandLineParseOption> optionRecord in _commandLineHandler.ParseResult.Options.GroupBy(x => x.Name))
{
string optionName = optionRecord.Key;
int arity = optionRecord.Count();
ICommandLineOptionsProvider extension = GetAllCommandLineOptionsProviderByOptionName(optionName).Single();
CommandLineOption option = extension.GetCommandLineOptions().Single(x => x.Name == optionName);
if (arity < option.Arity.Min || arity > option.Arity.Max)
{
stringBuilder.AppendLine(
CultureInfo.InvariantCulture,
$"The option '--{optionName}' can be specified between {option.Arity.Min} and {option.Arity.Max} times for tool '{tool.DisplayName}'.");
}
}
foreach (ICommandLineOptionsProvider extension in _commandLineHandler.ExtensionsCommandLineOptionsProviders)
{
foreach (CommandLineOption option in extension.GetCommandLineOptions())
{
if (_commandLineHandler.ParseResult.Options.Count(x => x.Name == option.Name) < option.Arity.Min)
{
stringBuilder.AppendLine(
CultureInfo.InvariantCulture,
$"The option '--{option.Name}' must be specified at least {option.Arity.Min} times for tool '{extension.DisplayName}'.");
}
}
}
if (stringBuilder.Length > 0)
{
error = stringBuilder.ToString();
return true;
}
return false;
}
private async Task<ValidationResult> ValidateOptionsArgumentsAsync(ITool tool)
{
StringBuilder stringBuilder = new();
foreach (CommandLineParseOption optionRecord in _commandLineHandler.ParseResult.Options)
{
ICommandLineOptionsProvider extension = GetAllCommandLineOptionsProviderByOptionName(optionRecord.Name).Single();
ValidationResult result = await extension.ValidateOptionArgumentsAsync(extension.GetCommandLineOptions().Single(x => x.Name == optionRecord.Name), optionRecord.Arguments).ConfigureAwait(false);
if (!result.IsValid)
{
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"Invalid arguments for option '--{optionRecord.Name}': {result.ErrorMessage}, tool {tool.DisplayName}");
}
}
return stringBuilder.Length > 0
? ValidationResult.Invalid(stringBuilder.ToString())
: ValidationResult.Valid();
}
private IEnumerable<ICommandLineOptionsProvider> GetAllCommandLineOptionsProviderByOptionName(string optionName)
{
foreach (ICommandLineOptionsProvider commandLineOptionsProvider in _commandLineHandler.ExtensionsCommandLineOptionsProviders)
{
if (commandLineOptionsProvider.GetCommandLineOptions().Any(option => option.Name == optionName))
{
yield return commandLineOptionsProvider;
}
}
}
}