-
Notifications
You must be signed in to change notification settings - Fork 758
/
CommandLine.cs
432 lines (353 loc) · 16.4 KB
/
CommandLine.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace Xunit.ConsoleClient
{
public class CommandLine
{
readonly Stack<string> arguments = new Stack<string>();
readonly List<string> unknownOptions = new List<string>();
protected CommandLine(string[] args, Predicate<string> fileExists = null)
{
if (fileExists == null)
fileExists = File.Exists;
for (var i = args.Length - 1; i >= 0; i--)
arguments.Push(args[i]);
Project = Parse(fileExists);
}
public AppDomainSupport? AppDomains { get; protected set; }
public bool Debug { get; protected set; }
public bool DiagnosticMessages { get; protected set; }
public bool InternalDiagnosticMessages { get; protected set; }
public bool FailSkips { get; protected set; }
public int? MaxParallelThreads { get; set; }
public bool NoAutoReporters { get; protected set; }
public bool NoColor { get; protected set; }
public bool NoLogo { get; protected set; }
#if DEBUG
public bool Pause { get; protected set; }
#endif
public XunitProject Project { get; protected set; }
public ParallelAlgorithm? ParallelAlgorithm { get; protected set; }
public bool? ParallelizeAssemblies { get; protected set; }
public bool? ParallelizeTestCollections { get; set; }
public bool Serialize { get; protected set; }
public bool StopOnFail { get; protected set; }
public bool Wait { get; protected set; }
public IRunnerReporter ChooseReporter(IReadOnlyList<IRunnerReporter> reporters)
{
var result = default(IRunnerReporter);
foreach (var unknownOption in unknownOptions)
{
var reporter = reporters.FirstOrDefault(r => r.RunnerSwitch == unknownOption) ?? throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "unknown option: -{0}", unknownOption));
if (result != null)
throw new ArgumentException("only one reporter is allowed");
result = reporter;
}
if (!NoAutoReporters)
result = reporters.FirstOrDefault(r => r.IsEnvironmentallyEnabled) ?? result;
return result ?? new DefaultRunnerReporterWithTypes();
}
protected virtual string GetFullPath(string fileName)
{
return Path.GetFullPath(fileName);
}
XunitProject GetProjectFile(List<Tuple<string, string>> assemblies)
{
var result = new XunitProject();
foreach (var assembly in assemblies)
result.Add(new XunitProjectAssembly
{
AssemblyFilename = GetFullPath(assembly.Item1),
ConfigFilename = assembly.Item2 != null ? GetFullPath(assembly.Item2) : null,
});
return result;
}
static void GuardNoOptionValue(KeyValuePair<string, string> option)
{
if (option.Value != null)
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "error: unknown command line option: {0}", option.Value));
}
static bool IsConfigFile(string fileName)
{
return fileName.EndsWith(".config", StringComparison.OrdinalIgnoreCase)
|| fileName.EndsWith(".json", StringComparison.OrdinalIgnoreCase);
}
public static CommandLine Parse(params string[] args)
=> new CommandLine(args);
protected XunitProject Parse(Predicate<string> fileExists)
{
var assemblies = new List<Tuple<string, string>>();
while (arguments.Count > 0)
{
if (arguments.Peek().StartsWith("-", StringComparison.Ordinal))
break;
var assemblyFile = arguments.Pop();
if (IsConfigFile(assemblyFile))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "expecting assembly, got config file: {0}", assemblyFile));
if (!fileExists(assemblyFile))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "file not found: {0}", assemblyFile));
string configFile = null;
if (arguments.Count > 0)
{
var value = arguments.Peek();
if (!value.StartsWith("-", StringComparison.Ordinal) && IsConfigFile(value))
{
configFile = arguments.Pop();
if (!fileExists(configFile))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "config file not found: {0}", configFile));
}
}
assemblies.Add(Tuple.Create(assemblyFile, configFile));
}
var project = GetProjectFile(assemblies);
while (arguments.Count > 0)
{
var option = PopOption(arguments);
var optionName = option.Key.ToLowerInvariant();
if (!optionName.StartsWith("-", StringComparison.Ordinal))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "unknown command line option: {0}", option.Key));
optionName = optionName.Substring(1);
if (optionName == "nologo")
{
GuardNoOptionValue(option);
NoLogo = true;
}
else if (optionName == "failskips")
{
GuardNoOptionValue(option);
FailSkips = true;
}
else if (optionName == "stoponfail")
{
GuardNoOptionValue(option);
StopOnFail = true;
}
else if (optionName == "nocolor")
{
GuardNoOptionValue(option);
NoColor = true;
TransformFactory.NoErrorColoring = NoColor;
}
else if (optionName == "noappdomain") // Here for historical reasons
{
GuardNoOptionValue(option);
AppDomains = AppDomainSupport.Denied;
}
else if (optionName == "noautoreporters")
{
GuardNoOptionValue(option);
NoAutoReporters = true;
}
#if DEBUG
else if (optionName == "pause")
{
GuardNoOptionValue(option);
Pause = true;
}
#endif
else if (optionName == "debug")
{
GuardNoOptionValue(option);
Debug = true;
}
else if (optionName == "serialize")
{
GuardNoOptionValue(option);
Serialize = true;
}
else if (optionName == "wait")
{
GuardNoOptionValue(option);
Wait = true;
}
else if (optionName == "diagnostics")
{
GuardNoOptionValue(option);
DiagnosticMessages = true;
}
else if (optionName == "internaldiagnostics")
{
GuardNoOptionValue(option);
InternalDiagnosticMessages = true;
}
else if (optionName == "appdomains")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -appdomains");
switch (option.Value)
{
case "ifavailable":
AppDomains = AppDomainSupport.IfAvailable;
break;
case "required":
#if NETFRAMEWORK
// We don't want to throw here on .NET Core, because the user may be specifying a value
// via "dotnet xunit" that is only compatible with some target frameworks.
AppDomains = AppDomainSupport.Required;
#endif
break;
case "denied":
AppDomains = AppDomainSupport.Denied;
break;
default:
throw new ArgumentException("incorrect argument value for -appdomains (must be 'ifavailable', 'required', or 'denied')");
}
}
else if (optionName == "maxthreads")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -maxthreads");
switch (option.Value)
{
case "default":
MaxParallelThreads = 0;
break;
case "unlimited":
MaxParallelThreads = -1;
break;
default:
int threadValue;
if (!int.TryParse(option.Value, out threadValue) || threadValue < 1)
throw new ArgumentException("incorrect argument value for -maxthreads (must be 'default', 'unlimited', or a positive number)");
MaxParallelThreads = threadValue;
break;
}
}
else if (optionName == "parallel")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -parallel");
if (!Enum.TryParse(option.Value, out ParallelismOption parallelismOption))
throw new ArgumentException("incorrect argument value for -parallel");
switch (parallelismOption)
{
case ParallelismOption.all:
ParallelizeAssemblies = true;
ParallelizeTestCollections = true;
break;
case ParallelismOption.assemblies:
ParallelizeAssemblies = true;
ParallelizeTestCollections = false;
break;
case ParallelismOption.collections:
ParallelizeAssemblies = false;
ParallelizeTestCollections = true;
break;
default:
ParallelizeAssemblies = false;
ParallelizeTestCollections = false;
break;
}
}
else if (optionName == "parallelalgorithm")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -parallelAlgorithm");
if (!Enum.TryParse(option.Value, ignoreCase: true, out ParallelAlgorithm parallelAlgorithm))
throw new ArgumentException("incorrect argument value for -parallelAlgorithm");
ParallelAlgorithm = parallelAlgorithm;
}
else if (optionName == "noshadow")
{
GuardNoOptionValue(option);
foreach (var assembly in project.Assemblies)
assembly.Configuration.ShadowCopy = false;
}
else if (optionName == "trait")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -trait");
var pieces = option.Value.Split('=');
if (pieces.Length != 2 || string.IsNullOrEmpty(pieces[0]) || string.IsNullOrEmpty(pieces[1]))
throw new ArgumentException("incorrect argument format for -trait (should be \"name=value\")");
var name = pieces[0];
var value = pieces[1];
project.Filters.IncludedTraits.Add(name, value);
}
else if (optionName == "notrait")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -notrait");
var pieces = option.Value.Split('=');
if (pieces.Length != 2 || string.IsNullOrEmpty(pieces[0]) || string.IsNullOrEmpty(pieces[1]))
throw new ArgumentException("incorrect argument format for -notrait (should be \"name=value\")");
var name = pieces[0];
var value = pieces[1];
project.Filters.ExcludedTraits.Add(name, value);
}
else if (optionName == "class")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -class");
project.Filters.IncludedClasses.Add(option.Value);
}
else if (optionName == "noclass")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -noclass");
project.Filters.ExcludedClasses.Add(option.Value);
}
else if (optionName == "method")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -method");
project.Filters.IncludedMethods.Add(option.Value);
}
else if (optionName == "nomethod")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -nomethod");
project.Filters.ExcludedMethods.Add(option.Value);
}
else if (optionName == "namespace")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -namespace");
project.Filters.IncludedNamespaces.Add(option.Value);
}
else if (optionName == "nonamespace")
{
if (option.Value == null)
throw new ArgumentException("missing argument for -nonamespace");
project.Filters.ExcludedNamespaces.Add(option.Value);
}
else
{
// Might be a result output file...
if (TransformFactory.AvailableTransforms.Any(t => t.CommandLine.Equals(optionName, StringComparison.OrdinalIgnoreCase)))
{
if (option.Value == null)
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "missing filename for {0}", option.Key));
EnsurePathExists(option.Value);
project.Output.Add(optionName, option.Value);
}
// ...or it might be a reporter (we won't know until later)
else
{
GuardNoOptionValue(option);
unknownOptions.Add(optionName);
}
}
}
return project;
}
static KeyValuePair<string, string> PopOption(Stack<string> arguments)
{
var option = arguments.Pop();
string value = null;
if (arguments.Count > 0 && !arguments.Peek().StartsWith("-", StringComparison.Ordinal))
value = arguments.Pop();
return new KeyValuePair<string, string>(option, value);
}
static void EnsurePathExists(string path)
{
var directory = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(directory))
return;
Directory.CreateDirectory(directory);
}
}
}