-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathTerminalOutputDevice.cs
More file actions
534 lines (460 loc) · 23.9 KB
/
TerminalOutputDevice.cs
File metadata and controls
534 lines (460 loc) · 23.9 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// 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.Messages;
using Microsoft.Testing.Platform.Extensions.OutputDevice;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.OutputDevice.Terminal;
using Microsoft.Testing.Platform.Resources;
using Microsoft.Testing.Platform.Services;
using Microsoft.Testing.Platform.TestHostControllers;
namespace Microsoft.Testing.Platform.OutputDevice;
/// <summary>
/// Implementation of output device that writes to terminal with progress and optionally with ANSI.
/// </summary>
[UnsupportedOSPlatform("browser")]
internal sealed partial class TerminalOutputDevice : IHotReloadPlatformOutputDevice,
IDataConsumer,
IOutputDeviceDataProducer,
IDisposable,
IAsyncInitializableExtension
{
#pragma warning disable SA1310 // Field names should not contain underscore
private const string TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER = nameof(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER);
#pragma warning restore SA1310 // Field names should not contain underscore
private const char Dash = '-';
private readonly IConsole _console;
private readonly ITestHostControllerInfo _testHostControllerInfo;
private readonly IAsyncMonitor _asyncMonitor;
private readonly IRuntimeFeature _runtimeFeature;
private readonly IEnvironment _environment;
private readonly IPlatformInformation _platformInformation;
private readonly ICommandLineOptions _commandLineOptions;
private readonly IFileLoggerInformation? _fileLoggerInformation;
private readonly ILoggerFactory _loggerFactory;
private readonly IClock _clock;
private readonly IStopPoliciesService _policiesService;
private readonly ITestApplicationCancellationTokenSource _testApplicationCancellationTokenSource;
private readonly string? _longArchitecture;
private readonly string? _shortArchitecture;
// The effective runtime that is executing the application e.g. .NET 9, when .NET 8 application is running with --roll-forward latest.
private readonly string? _runtimeFramework;
// The targeted framework, .NET 8 when application specifies <TargetFramework>net8.0</TargetFramework>
private readonly string? _targetFramework;
private readonly string _assemblyName;
private TerminalTestReporter? _terminalTestReporter;
private bool _bannerDisplayed;
private bool _isListTests;
private bool _isServerMode;
private ILogger? _logger;
private TestProcessRole? _processRole;
public TerminalOutputDevice(
IConsole console,
ITestApplicationModuleInfo testApplicationModuleInfo, ITestHostControllerInfo testHostControllerInfo, IAsyncMonitor asyncMonitor,
IRuntimeFeature runtimeFeature, IEnvironment environment, IPlatformInformation platformInformation,
ICommandLineOptions commandLineOptions, IFileLoggerInformation? fileLoggerInformation, ILoggerFactory loggerFactory, IClock clock,
IStopPoliciesService policiesService, ITestApplicationCancellationTokenSource testApplicationCancellationTokenSource)
{
_console = console;
_testHostControllerInfo = testHostControllerInfo;
_asyncMonitor = asyncMonitor;
_runtimeFeature = runtimeFeature;
_environment = environment;
_platformInformation = platformInformation;
_commandLineOptions = commandLineOptions;
_fileLoggerInformation = fileLoggerInformation;
_loggerFactory = loggerFactory;
_clock = clock;
_policiesService = policiesService;
_testApplicationCancellationTokenSource = testApplicationCancellationTokenSource;
if (_runtimeFeature.IsDynamicCodeSupported)
{
#if !NETCOREAPP
_longArchitecture = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
_shortArchitecture = GetShortArchitecture(_longArchitecture);
#else
// RID has the operating system, we want to see that in the banner, but not next to every dll.
_longArchitecture = RuntimeInformation.RuntimeIdentifier;
_shortArchitecture = TerminalOutputDevice.GetShortArchitecture(RuntimeInformation.RuntimeIdentifier);
#endif
_runtimeFramework = TargetFrameworkParser.GetShortTargetFramework(RuntimeInformation.FrameworkDescription);
_targetFramework = TargetFrameworkParser.GetShortTargetFramework(Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkDisplayName) ?? _runtimeFramework;
}
_assemblyName = testApplicationModuleInfo.GetDisplayName();
if (environment.GetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER) is not null)
{
_bannerDisplayed = true;
}
_testApplicationCancellationTokenSource = testApplicationCancellationTokenSource;
}
public async Task InitializeAsync()
{
await _policiesService.RegisterOnAbortCallbackAsync(
() =>
{
_terminalTestReporter?.StartCancelling();
return Task.CompletedTask;
}).ConfigureAwait(false);
if (_fileLoggerInformation is not null)
{
_logger = _loggerFactory.CreateLogger(GetType().ToString());
}
_isListTests = _commandLineOptions.IsOptionSet(PlatformCommandLineProvider.DiscoverTestsOptionKey);
_isServerMode = _commandLineOptions.IsOptionSet(PlatformCommandLineProvider.ServerOptionKey);
bool noAnsi = _commandLineOptions.IsOptionSet(TerminalTestReporterCommandLineOptionsProvider.NoAnsiOption);
// TODO: Replace this with proper CI detection that we already have in telemetry. https://github.com/microsoft/testfx/issues/5533#issuecomment-2838893327
bool inCI = string.Equals(_environment.GetEnvironmentVariable("TF_BUILD"), "true", StringComparison.OrdinalIgnoreCase) || string.Equals(_environment.GetEnvironmentVariable("GITHUB_ACTIONS"), "true", StringComparison.OrdinalIgnoreCase);
AnsiMode ansiMode = AnsiMode.AnsiIfPossible;
if (noAnsi)
{
// User explicitly specified --no-ansi.
// We should respect that.
ansiMode = AnsiMode.NoAnsi;
}
else if (inCI)
{
ansiMode = AnsiMode.SimpleAnsi;
}
bool noProgress = _commandLineOptions.IsOptionSet(TerminalTestReporterCommandLineOptionsProvider.NoProgressOption);
// _runtimeFeature.IsHotReloadEnabled is not set to true here, even if the session will be HotReload,
// we need to postpone that decision until the first test result.
//
// This works but is NOT USED, we prefer to have the same experience of not showing passed tests in hotReload mode as in normal mode.
// Func<bool> showPassed = () => _runtimeFeature.IsHotReloadEnabled;
Func<bool> showPassed = () => false;
bool outputOption = _commandLineOptions.TryGetOptionArgumentList(TerminalTestReporterCommandLineOptionsProvider.OutputOption, out string[]? arguments);
if (outputOption && arguments?.Length > 0 && TerminalTestReporterCommandLineOptionsProvider.OutputOptionDetailedArgument.Equals(arguments[0], StringComparison.OrdinalIgnoreCase))
{
showPassed = () => true;
}
Func<bool?> shouldShowProgress = noProgress || ansiMode is AnsiMode.NoAnsi or AnsiMode.SimpleAnsi
// User preference is to not show progress.
// Or, we are in terminal that's not capable of changing cursor and we can't update progress in-place.
// In that case, we force disable progress as well.
? static () => false
// User preference is to allow showing progress, figure if we should actually show it based on whether or not we are a testhost controller.
//
// TestHost controller is not running any tests and it should not be writing progress.
//
// The test host controller info is not setup and populated until after this constructor, because it writes banner and then after it figures out if
// the runner is a testHost controller, so we would always have it as null if we capture it directly. Instead we need to check it via
// func.
: () => _isListTests || _isServerMode
? false
: !_testHostControllerInfo.IsCurrentProcessTestHostController;
// This is single exe run, don't show all the details of assemblies and their summaries.
_terminalTestReporter = new TerminalTestReporter(_assemblyName, _targetFramework, _shortArchitecture, _console, _testApplicationCancellationTokenSource, new()
{
ShowPassedTests = showPassed,
MinimumExpectedTests = PlatformCommandLineProvider.GetMinimumExpectedTests(_commandLineOptions),
AnsiMode = ansiMode,
ShowActiveTests = true,
ShowProgress = shouldShowProgress,
});
}
private static string GetShortArchitecture(string runtimeIdentifier)
=> runtimeIdentifier.Contains(Dash)
? runtimeIdentifier.Split(Dash, 2)[1]
: runtimeIdentifier;
public Type[] DataTypesConsumed { get; } =
[
typeof(TestNodeUpdateMessage),
typeof(SessionFileArtifact),
typeof(FileArtifact),
];
/// <inheritdoc />
public string Uid => nameof(TerminalOutputDevice);
/// <inheritdoc />
public string Version => AppVersion.DefaultSemVer;
/// <inheritdoc />
public string DisplayName => "Test Platform Console Service";
/// <inheritdoc />
public string Description => "Test Platform default console service";
/// <inheritdoc />
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
private async Task LogDebugAsync(string message)
{
if (_logger is not null)
{
await _logger.LogDebugAsync(message).ConfigureAwait(false);
}
}
public async Task DisplayBannerAsync(string? bannerMessage, CancellationToken cancellationToken)
{
RoslynDebug.Assert(_terminalTestReporter is not null);
using (await _asyncMonitor.LockAsync(TimeoutHelper.DefaultHangTimeSpanTimeout).ConfigureAwait(false))
{
if (!_bannerDisplayed && !_isServerMode)
{
// skip the banner for the children processes
_environment.SetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
_bannerDisplayed = true;
if (bannerMessage is not null)
{
_terminalTestReporter.WriteMessage(bannerMessage);
}
else
{
StringBuilder stringBuilder = new();
stringBuilder.Append(_platformInformation.Name);
if (_platformInformation.Version is { } version)
{
stringBuilder.Append(CultureInfo.InvariantCulture, $" v{version}");
if (_platformInformation.CommitHash is { } commitHash)
{
stringBuilder.Append(CultureInfo.InvariantCulture, $"+{commitHash[..10]}");
}
}
if (_platformInformation.BuildDate is { } buildDate)
{
stringBuilder.Append(CultureInfo.InvariantCulture, $" (UTC {buildDate.UtcDateTime:d})");
}
if (_runtimeFeature.IsDynamicCodeSupported)
{
stringBuilder.Append(" [");
stringBuilder.Append(_longArchitecture);
stringBuilder.Append(" - ");
stringBuilder.Append(_runtimeFramework);
stringBuilder.Append(']');
}
_terminalTestReporter.WriteMessage(stringBuilder.ToString());
}
}
if (_fileLoggerInformation is not null)
{
if (_fileLoggerInformation.SynchronousWrite)
{
_terminalTestReporter.WriteWarningMessage(string.Format(CultureInfo.CurrentCulture, PlatformResources.DiagnosticFileLevelWithFlush, _fileLoggerInformation.LogLevel, _fileLoggerInformation.LogFile.FullName), padding: null);
}
else
{
_terminalTestReporter.WriteWarningMessage(string.Format(CultureInfo.CurrentCulture, PlatformResources.DiagnosticFileLevelWithAsyncFlush, _fileLoggerInformation.LogLevel, _fileLoggerInformation.LogFile.FullName), padding: null);
}
}
}
}
public async Task DisplayBeforeHotReloadSessionStartAsync(CancellationToken cancellationToken)
=> await DisplayBeforeSessionStartAsync(cancellationToken).ConfigureAwait(false);
public async Task DisplayBeforeSessionStartAsync(CancellationToken cancellationToken)
{
if (_isServerMode)
{
return;
}
RoslynDebug.Assert(_terminalTestReporter is not null);
// Start test execution here, rather than in ShowBanner, because then we know
// if we are a testHost controller or not, and if we should show progress bar.
_terminalTestReporter.TestExecutionStarted(_clock.UtcNow, workerCount: 1, isDiscovery: _isListTests);
_terminalTestReporter.AssemblyRunStarted();
if (_logger is not null && _logger.IsEnabled(LogLevel.Trace))
{
await _logger.LogTraceAsync("DisplayBeforeSessionStartAsync").ConfigureAwait(false);
}
}
public async Task DisplayAfterHotReloadSessionEndAsync(CancellationToken cancellationToken)
=> await DisplayAfterSessionEndRunInternalAsync().ConfigureAwait(false);
public async Task DisplayAfterSessionEndRunAsync(CancellationToken cancellationToken)
{
if (_isServerMode)
{
return;
}
// Do NOT check and store the value in the constructor
// it won't be populated yet, so you will always see false.
if (_runtimeFeature.IsHotReloadEnabled)
{
return;
}
await DisplayAfterSessionEndRunInternalAsync().ConfigureAwait(false);
}
private async Task DisplayAfterSessionEndRunInternalAsync()
{
RoslynDebug.Assert(_terminalTestReporter is not null);
using (await _asyncMonitor.LockAsync(TimeoutHelper.DefaultHangTimeSpanTimeout).ConfigureAwait(false))
{
if (_processRole == TestProcessRole.TestHost)
{
_terminalTestReporter.AssemblyRunCompleted();
_terminalTestReporter.TestExecutionCompleted(_clock.UtcNow);
}
else
{
_terminalTestReporter.PrintOutOfProcessArtifacts();
}
}
}
/// <summary>
/// Displays provided data through IConsole, which is typically System.Console.
/// </summary>
/// <param name="producer">The producer that sent the data.</param>
/// <param name="data">The data to be displayed.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task DisplayAsync(IOutputDeviceDataProducer producer, IOutputDeviceData data, CancellationToken cancellationToken)
{
RoslynDebug.Assert(_terminalTestReporter is not null);
using (await _asyncMonitor.LockAsync(TimeoutHelper.DefaultHangTimeSpanTimeout).ConfigureAwait(false))
{
switch (data)
{
case FormattedTextOutputDeviceData formattedTextData:
await LogDebugAsync(formattedTextData.Text).ConfigureAwait(false);
_terminalTestReporter.WriteMessage(formattedTextData.Text, formattedTextData.ForegroundColor as SystemConsoleColor, formattedTextData.Padding);
break;
case TextOutputDeviceData textData:
await LogDebugAsync(textData.Text).ConfigureAwait(false);
_terminalTestReporter.WriteMessage(textData.Text);
break;
case WarningMessageOutputDeviceData warningData:
await LogDebugAsync(warningData.Message).ConfigureAwait(false);
_terminalTestReporter.WriteWarningMessage(warningData.Message, null);
break;
case ErrorMessageOutputDeviceData errorData:
await LogDebugAsync(errorData.Message).ConfigureAwait(false);
_terminalTestReporter.WriteErrorMessage(errorData.Message, null);
break;
case ExceptionOutputDeviceData exceptionOutputDeviceData:
await LogDebugAsync(exceptionOutputDeviceData.Exception.ToString()).ConfigureAwait(false);
_terminalTestReporter.WriteErrorMessage(exceptionOutputDeviceData.Exception);
break;
}
}
}
public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
{
RoslynDebug.Assert(_terminalTestReporter is not null);
cancellationToken.ThrowIfCancellationRequested();
if (_isServerMode)
{
return Task.CompletedTask;
}
switch (value)
{
case TestNodeUpdateMessage testNodeStateChanged:
TimeSpan? duration = testNodeStateChanged.TestNode.Properties.SingleOrDefault<TimingProperty>()?.GlobalTiming.Duration;
foreach (FileArtifactProperty artifact in testNodeStateChanged.TestNode.Properties.OfType<FileArtifactProperty>())
{
_terminalTestReporter.ArtifactAdded(
outOfProcess: _processRole != TestProcessRole.TestHost,
testNodeStateChanged.TestNode.DisplayName,
artifact.FileInfo.FullName);
}
switch (testNodeStateChanged.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>())
{
case InProgressTestNodeStateProperty:
_terminalTestReporter.TestInProgress(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName);
break;
case ErrorTestNodeStateProperty errorState:
_terminalTestReporter.TestCompleted(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName,
TestOutcome.Error,
duration,
null,
errorState.Explanation,
errorState.Exception,
expected: null,
actual: null);
break;
case FailedTestNodeStateProperty failedState:
_terminalTestReporter.TestCompleted(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName,
TestOutcome.Fail,
duration,
null,
failedState.Explanation,
failedState.Exception,
expected: failedState.Exception?.Data["assert.expected"] as string,
actual: failedState.Exception?.Data["assert.actual"] as string);
break;
case TimeoutTestNodeStateProperty timeoutState:
_terminalTestReporter.TestCompleted(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName,
TestOutcome.Timeout,
duration,
null,
timeoutState.Explanation,
timeoutState.Exception,
expected: null,
actual: null);
break;
#pragma warning disable CS0618 // Type or member is obsolete
case CancelledTestNodeStateProperty cancelledState:
#pragma warning restore CS0618 // Type or member is obsolete
_terminalTestReporter.TestCompleted(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName,
TestOutcome.Canceled,
duration,
null,
cancelledState.Explanation,
cancelledState.Exception,
expected: null,
actual: null);
break;
case PassedTestNodeStateProperty:
_terminalTestReporter.TestCompleted(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName,
outcome: TestOutcome.Passed,
duration: duration,
informativeMessage: null,
errorMessage: null,
exception: null,
expected: null,
actual: null);
break;
case SkippedTestNodeStateProperty skippedState:
_terminalTestReporter.TestCompleted(
testNodeStateChanged.TestNode.Uid.Value,
testNodeStateChanged.TestNode.DisplayName,
TestOutcome.Skipped,
duration,
informativeMessage: skippedState.Explanation,
errorMessage: null,
exception: null,
expected: null,
actual: null);
break;
case DiscoveredTestNodeStateProperty:
_terminalTestReporter.TestDiscovered(testNodeStateChanged.TestNode.DisplayName);
break;
}
break;
case SessionFileArtifact artifact:
{
_terminalTestReporter.ArtifactAdded(
outOfProcess: _processRole != TestProcessRole.TestHost,
testName: null,
artifact.FileInfo.FullName);
}
break;
case FileArtifact artifact:
{
_terminalTestReporter.ArtifactAdded(
outOfProcess: _processRole != TestProcessRole.TestHost,
testName: null,
artifact.FileInfo.FullName);
}
break;
}
return Task.CompletedTask;
}
public void Dispose()
=> _terminalTestReporter?.Dispose();
public async Task HandleProcessRoleAsync(TestProcessRole processRole, CancellationToken cancellationToken)
{
_processRole = processRole;
if (processRole == TestProcessRole.TestHost)
{
await _policiesService.RegisterOnMaxFailedTestsCallbackAsync(
async (maxFailedTests, _) => await DisplayAsync(
this, new TextOutputDeviceData(string.Format(CultureInfo.InvariantCulture, PlatformResources.ReachedMaxFailedTestsMessage, maxFailedTests)), cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}
}
}