forked from microsoft/vstest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentProcessorFactory.cs
348 lines (304 loc) · 13.7 KB
/
ArgumentProcessorFactory.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// 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.Diagnostics.Contracts;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.Utilities;
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors;
/// <summary>
/// Used to create the appropriate instance of an argument processor.
/// </summary>
internal class ArgumentProcessorFactory
{
/// <summary>
/// The command starter.
/// </summary>
internal const string CommandStarter = "/";
/// <summary>
/// The xplat command starter.
/// </summary>
internal const string XplatCommandStarter = "-";
/// <summary>
/// Available argument processors.
/// </summary>
private Dictionary<string, IArgumentProcessor>? _commandToProcessorMap;
private Dictionary<string, IArgumentProcessor>? _specialCommandToProcessorMap;
/// Initializes the argument processor factory.
/// </summary>
/// <param name="argumentProcessors">
/// The argument Processors.
/// </param>
/// <param name="featureFlag">
/// The feature flag support.
/// </param>
/// <remarks>
/// This is not public because the static Create method should be used to access the instance.
/// </remarks>
protected ArgumentProcessorFactory(IEnumerable<IArgumentProcessor> argumentProcessors)
{
ValidateArg.NotNull(argumentProcessors, nameof(argumentProcessors));
AllArgumentProcessors = argumentProcessors;
}
/// <summary>
/// Creates ArgumentProcessorFactory.
/// </summary>
/// <param name="featureFlag">
/// The feature flag support.
/// </param>
/// <returns>ArgumentProcessorFactory.</returns>
internal static ArgumentProcessorFactory Create(IFeatureFlag? featureFlag = null)
{
var defaultArgumentProcessor = DefaultArgumentProcessors;
if (!(featureFlag ?? FeatureFlag.Instance).IsSet(FeatureFlag.DISABLE_ARTIFACTS_POSTPROCESSING))
{
defaultArgumentProcessor.Add(new ArtifactProcessingCollectModeProcessor());
defaultArgumentProcessor.Add(new ArtifactProcessingPostProcessModeProcessor());
defaultArgumentProcessor.Add(new TestSessionCorrelationIdProcessor());
}
// Get the ArgumentProcessorFactory
return new ArgumentProcessorFactory(defaultArgumentProcessor);
}
/// <summary>
/// Returns all of the available argument processors.
/// </summary>
public IEnumerable<IArgumentProcessor> AllArgumentProcessors { get; }
/// <summary>
/// Gets a mapping between command and Argument Executor.
/// </summary>
internal Dictionary<string, IArgumentProcessor> CommandToProcessorMap
{
get
{
// Build the mapping if it does not already exist.
if (_commandToProcessorMap == null)
{
BuildCommandMaps();
}
return _commandToProcessorMap;
}
}
/// <summary>
/// Gets a mapping between special commands and their Argument Processors.
/// </summary>
internal Dictionary<string, IArgumentProcessor> SpecialCommandToProcessorMap
{
get
{
// Build the mapping if it does not already exist.
if (_specialCommandToProcessorMap == null)
{
BuildCommandMaps();
}
return _specialCommandToProcessorMap;
}
}
/// <summary>
/// Creates the argument processor associated with the provided command line argument.
/// The Lazy that is returned will initialize the underlying argument processor when it is first accessed.
/// </summary>
/// <param name="argument">Command line argument to create the argument processor for.</param>
/// <returns>The argument processor or null if one was not found.</returns>
public IArgumentProcessor? CreateArgumentProcessor(string argument)
{
ValidateArg.NotNullOrWhiteSpace(argument, nameof(argument));
// Parse the input into its command and argument parts.
var pair = new CommandArgumentPair(argument);
// Find the associated argument processor.
CommandToProcessorMap.TryGetValue(pair.Command, out IArgumentProcessor? argumentProcessor);
// If an argument processor was not found for the command, then consider it as a test source argument.
if (argumentProcessor == null)
{
// Update the command pair since the command is actually the argument in the case of
// a test source.
pair = new CommandArgumentPair(TestSourceArgumentProcessor.CommandName, argument);
argumentProcessor = SpecialCommandToProcessorMap[TestSourceArgumentProcessor.CommandName];
}
if (argumentProcessor != null)
{
argumentProcessor = WrapLazyProcessorToInitializeOnInstantiation(argumentProcessor, pair.Argument);
}
return argumentProcessor;
}
/// <summary>
/// Creates the argument processor associated with the provided command line argument.
/// The Lazy that is returned will initialize the underlying argument processor when it is first accessed.
/// </summary>
/// <param name="command">Command name of the argument processor.</param>
/// <param name="arguments">Command line arguments to create the argument processor for.</param>
/// <returns>The argument processor or null if one was not found.</returns>
public IArgumentProcessor? CreateArgumentProcessor(string command, string[] arguments)
{
if (arguments == null || arguments.Length == 0)
{
throw new ArgumentException("Cannot be null or empty", nameof(arguments));
}
Contract.EndContractBlock();
// Find the associated argument processor.
CommandToProcessorMap.TryGetValue(command, out IArgumentProcessor? argumentProcessor);
if (argumentProcessor != null)
{
argumentProcessor = WrapLazyProcessorToInitializeOnInstantiation(argumentProcessor, arguments);
}
return argumentProcessor;
}
/// <summary>
/// Creates the default action argument processor.
/// The Lazy that is returned will initialize the underlying argument processor when it is first accessed.
/// </summary>
/// <returns>The default action argument processor.</returns>
public IArgumentProcessor CreateDefaultActionArgumentProcessor()
{
var argumentProcessor = SpecialCommandToProcessorMap[RunTestsArgumentProcessor.CommandName];
return WrapLazyProcessorToInitializeOnInstantiation(argumentProcessor);
}
/// <summary>
/// Gets the argument processors that are tagged as special and to be always executed.
/// The Lazy's that are returned will initialize the underlying argument processor when first accessed.
/// </summary>
/// <returns>The argument processors that are tagged as special and to be always executed.</returns>
public IEnumerable<IArgumentProcessor> GetArgumentProcessorsToAlwaysExecute()
{
return SpecialCommandToProcessorMap.Values
.Where(lazyProcessor => lazyProcessor.Metadata.Value.IsSpecialCommand && lazyProcessor.Metadata.Value.AlwaysExecute);
}
private static IList<IArgumentProcessor> DefaultArgumentProcessors => new List<IArgumentProcessor> {
new HelpArgumentProcessor(),
new TestSourceArgumentProcessor(),
new ListTestsArgumentProcessor(),
new RunTestsArgumentProcessor(),
new RunSpecificTestsArgumentProcessor(),
new TestAdapterPathArgumentProcessor(),
new TestAdapterLoadingStrategyArgumentProcessor(),
new TestCaseFilterArgumentProcessor(),
new ParentProcessIdArgumentProcessor(),
new PortArgumentProcessor(),
new RunSettingsArgumentProcessor(),
new PlatformArgumentProcessor(),
new FrameworkArgumentProcessor(),
new EnableLoggerArgumentProcessor(),
new ParallelArgumentProcessor(),
new EnableDiagArgumentProcessor(),
new CliRunSettingsArgumentProcessor(),
new ResultsDirectoryArgumentProcessor(),
new InIsolationArgumentProcessor(),
new CollectArgumentProcessor(),
new EnableCodeCoverageArgumentProcessor(),
new DisableAutoFakesArgumentProcessor(),
new ResponseFileArgumentProcessor(),
new EnableBlameArgumentProcessor(),
new AeDebuggerArgumentProcessor(),
new UseVsixExtensionsArgumentProcessor(),
new ListDiscoverersArgumentProcessor(),
new ListExecutorsArgumentProcessor(),
new ListLoggersArgumentProcessor(),
new ListSettingsProvidersArgumentProcessor(),
new ListFullyQualifiedTestsArgumentProcessor(),
new ListTestsTargetPathArgumentProcessor(),
new ShowDeprecateDotnetVStestMessageArgumentProcessor(),
new EnvironmentArgumentProcessor()
};
/// <summary>
/// Builds the command to processor map and special command to processor map.
/// </summary>
[MemberNotNull(nameof(_commandToProcessorMap), nameof(_specialCommandToProcessorMap))]
private void BuildCommandMaps()
{
_commandToProcessorMap = new Dictionary<string, IArgumentProcessor>(StringComparer.OrdinalIgnoreCase);
_specialCommandToProcessorMap = new Dictionary<string, IArgumentProcessor>(StringComparer.OrdinalIgnoreCase);
foreach (IArgumentProcessor argumentProcessor in AllArgumentProcessors)
{
// Add the command to the appropriate dictionary.
var processorsMap = argumentProcessor.Metadata.Value.IsSpecialCommand
? _specialCommandToProcessorMap
: _commandToProcessorMap;
string commandName = argumentProcessor.Metadata.Value.CommandName;
processorsMap.Add(commandName, argumentProcessor);
// Add xplat name for the command name
commandName = string.Concat("--", commandName.Remove(0, 1));
processorsMap.Add(commandName, argumentProcessor);
if (!argumentProcessor.Metadata.Value.ShortCommandName.IsNullOrEmpty())
{
string shortCommandName = argumentProcessor.Metadata.Value.ShortCommandName;
processorsMap.Add(shortCommandName, argumentProcessor);
// Add xplat short name for the command name
shortCommandName = shortCommandName.Replace('/', '-');
processorsMap.Add(shortCommandName, argumentProcessor);
}
}
}
/// <summary>
/// Decorates a lazy argument processor so that the real processor is initialized when the lazy value is obtained.
/// </summary>
/// <param name="processor">The lazy processor.</param>
/// <param name="initArg">The argument with which the real processor should be initialized.</param>
/// <returns>The decorated lazy processor.</returns>
public static IArgumentProcessor WrapLazyProcessorToInitializeOnInstantiation(IArgumentProcessor processor, string? initArg = null)
{
var processorExecutor = processor.Executor;
var lazyArgumentProcessor = new Lazy<IArgumentExecutor>(() =>
{
IArgumentExecutor? instance = null;
try
{
instance = processorExecutor!.Value;
}
catch (Exception e)
{
EqtTrace.Error("ArgumentProcessorFactory.WrapLazyProcessorToInitializeOnInstantiation: Exception creating argument processor: {0}", e);
throw;
}
try
{
instance.Initialize(initArg);
}
catch (Exception e)
{
EqtTrace.Error("ArgumentProcessorFactory.WrapLazyProcessorToInitializeOnInstantiation: Exception initializing argument processor: {0}", e);
throw;
}
return instance;
}, System.Threading.LazyThreadSafetyMode.PublicationOnly);
processor.Executor = lazyArgumentProcessor;
return processor;
}
/// <summary>
/// Decorates a lazy argument processor so that the real processor is initialized when the lazy value is obtained.
/// </summary>
/// <param name="processor">The lazy processor.</param>
/// <param name="initArg">The argument with which the real processor should be initialized.</param>
/// <returns>The decorated lazy processor.</returns>
private static IArgumentProcessor WrapLazyProcessorToInitializeOnInstantiation(
IArgumentProcessor processor,
string[] initArgs)
{
var processorExecutor = processor.Executor;
var lazyArgumentProcessor = new Lazy<IArgumentExecutor>(() =>
{
IArgumentsExecutor? instance = null;
try
{
instance = (IArgumentsExecutor)processorExecutor!.Value;
}
catch (Exception e)
{
EqtTrace.Error("ArgumentProcessorFactory.WrapLazyProcessorToInitializeOnInstantiation: Exception creating argument processor: {0}", e);
throw;
}
try
{
instance.Initialize(initArgs);
}
catch (Exception e)
{
EqtTrace.Error("ArgumentProcessorFactory.WrapLazyProcessorToInitializeOnInstantiation: Exception initializing argument processor: {0}", e);
throw;
}
return instance;
}, System.Threading.LazyThreadSafetyMode.PublicationOnly);
processor.Executor = lazyArgumentProcessor;
return processor;
}
}