forked from microsoft/vstest
/
RunSpecificTestsArgumentProcessor.cs
393 lines (323 loc) · 15.6 KB
/
RunSpecificTestsArgumentProcessor.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal;
using Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
internal class RunSpecificTestsArgumentProcessor : IArgumentProcessor
{
public const string CommandName = "/Tests";
private Lazy<IArgumentProcessorCapabilities> metadata;
private Lazy<IArgumentExecutor> executor;
public Lazy<IArgumentProcessorCapabilities> Metadata
{
get
{
if (this.metadata == null)
{
this.metadata = new Lazy<IArgumentProcessorCapabilities>(() => new RunSpecificTestsArgumentProcessorCapabilities());
}
return this.metadata;
}
}
public Lazy<IArgumentExecutor> Executor
{
get
{
if (this.executor == null)
{
this.executor = new Lazy<IArgumentExecutor>(() =>
new RunSpecificTestsArgumentExecutor(
CommandLineOptions.Instance,
RunSettingsManager.Instance,
TestRequestManager.Instance,
ConsoleOutput.Instance));
}
return this.executor;
}
set
{
this.executor = value;
}
}
}
internal class RunSpecificTestsArgumentProcessorCapabilities : BaseArgumentProcessorCapabilities
{
public override string CommandName => RunSpecificTestsArgumentProcessor.CommandName;
public override bool IsAction => true;
public override bool AllowMultiple => false;
public override string HelpContentResourceName => CommandLineResources.RunSpecificTestsHelp;
public override HelpContentPriority HelpPriority => HelpContentPriority.RunSpecificTestsArgumentProcessorHelpPriority;
public override ArgumentProcessorPriority Priority => ArgumentProcessorPriority.Normal;
}
internal class RunSpecificTestsArgumentExecutor : IArgumentExecutor
{
public const char SplitDelimiter = ',';
public const char EscapeDelimiter = '\\';
#region Fields
/// <summary>
/// Used for getting sources.
/// </summary>
private CommandLineOptions commandLineOptions;
/// <summary>
/// The instance of testPlatforms
/// </summary>
private ITestRequestManager testRequestManager;
/// <summary>
/// Used for sending output.
/// </summary>
internal IOutput output;
/// <summary>
/// RunSettingsManager to get currently active run settings.
/// </summary>
private IRunSettingsProvider runSettingsManager;
/// <summary>
/// Given Collection of strings for filtering test cases
/// </summary>
private Collection<string> selectedTestNames;
/// <summary>
/// Used for tracking the total no. of tests discovered from the given sources.
/// </summary>
private long discoveredTestCount = 0;
/// <summary>
/// Collection of test cases that match atleast one of the given search strings
/// </summary>
private Collection<TestCase> selectedTestCases = new Collection<TestCase>();
/// <summary>
/// Effective run settings applicable to test run after inferring the multi-targeting settings.
/// </summary>
private string effectiveRunSettings = null;
/// <summary>
/// List of filters that have not yet been discovered
/// </summary>
HashSet<string> undiscoveredFilters = new HashSet<string>();
/// <summary>
/// Registers for discovery events during discovery
/// </summary>
private ITestDiscoveryEventsRegistrar discoveryEventsRegistrar;
/// <summary>
/// Registers and Unregisters for test run events before and after test run
/// </summary>
private ITestRunEventsRegistrar testRunEventsRegistrar;
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public RunSpecificTestsArgumentExecutor(
CommandLineOptions options,
IRunSettingsProvider runSettingsProvider,
ITestRequestManager testRequestManager,
IOutput output)
{
Contract.Requires(options != null);
Contract.Requires(testRequestManager != null);
this.commandLineOptions = options;
this.testRequestManager = testRequestManager;
this.runSettingsManager = runSettingsProvider;
this.output = output;
this.discoveryEventsRegistrar = new DiscoveryEventsRegistrar(this.discoveryRequest_OnDiscoveredTests);
this.testRunEventsRegistrar = new TestRunRequestEventsRegistrar(this.output, this.commandLineOptions);
}
#endregion
#region IArgumentProcessor
/// <summary>
/// Splits given the search strings and adds to selectTestNamesCollection.
/// </summary>
/// <param name="argument"></param>
public void Initialize(string argument)
{
if (!string.IsNullOrWhiteSpace(argument))
{
this.selectedTestNames = new Collection<string>(
argument.Tokenize(SplitDelimiter, EscapeDelimiter)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(s => s.Trim()).ToList());
}
if (this.selectedTestNames == null || this.selectedTestNames.Count <= 0)
{
throw new CommandLineException(CommandLineResources.SpecificTestsRequired);
}
// by default all filters are not discovered on launch
this.undiscoveredFilters = new HashSet<string>(this.selectedTestNames);
}
/// <summary>
/// Execute specific tests that match any of the given strings.
/// </summary>
/// <returns></returns>
public ArgumentProcessorResult Execute()
{
Contract.Assert(this.output != null);
Contract.Assert(this.commandLineOptions != null);
Contract.Assert(this.testRequestManager != null);
Contract.Assert(!string.IsNullOrWhiteSpace(this.runSettingsManager.ActiveRunSettings.SettingsXml));
if (!this.commandLineOptions.Sources.Any())
{
throw new CommandLineException(string.Format(CultureInfo.CurrentUICulture, CommandLineResources.MissingTestSourceFile));
}
if (!string.IsNullOrWhiteSpace(this.commandLineOptions.TestCaseFilterValue))
{
throw new CommandLineException(string.Format(CultureInfo.CurrentUICulture, CommandLineResources.InvalidTestCaseFilterValueForSpecificTests));
}
this.effectiveRunSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml;
// Discover tests from sources and filter on every discovery reported.
this.DiscoverTestsAndSelectSpecified(this.commandLineOptions.Sources);
// Now that tests are discovered and filtered, we run only those selected tests.
this.ExecuteSelectedTests();
return ArgumentProcessorResult.Success;
}
#endregion
#region Private Methods
/// <summary>
/// Discovers tests from the given sources and selects only specified tests.
/// </summary>
/// <param name="sources"> Test source assemblies paths. </param>
private void DiscoverTestsAndSelectSpecified(IEnumerable<string> sources)
{
this.output.WriteLine(CommandLineResources.StartingDiscovery, OutputLevel.Information);
if (!string.IsNullOrEmpty(EqtTrace.LogFile))
{
this.output.Information(false, CommandLineResources.VstestDiagLogOutputPath, EqtTrace.LogFile);
}
this.testRequestManager.DiscoverTests(
new DiscoveryRequestPayload() { Sources = sources, RunSettings = this.effectiveRunSettings }, this.discoveryEventsRegistrar, Constants.DefaultProtocolConfig);
}
/// <summary>
/// Executes the selected tests
/// </summary>
private void ExecuteSelectedTests()
{
if (this.selectedTestCases.Count > 0)
{
if (this.undiscoveredFilters.Count() != 0)
{
string missingFilters = string.Join(", ", this.undiscoveredFilters);
string warningMessage = string.Format(CultureInfo.CurrentCulture, CommandLineResources.SomeTestsUnavailableAfterFiltering, this.discoveredTestCount, missingFilters);
this.output.Warning(false, warningMessage);
}
// for command line keep alive is always false.
bool keepAlive = false;
EqtTrace.Verbose("RunSpecificTestsArgumentProcessor:Execute: Test run is queued.");
var runRequestPayload = new TestRunRequestPayload() { TestCases = this.selectedTestCases.ToList(), RunSettings = this.effectiveRunSettings, KeepAlive = keepAlive, TestPlatformOptions = new TestPlatformOptions() { TestCaseFilter = this.commandLineOptions.TestCaseFilterValue }};
this.testRequestManager.RunTests(runRequestPayload, null, this.testRunEventsRegistrar, Constants.DefaultProtocolConfig);
}
else
{
string warningMessage;
if (this.discoveredTestCount > 0)
{
// No tests that matched any of the given strings.
warningMessage = string.Format(CultureInfo.CurrentCulture, CommandLineResources.NoTestsAvailableAfterFiltering, this.discoveredTestCount, string.Join(", ", this.selectedTestNames));
}
else
{
// No tests were discovered from the given sources.
warningMessage = string.Format(CultureInfo.CurrentUICulture, CommandLineResources.NoTestsAvailableInSources, string.Join(", ", this.commandLineOptions.Sources));
if (string.IsNullOrEmpty(this.commandLineOptions.TestAdapterPath))
{
warningMessage = string.Format(CultureInfo.CurrentCulture, CommandLineResources.StringFormatToJoinTwoStrings, warningMessage, CommandLineResources.SuggestTestAdapterPathIfNoTestsIsFound);
}
}
this.output.Warning(false, warningMessage);
}
}
/// <summary>
/// Filter discovered tests and find matching tests from given search strings.
/// Any name of the test that can match multiple strings will be added only once.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void discoveryRequest_OnDiscoveredTests(object sender, DiscoveredTestsEventArgs args)
{
this.discoveredTestCount += args.DiscoveredTestCases.Count();
foreach (var testCase in args.DiscoveredTestCases)
{
foreach (var nameCriteria in this.selectedTestNames)
{
if (testCase.FullyQualifiedName.IndexOf(nameCriteria, StringComparison.OrdinalIgnoreCase) != -1)
{
this.selectedTestCases.Add(testCase);
// If a testcase matched then a filter matched - so remove the filter from not found list
this.undiscoveredFilters.Remove(nameCriteria);
break;
}
}
}
}
#endregion
private class DiscoveryEventsRegistrar : ITestDiscoveryEventsRegistrar
{
private EventHandler<DiscoveredTestsEventArgs> discoveredTestsHandler;
public DiscoveryEventsRegistrar(EventHandler<DiscoveredTestsEventArgs> discoveredTestsHandler)
{
this.discoveredTestsHandler = discoveredTestsHandler;
}
public void LogWarning(string message)
{
ConsoleLogger.RaiseTestRunWarning(message);
}
public void RegisterDiscoveryEvents(IDiscoveryRequest discoveryRequest)
{
discoveryRequest.OnDiscoveredTests += this.discoveredTestsHandler;
}
public void UnregisterDiscoveryEvents(IDiscoveryRequest discoveryRequest)
{
discoveryRequest.OnDiscoveredTests -= this.discoveredTestsHandler;
}
}
private class TestRunRequestEventsRegistrar : ITestRunEventsRegistrar
{
private IOutput output;
private CommandLineOptions commandLineOptions;
public TestRunRequestEventsRegistrar(IOutput output, CommandLineOptions commandLineOptions)
{
this.output = output;
this.commandLineOptions = commandLineOptions;
}
public void LogWarning(string message)
{
ConsoleLogger.RaiseTestRunWarning(message);
}
public void RegisterTestRunEvents(ITestRunRequest testRunRequest)
{
testRunRequest.OnRunCompletion += TestRunRequest_OnRunCompletion;
}
public void UnregisterTestRunEvents(ITestRunRequest testRunRequest)
{
testRunRequest.OnRunCompletion -= TestRunRequest_OnRunCompletion;
}
/// <summary>
/// Handles the TestRunRequest complete event
/// </summary>
/// <param name="sender"></param>
/// <param name="e">RunCompletion args</param>
private void TestRunRequest_OnRunCompletion(object sender, TestRunCompleteEventArgs e)
{
// If run is not aborted/cancelled then check the count of executed tests.
// we need to check if there are any tests executed - to try show some help info to user to check for installed vsix extensions
if (!e.IsAborted && !e.IsCanceled)
{
var testsFoundInAnySource = (e.TestRunStatistics == null) ? false : (e.TestRunStatistics.ExecutedTests > 0);
// Indicate the user to use testadapterpath command if there are no tests found
if (!testsFoundInAnySource && string.IsNullOrEmpty(CommandLineOptions.Instance.TestAdapterPath) && this.commandLineOptions.TestCaseFilterValue == null)
{
this.output.Warning(false, CommandLineResources.SuggestTestAdapterPathIfNoTestsIsFound);
}
}
}
}
}
}