-
Notifications
You must be signed in to change notification settings - Fork 758
/
ConsoleRunner.cs
430 lines (347 loc) · 14.3 KB
/
ConsoleRunner.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Xunit.Internal;
using Xunit.Runner.Common;
using Xunit.Sdk;
using Xunit.v3;
namespace Xunit.Runner.InProc.SystemConsole;
/// <summary>
/// This class is the entry point for the in-process console-based runner used for
/// xUnit.net v3 test projects.
/// </summary>
public class ConsoleRunner
{
readonly string[] args;
bool automated;
volatile bool cancel;
readonly object consoleLock;
bool executed;
bool failed;
IRunnerLogger? logger;
IReadOnlyList<IRunnerReporter>? runnerReporters;
readonly Assembly testAssembly;
readonly TestExecutionSummaries testExecutionSummaries = new();
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleRunner"/> class.
/// </summary>
/// <param name="args">The arguments passed to the application; typically pulled from the Main method.</param>
/// <param name="testAssembly">The (optional) assembly to test; defaults to <see cref="Assembly.GetEntryAssembly"/>.</param>
/// <param name="runnerReporters">The (optional) list of runner reporters.</param>
/// <param name="consoleLock">The (optional) lock used around all console output to ensure there are no write collisions.</param>
public ConsoleRunner(
string[] args,
Assembly? testAssembly = null,
IEnumerable<IRunnerReporter>? runnerReporters = null,
object? consoleLock = null)
{
this.args = Guard.ArgumentNotNull(args);
this.testAssembly = Guard.ArgumentNotNull("testAssembly was null, and Assembly.GetEntryAssembly() returned null; you should pass a non-null value for testAssembly", testAssembly ?? Assembly.GetEntryAssembly(), nameof(testAssembly));
this.consoleLock = consoleLock ?? new object();
this.runnerReporters = runnerReporters.CastOrToReadOnlyList();
}
/// <summary>
/// The entry point to begin running tests.
/// </summary>
/// <returns>The return value intended to be returned by the Main method.</returns>
public async ValueTask<int> EntryPoint()
{
if (executed)
throw new InvalidOperationException("The EntryPoint method can only be called once.");
executed = true;
SetOutputEncoding();
var globalInternalDiagnosticMessages = false;
var noColor = false;
try
{
var commandLine = new CommandLine(testAssembly, args, runnerReporters);
if (commandLine.HelpRequested)
{
PrintHeader();
Console.WriteLine("Copyright (C) .NET Foundation.");
Console.WriteLine();
Console.WriteLine("usage: [:seed] [path/to/configFile.json] [options] [filters] [reporter] [resultFormat filename [...]]");
commandLine.PrintUsage();
return 2;
}
// We pick up the -automated flag early, because Parse() can throw and we want to use automated output
// to report any command line parsing problems.
automated = commandLine.AutomatedRequested;
if (automated)
noColor = true;
var project = commandLine.Parse();
var useAnsiColor = project.Configuration.UseAnsiColorOrDefault;
if (useAnsiColor)
ConsoleHelper.UseAnsiColor();
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
Console.CancelKeyPress += (sender, e) =>
{
if (!cancel)
{
if (automated)
Console.WriteLine(new _DiagnosticMessage("Cancellation request received").ToJson());
else
Console.WriteLine("Canceling... (Press Ctrl+C again to terminate)");
cancel = true;
e.Cancel = true;
}
};
if (project.Configuration.PauseOrDefault)
{
if (!automated)
Console.Write("Press any key to start execution...");
Console.ReadKey(true);
if (!automated)
Console.WriteLine();
}
if (project.Configuration.DebugOrDefault)
Debugger.Launch();
var globalDiagnosticMessages = project.Assemblies.Any(a => a.Configuration.DiagnosticMessagesOrDefault);
globalInternalDiagnosticMessages = project.Assemblies.Any(a => a.Configuration.InternalDiagnosticMessagesOrDefault);
if (!automated)
noColor = project.Configuration.NoColorOrDefault;
logger = new ConsoleRunnerLogger(!noColor, useAnsiColor, consoleLock);
_IMessageSink? globalDiagnosticMessageSink =
automated
? new AutomatedDiagnosticMessageSink(consoleLock)
: ConsoleDiagnosticMessageSink.TryCreate(consoleLock, noColor, globalDiagnosticMessages, globalInternalDiagnosticMessages);
var reporter = automated ? new JsonReporter() : project.RunnerReporter;
var reporterMessageHandler = await reporter.CreateMessageHandler(logger, globalDiagnosticMessageSink);
if (!reporter.ForceNoLogo && !project.Configuration.NoLogoOrDefault)
PrintHeader();
foreach (string warning in commandLine.ParseWarnings)
if (automated)
Console.WriteLine(new _DiagnosticMessage("warning: " + warning).ToJson());
else
logger.LogWarning(warning);
var failCount = 0;
if (project.Configuration.List is not null)
await ListProject(project);
else
failCount = await RunProject(project, reporterMessageHandler);
if (cancel)
return -1073741510; // 0xC000013A: The application terminated as a result of a CTRL+C
if (project.Configuration.WaitOrDefault)
{
if (!automated)
{
Console.WriteLine();
Console.Write("Press any key to continue...");
}
Console.ReadKey();
if (!automated)
Console.WriteLine();
}
return project.Configuration.IgnoreFailures == true || failCount == 0 ? 0 : 1;
}
catch (Exception ex)
{
if (!noColor)
ConsoleHelper.SetForegroundColor(ConsoleColor.Red);
if (automated)
Console.WriteLine(new _DiagnosticMessage("error: " + ex.Message).ToJson());
else
{
Console.WriteLine("error: {0}", ex.Message);
if (globalInternalDiagnosticMessages)
{
if (!noColor)
ConsoleHelper.SetForegroundColor(ConsoleColor.DarkGray);
Console.WriteLine(ex.StackTrace);
}
}
return ex is ArgumentException ? 3 : 4;
}
finally
{
if (!noColor)
ConsoleHelper.ResetColor();
}
}
async ValueTask ListProject(XunitProject project)
{
var (listOption, listFormat) = project.Configuration.List!.Value;
var testCasesByAssembly = new Dictionary<string, List<_ITestCase>>();
foreach (var assembly in project.Assemblies)
{
var assemblyFileName = Guard.ArgumentNotNull(assembly.AssemblyFileName);
// Default to false for console runners
assembly.Configuration.PreEnumerateTheories ??= false;
// Setup discovery options with command line overrides
var discoveryOptions = _TestFrameworkOptions.ForDiscovery(assembly.Configuration);
var noColor = assembly.Project.Configuration.NoColorOrDefault;
var diagnosticMessages = assembly.Configuration.DiagnosticMessagesOrDefault;
var internalDiagnosticMessages = assembly.Configuration.InternalDiagnosticMessagesOrDefault;
var diagnosticMessageSink = ConsoleDiagnosticMessageSink.TryCreate(consoleLock, noColor, diagnosticMessages, internalDiagnosticMessages);
TestContext.SetForInitialization(diagnosticMessageSink, diagnosticMessages, internalDiagnosticMessages);
var assemblyInfo = new ReflectionAssemblyInfo(testAssembly);
#pragma warning disable CA2007 // Cannot use ConfigureAwait here because it changes the type of disposalTracker
await using var disposalTracker = new DisposalTracker();
#pragma warning restore CA2007
var testFramework = ExtensibilityPointFactory.GetTestFramework(assemblyInfo);
disposalTracker.Add(testFramework);
// Discover & filter the tests
var testCases = new List<_ITestCase>();
var testDiscoverer = testFramework.GetDiscoverer(assemblyInfo);
await testDiscoverer.Find(testCase => { testCases.Add(testCase); return new(!cancel); }, discoveryOptions);
var testCasesDiscovered = testCases.Count;
var filteredTestCases = testCases.Where(assembly.Configuration.Filters.Filter).ToList();
testCasesByAssembly.Add(assemblyFileName, filteredTestCases);
}
ConsoleProjectLister.List(testCasesByAssembly, listOption, listFormat);
}
void OnUnhandledException(
object sender,
UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
if (automated)
Console.WriteLine(_ErrorMessage.FromException(ex).ToJson());
else
Console.WriteLine(ex.ToString());
}
else
{
if (automated)
Console.WriteLine(new _DiagnosticMessage("Error of unknown type thrown in application domain").ToJson());
else
Console.WriteLine("Error of unknown type thrown in application domain");
}
Environment.Exit(1);
}
static void PrintHeader() =>
Console.WriteLine(
"xUnit.net v3 In-Process Runner v{0} ({1}-bit {2})",
ThisAssembly.AssemblyInformationalVersion,
IntPtr.Size * 8,
RuntimeInformation.FrameworkDescription
);
/// <summary>
/// Creates a new <see cref="ConsoleRunner"/> instance and runs it via <see cref="EntryPoint"/>.
/// </summary>
/// <param name="args">The arguments passed to the application; typically pulled from the Main method.</param>
/// <param name="testAssembly">The (optional) assembly to test; defaults to <see cref="Assembly.GetEntryAssembly"/>.</param>
/// <param name="runnerReporters">The (optional) list of runner reporters.</param>
/// <param name="consoleLock">The (optional) lock used around all console output to ensure there are no write collisions.</param>
/// <returns>The return value intended to be returned by the Main method.</returns>
public static ValueTask<int> Run(
string[] args,
Assembly? testAssembly = null,
IEnumerable<IRunnerReporter>? runnerReporters = null,
object? consoleLock = null) =>
new ConsoleRunner(args, testAssembly, runnerReporters, consoleLock).EntryPoint();
async ValueTask<int> RunProject(
XunitProject project,
_IMessageSink reporterMessageHandler)
{
XElement? assembliesElement = null;
var clockTime = Stopwatch.StartNew();
var xmlTransformers = TransformFactory.GetXmlTransformers(project);
var needsXml = xmlTransformers.Count > 0;
if (needsXml)
assembliesElement = TransformFactory.CreateAssembliesElement();
var originalWorkingFolder = Directory.GetCurrentDirectory();
var assembly = project.Assemblies.Single();
var assemblyElement = await RunProjectAssembly(
assembly,
needsXml,
reporterMessageHandler
);
if (assemblyElement is not null)
assembliesElement?.Add(assemblyElement);
clockTime.Stop();
testExecutionSummaries.ElapsedClockTime = clockTime.Elapsed;
reporterMessageHandler.OnMessage(testExecutionSummaries);
Directory.SetCurrentDirectory(originalWorkingFolder);
if (assembliesElement is not null)
{
TransformFactory.FinishAssembliesElement(assembliesElement);
xmlTransformers.ForEach(transformer => transformer(assembliesElement));
}
return failed ? 1 : testExecutionSummaries.SummariesByAssemblyUniqueID.Sum(s => s.Summary.Failed + s.Summary.Errors);
}
async ValueTask<XElement?> RunProjectAssembly(
XunitProjectAssembly assembly,
bool needsXml,
_IMessageSink reporterMessageHandler)
{
if (cancel)
return null;
var assemblyElement = needsXml ? new XElement("assembly") : null;
try
{
// Default to false for console runners
assembly.Configuration.PreEnumerateTheories ??= false;
// Setup discovery and execution options with command-line overrides
var discoveryOptions = _TestFrameworkOptions.ForDiscovery(assembly.Configuration);
var executionOptions = _TestFrameworkOptions.ForExecution(assembly.Configuration);
var noColor = assembly.Project.Configuration.NoColorOrDefault;
var diagnosticMessages = assembly.Configuration.DiagnosticMessagesOrDefault;
var internalDiagnosticMessages = assembly.Configuration.InternalDiagnosticMessagesOrDefault;
var diagnosticMessageSink = ConsoleDiagnosticMessageSink.TryCreate(consoleLock, noColor, diagnosticMessages, internalDiagnosticMessages);
var longRunningSeconds = assembly.Configuration.LongRunningTestSecondsOrDefault;
TestContext.SetForInitialization(diagnosticMessageSink, diagnosticMessages, internalDiagnosticMessages);
var assemblyInfo = new ReflectionAssemblyInfo(testAssembly);
#pragma warning disable CA2007 // Cannot use ConfigureAwait here because it changes the type of disposalTracker
await using var disposalTracker = new DisposalTracker();
#pragma warning restore CA2007
var testFramework = ExtensibilityPointFactory.GetTestFramework(assemblyInfo);
disposalTracker.Add(testFramework);
var frontController = new InProcessFrontController(testFramework, assemblyInfo, assembly.ConfigFileName);
var sinkOptions = new ExecutionSinkOptions
{
AssemblyElement = assemblyElement,
CancelThunk = () => cancel,
DiagnosticMessageSink = diagnosticMessageSink,
FailSkips = assembly.Configuration.FailSkipsOrDefault,
FailWarn = assembly.Configuration.FailWarnsOrDefault,
LongRunningTestTime = TimeSpan.FromSeconds(longRunningSeconds),
};
using var resultsSink = new ExecutionSink(assembly, discoveryOptions, executionOptions, AppDomainOption.NotAvailable, shadowCopy: false, reporterMessageHandler, sinkOptions);
await frontController.FindAndRun(resultsSink, discoveryOptions, executionOptions, assembly.Configuration.Filters.Filter);
testExecutionSummaries.Add(frontController.TestAssemblyUniqueID, resultsSink.ExecutionSummary);
if (resultsSink.ExecutionSummary.Failed != 0 && executionOptions.GetStopOnTestFailOrDefault())
{
if (automated)
Console.WriteLine(new _DiagnosticMessage("Canceling due to test failure").ToJson());
else
Console.WriteLine("Canceling due to test failure...");
cancel = true;
}
}
catch (Exception ex)
{
failed = true;
var e = ex;
while (e is not null)
{
if (automated)
Console.WriteLine(_ErrorMessage.FromException(e).ToJson());
else
{
Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message);
if (assembly.Configuration.InternalDiagnosticMessagesOrDefault)
Console.WriteLine(e.StackTrace);
}
e = e.InnerException;
}
}
return assemblyElement;
}
/// <summary>
/// Override this function to change the default output encoding for the system console.
/// The default is set to <see cref="Encoding.UTF8"/> to support our usage of Unicode
/// characters in output (for example, the up and down arrows printed for pointers with
/// mismatched assertion values).
/// </summary>
protected virtual void SetOutputEncoding() =>
Console.OutputEncoding = Encoding.UTF8;
}