-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathOutputDeviceManager.cs
More file actions
99 lines (88 loc) · 4.24 KB
/
OutputDeviceManager.cs
File metadata and controls
99 lines (88 loc) · 4.24 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
// 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.Logging;
#if !NET7_0_OR_GREATER
using Microsoft.Testing.Platform.Resources;
#endif
using Microsoft.Testing.Platform.ServerMode;
using Microsoft.Testing.Platform.Services;
namespace Microsoft.Testing.Platform.OutputDevice;
internal sealed class PlatformOutputDeviceManager
{
private Func<IServiceProvider, IPlatformOutputDevice>? _platformOutputDeviceFactory;
public void SetPlatformOutputDevice(Func<IServiceProvider, IPlatformOutputDevice> platformOutputDeviceFactory)
{
Ensure.NotNull(platformOutputDeviceFactory);
_platformOutputDeviceFactory = platformOutputDeviceFactory;
}
internal async Task<ProxyOutputDevice> BuildAsync(ServiceProvider serviceProvider, bool useServerModeOutputDevice, bool isPipeProtocol)
{
// TODO: SetPlatformOutputDevice isn't public yet.
// Before exposing it, do we want to pass the "useServerModeOutputDevice" info to it?
IPlatformOutputDevice? nonServerOutputDevice = _platformOutputDeviceFactory is null
? GetDefaultTerminalOutputDevice(serviceProvider, isPipeProtocol)
: _platformOutputDeviceFactory(serviceProvider);
// If the externally provided output device is not enabled, we opt-in the default terminal output device.
if (_platformOutputDeviceFactory is not null
&& nonServerOutputDevice is not null &&
!await nonServerOutputDevice.IsEnabledAsync().ConfigureAwait(false))
{
nonServerOutputDevice = GetDefaultTerminalOutputDevice(serviceProvider, isPipeProtocol);
}
return new ProxyOutputDevice(
nonServerOutputDevice,
useServerModeOutputDevice
? new ServerModePerCallOutputDevice(
serviceProvider.GetService<FileLoggerProvider>(),
serviceProvider.GetRequiredService<IStopPoliciesService>())
: null);
}
private static IPlatformOutputDevice? GetDefaultTerminalOutputDevice(ServiceProvider serviceProvider, bool isPipeProtocol)
{
if (isPipeProtocol)
{
return null;
}
if (OperatingSystem.IsBrowser())
{
#if NET7_0_OR_GREATER
return new BrowserOutputDevice(
serviceProvider.GetConsole(),
serviceProvider.GetTestApplicationModuleInfo(),
serviceProvider.GetAsyncMonitorFactory().Create(),
serviceProvider.GetRuntimeFeature(),
serviceProvider.GetEnvironment(),
serviceProvider.GetPlatformInformation(),
serviceProvider.GetRequiredService<IStopPoliciesService>());
#else
throw new PlatformNotSupportedException(PlatformResources.BrowserPlatformNotSupportedOnOlderFrameworks);
#endif
}
if (OperatingSystem.IsWasi())
{
return new WasiOutputDevice(
serviceProvider.GetConsole(),
serviceProvider.GetTestApplicationModuleInfo(),
serviceProvider.GetAsyncMonitorFactory().Create(),
serviceProvider.GetRuntimeFeature(),
serviceProvider.GetEnvironment(),
serviceProvider.GetPlatformInformation(),
serviceProvider.GetRequiredService<IStopPoliciesService>());
}
// Default to terminal output device for other platforms.
return new TerminalOutputDevice(
serviceProvider.GetConsole(),
serviceProvider.GetTestApplicationModuleInfo(),
serviceProvider.GetTestHostControllerInfo(),
serviceProvider.GetAsyncMonitorFactory().Create(),
serviceProvider.GetRuntimeFeature(),
serviceProvider.GetEnvironment(),
serviceProvider.GetPlatformInformation(),
serviceProvider.GetCommandLineOptions(),
serviceProvider.GetFileLoggerInformation(),
serviceProvider.GetLoggerFactory(),
serviceProvider.GetClock(),
serviceProvider.GetRequiredService<IStopPoliciesService>(),
serviceProvider.GetTestApplicationCancellationTokenSource());
}
}