-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathMSTestExecutor.cs
More file actions
265 lines (231 loc) · 10.2 KB
/
MSTestExecutor.cs
File metadata and controls
265 lines (231 loc) · 10.2 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
// 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.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.VSTestAdapter;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Resources;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
/// <summary>
/// Contains the execution logic for this adapter.
/// </summary>
[ExtensionUri(EngineConstants.ExecutorUriString)]
internal sealed class MSTestExecutor : ITestExecutor
{
private readonly CancellationToken _cancellationToken;
#if !WINDOWS_UWP && !WIN_UI
private readonly Func<string, IDictionary<string, object>, Task>? _telemetrySender;
#endif
/// <summary>
/// Token for canceling the test run.
/// </summary>
private TestRunCancellationToken? _testRunCancellationToken;
/// <summary>
/// Initializes a new instance of the <see cref="MSTestExecutor"/> class.
/// </summary>
public MSTestExecutor()
{
TestExecutionManager = new TestExecutionManager();
_cancellationToken = CancellationToken.None;
}
internal MSTestExecutor(CancellationToken cancellationToken, Func<string, IDictionary<string, object>, Task>? telemetrySender = null)
{
TestExecutionManager = new TestExecutionManager();
_cancellationToken = cancellationToken;
#if !WINDOWS_UWP && !WIN_UI
_telemetrySender = telemetrySender;
#else
_ = telemetrySender;
#endif
}
/// <summary>
/// Gets the ms test execution manager.
/// </summary>
internal TestExecutionManager TestExecutionManager { get; }
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
[ModuleInitializer]
#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
internal static void MSTestModuleInitializer()
{
SetPlatformLogger();
EnsureAdapterAndFrameworkVersions();
}
private static void SetPlatformLogger()
// We set the logger to the VSTest EqtTrace logger as soon as possible via ModuleInitializer.
// If MTP is used, this will get replaced later.
=> PlatformServiceProvider.Instance.AdapterTraceLogger = EqtTraceLogger.Instance;
private static void EnsureAdapterAndFrameworkVersions()
{
string? adapterVersion = typeof(MSTestExecutor).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
string? frameworkVersion = typeof(TestMethodAttribute).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
if (adapterVersion is not null && frameworkVersion is not null
&& adapterVersion != frameworkVersion)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resource.VersionMismatchBetweenAdapterAndFramework, adapterVersion, frameworkVersion));
}
}
/// <summary>
/// Runs the tests.
/// </summary>
/// <param name="tests">The collection of test cases to run.</param>
/// <param name="runContext">The run context.</param>
/// <param name="frameworkHandle">The handle to the framework.</param>
#if DEBUG
[Obsolete("Use RunTestsAsync instead.")]
#endif
public void RunTests(IEnumerable<TestCase>? tests, IRunContext? runContext, IFrameworkHandle? frameworkHandle)
=> RunTestsAsync(tests, runContext, frameworkHandle, null).GetAwaiter().GetResult();
/// <summary>
/// Runs the tests.
/// </summary>
/// <param name="sources">The collection of assemblies to run.</param>
/// <param name="runContext">The run context.</param>
/// <param name="frameworkHandle">The handle to the framework.</param>
#if DEBUG
[Obsolete("Use RunTestsAsync instead.")]
#endif
public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFrameworkHandle? frameworkHandle)
=> RunTestsAsync(sources, runContext, frameworkHandle, null, false).GetAwaiter().GetResult();
internal async Task RunTestsAsync(IEnumerable<TestCase>? tests, IRunContext? runContext, IFrameworkHandle? frameworkHandle, IConfiguration? configuration)
{
if (PlatformServiceProvider.Instance.AdapterTraceLogger.IsInfoEnabled)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.Info("MSTestExecutor.RunTests: Running tests from testcases.");
}
if (frameworkHandle is null)
{
throw new ArgumentNullException(nameof(frameworkHandle));
}
// TODO: Verify why VSTest annotates the IEnumerable as nullable.
if (tests is null)
{
throw new ArgumentNullException(nameof(tests));
}
Ensure.NotEmpty(tests);
// Initialize telemetry collection if not already set
#if !WINDOWS_UWP && !WIN_UI
if (!MSTestTelemetryDataCollector.IsTelemetryOptedOut())
{
_ = MSTestTelemetryDataCollector.EnsureInitialized();
}
#endif
if (!MSTestDiscovererHelpers.InitializeDiscovery(from test in tests select test.Source, runContext, frameworkHandle, configuration, new TestSourceHandler()))
{
return;
}
try
{
await RunTestsFromRightContextAsync(frameworkHandle, async testRunToken => await TestExecutionManager.RunTestsAsync(tests, runContext, frameworkHandle, testRunToken).ConfigureAwait(false)).ConfigureAwait(false);
}
finally
{
await SendTelemetryAsync().ConfigureAwait(false);
}
}
internal async Task RunTestsAsync(IEnumerable<string>? sources, IRunContext? runContext, IFrameworkHandle? frameworkHandle, IConfiguration? configuration, bool isMTP)
{
if (PlatformServiceProvider.Instance.AdapterTraceLogger.IsInfoEnabled)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.Info("MSTestExecutor.RunTests: Running tests from sources.");
}
if (frameworkHandle is null)
{
throw new ArgumentNullException(nameof(frameworkHandle));
}
// TODO: Verify why VSTest annotates the IEnumerable as nullable.
if (sources is null)
{
throw new ArgumentNullException(nameof(sources));
}
Ensure.NotEmpty(sources);
// Initialize telemetry collection if not already set
#if !WINDOWS_UWP && !WIN_UI
if (!MSTestTelemetryDataCollector.IsTelemetryOptedOut())
{
_ = MSTestTelemetryDataCollector.EnsureInitialized();
}
#endif
TestSourceHandler testSourceHandler = new();
if (!MSTestDiscovererHelpers.InitializeDiscovery(sources, runContext, frameworkHandle, configuration, testSourceHandler))
{
return;
}
sources = testSourceHandler.GetTestSources(sources);
try
{
await RunTestsFromRightContextAsync(frameworkHandle, async testRunToken => await TestExecutionManager.RunTestsAsync(sources, runContext, frameworkHandle, testSourceHandler, isMTP, testRunToken).ConfigureAwait(false)).ConfigureAwait(false);
}
finally
{
await SendTelemetryAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Cancel the test run.
/// </summary>
public void Cancel()
=> _testRunCancellationToken?.Cancel();
#if !WINDOWS_UWP && !WIN_UI
private Task SendTelemetryAsync()
=> MSTestTelemetryDataCollector.SendTelemetryAndResetAsync(_telemetrySender);
#else
private static Task SendTelemetryAsync()
=> Task.CompletedTask;
#endif
private async Task RunTestsFromRightContextAsync(IFrameworkHandle frameworkHandle, Func<TestRunCancellationToken, Task> runTestsAction)
{
ApartmentState? requestedApartmentState = MSTestSettings.RunConfigurationSettings.ExecutionApartmentState;
// If we are on Windows and the requested apartment state is different from the current apartment state,
// then run the tests in a new thread.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
&& requestedApartmentState is not null
&& Thread.CurrentThread.GetApartmentState() != requestedApartmentState)
{
Thread entryPointThread = new(() => DoRunTestsAsync().GetAwaiter().GetResult())
{
Name = "MSTest Entry Point",
};
entryPointThread.SetApartmentState(requestedApartmentState.Value);
entryPointThread.Start();
try
{
var threadTask = Task.Run(entryPointThread.Join, _cancellationToken);
await threadTask.ConfigureAwait(false);
}
catch (Exception ex)
{
frameworkHandle.SendMessage(TestMessageLevel.Error, ex.ToString());
}
}
else
{
// If the requested apartment state is STA and the OS is not Windows, then warn the user.
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
&& requestedApartmentState is ApartmentState.STA)
{
frameworkHandle.SendMessage(TestMessageLevel.Warning, Resource.STAIsOnlySupportedOnWindowsWarning);
}
await DoRunTestsAsync().ConfigureAwait(false);
}
// Local functions
async Task DoRunTestsAsync()
{
using (_cancellationToken.Register(Cancel))
{
try
{
_testRunCancellationToken = new TestRunCancellationToken(_cancellationToken);
await runTestsAction(_testRunCancellationToken).ConfigureAwait(false);
}
finally
{
_testRunCancellationToken = null;
}
}
}
}
}