-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathTestAssemblyInfo.cs
337 lines (295 loc) · 13.6 KB
/
TestAssemblyInfo.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
// 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.Extensions;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestOutcome = Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel.UnitTestOutcome;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
/// <summary>
/// Defines TestAssembly Info object.
/// </summary>
#if RELEASE
#if NET6_0_OR_GREATER
[Obsolete(Constants.PublicTypeObsoleteMessage, DiagnosticId = "MSTESTOBS")]
#else
[Obsolete(Constants.PublicTypeObsoleteMessage)]
#endif
#endif
public class TestAssemblyInfo
{
private readonly Lock _assemblyInfoExecuteSyncObject = new();
/// <summary>
/// Initializes a new instance of the <see cref="TestAssemblyInfo"/> class.
/// </summary>
/// <param name="assembly">Sets the <see cref="Assembly"/> this class is representing. </param>
internal TestAssemblyInfo(Assembly assembly)
=> Assembly = assembly;
/// <summary>
/// Gets <c>AssemblyInitialize</c> method for the assembly.
/// </summary>
public MethodInfo? AssemblyInitializeMethod
{
get;
internal set
{
if (field != null)
{
DebugEx.Assert(field.DeclaringType?.FullName is not null, "AssemblyInitializeMethod.DeclaringType.FullName is null");
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiAssemblyInit, field.DeclaringType.FullName);
throw new TypeInspectionException(message);
}
field = value;
}
}
internal AssemblyInitializeAttribute? AssemblyInitializeAttribute { get; set; }
/// <summary>
/// Gets or sets the AssemblyInitializeMethod timeout.
/// </summary>
internal TimeoutInfo? AssemblyInitializeMethodTimeoutMilliseconds { get; set; }
/// <summary>
/// Gets or sets the AssemblyCleanupMethod timeout.
/// </summary>
internal TimeoutInfo? AssemblyCleanupMethodTimeoutMilliseconds { get; set; }
/// <summary>
/// Gets <c>AssemblyCleanup</c> method for the assembly.
/// </summary>
public MethodInfo? AssemblyCleanupMethod
{
get;
internal set
{
if (field != null)
{
DebugEx.Assert(field.DeclaringType?.FullName is not null, "AssemblyCleanupMethod.DeclaringType.FullName is null");
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiAssemblyClean, field.DeclaringType.FullName);
throw new TypeInspectionException(message);
}
field = value;
}
}
internal AssemblyCleanupAttribute? AssemblyCleanupAttribute { get; set; }
/// <summary>
/// Gets a value indicating whether <c>AssemblyInitialize</c> has been executed.
/// </summary>
public bool IsAssemblyInitializeExecuted { get; internal set; }
/// <summary>
/// Gets the assembly initialization exception.
/// </summary>
public Exception? AssemblyInitializationException { get; internal set; }
/// <summary>
/// Gets the assembly cleanup exception.
/// </summary>
internal Exception? AssemblyCleanupException { get; private set; }
/// <summary>
/// Gets a value indicating whether this assembly has an executable <c>AssemblyCleanup</c> method.
/// </summary>
public bool HasExecutableCleanupMethod =>
// If no assembly cleanup, then continue with the next one.
AssemblyCleanupMethod != null;
/// <summary>
/// Gets the <see cref="Assembly"/> this class represents.
/// </summary>
internal Assembly Assembly { get; }
/// <summary>
/// Runs assembly initialize method.
/// </summary>
/// <param name="testContext"> The test context. </param>
/// <exception cref="TestFailedException"> Throws a test failed exception if the initialization method throws an exception. </exception>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
public void RunAssemblyInitialize(TestContext testContext)
{
// No assembly initialize => nothing to do.
if (AssemblyInitializeMethod == null)
{
IsAssemblyInitializeExecuted = true;
return;
}
if (testContext == null)
{
// TODO: This exception should be of type ArgumentNullException
#pragma warning disable CA2201 // Do not raise reserved exception types
throw new NullReferenceException(Resource.TestContextIsNull);
#pragma warning restore CA2201 // Do not raise reserved exception types
}
// If assembly initialization is not done, then do it.
if (!IsAssemblyInitializeExecuted)
{
// Acquiring a lock is usually a costly operation which does not need to be
// performed every time if the assembly initialization is already executed.
lock (_assemblyInfoExecuteSyncObject)
{
// Perform a check again.
if (!IsAssemblyInitializeExecuted)
{
try
{
AssemblyInitializationException = FixtureMethodRunner.RunWithTimeoutAndCancellation(
() => AssemblyInitializeAttribute!.ExecuteAsync(new AssemblyInitializeExecutionContext(() => AssemblyInitializeMethod.InvokeAsync(null, testContext))).GetAwaiter().GetResult(),
testContext.CancellationTokenSource,
AssemblyInitializeMethodTimeoutMilliseconds,
AssemblyInitializeMethod,
new AssemblyExecutionContextScope(isCleanup: false),
Resource.AssemblyInitializeWasCancelled,
Resource.AssemblyInitializeTimedOut);
}
catch (Exception ex)
{
AssemblyInitializationException = ex;
}
finally
{
IsAssemblyInitializeExecuted = true;
}
}
}
}
// If assemblyInitialization was successful, then don't do anything
if (AssemblyInitializationException == null)
{
return;
}
// If the exception is already a `TestFailedException` we throw it as-is
if (AssemblyInitializationException is TestFailedException)
{
throw AssemblyInitializationException;
}
Exception realException = AssemblyInitializationException.GetRealException();
UnitTestOutcome outcome = realException is AssertInconclusiveException ? UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed;
// Do not use StackTraceHelper.GetFormattedExceptionMessage(realException) as it prefixes the message with the exception type name.
string exceptionMessage = realException.TryGetMessage();
DebugEx.Assert(AssemblyInitializeMethod.DeclaringType?.FullName is not null, "AssemblyInitializeMethod.DeclaringType.FullName is null");
string errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_AssemblyInitMethodThrows,
AssemblyInitializeMethod.DeclaringType.FullName,
AssemblyInitializeMethod.Name,
realException.GetType().ToString(),
exceptionMessage);
StackTraceInformation? exceptionStackTraceInfo = realException.GetStackTraceInformation();
var testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo, realException);
AssemblyInitializationException = testFailedException;
throw testFailedException;
}
/// <summary>
/// Run assembly cleanup methods.
/// </summary>
/// <returns>
/// Any exception that can be thrown as part of a assembly cleanup as warning messages.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")]
public string? RunAssemblyCleanup()
{
if (AssemblyCleanupMethod == null)
{
return null;
}
lock (_assemblyInfoExecuteSyncObject)
{
try
{
AssemblyCleanupException = FixtureMethodRunner.RunWithTimeoutAndCancellation(
() => AssemblyCleanupAttribute!.ExecuteAsync(new AssemblyCleanupExecutionContext(() => AssemblyCleanupMethod.InvokeAsync(null))).GetAwaiter().GetResult(),
new CancellationTokenSource(),
AssemblyCleanupMethodTimeoutMilliseconds,
AssemblyCleanupMethod,
new AssemblyExecutionContextScope(isCleanup: true),
Resource.AssemblyCleanupWasCancelled,
Resource.AssemblyCleanupTimedOut);
}
catch (Exception ex)
{
AssemblyCleanupException = ex;
}
}
// If assemblyCleanup was successful, then don't do anything
if (AssemblyCleanupException is null)
{
return null;
}
Exception realException = AssemblyCleanupException.GetRealException();
// special case AssertFailedException to trim off part of the stack trace
string errorMessage = realException is AssertFailedException or AssertInconclusiveException
? realException.Message
: realException.GetFormattedExceptionMessage();
DebugEx.Assert(AssemblyCleanupMethod.DeclaringType?.Name is not null, "AssemblyCleanupMethod.DeclaringType.Name is null");
return string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_AssemblyCleanupMethodWasUnsuccesful,
AssemblyCleanupMethod.DeclaringType.Name,
AssemblyCleanupMethod.Name,
errorMessage,
realException.GetStackTraceInformation()?.ErrorStackTrace);
}
/// <summary>
/// Calls the assembly cleanup method in a thread-safe.
/// </summary>
/// <remarks>
/// It is a replacement for RunAssemblyCleanup but as we are in a bug-fix version, we do not want to touch
/// public API and so we introduced this method.
/// </remarks>
internal void ExecuteAssemblyCleanup(TestContext testContext)
{
if (AssemblyCleanupMethod == null)
{
return;
}
lock (_assemblyInfoExecuteSyncObject)
{
try
{
AssemblyCleanupException = FixtureMethodRunner.RunWithTimeoutAndCancellation(
() =>
{
if (AssemblyCleanupMethod.GetParameters().Length == 0)
{
AssemblyCleanupAttribute!.ExecuteAsync(new AssemblyCleanupExecutionContext(() => AssemblyCleanupMethod.InvokeAsync(null))).GetAwaiter().GetResult();
}
else
{
AssemblyCleanupAttribute!.ExecuteAsync(new AssemblyCleanupExecutionContext(() => AssemblyCleanupMethod.InvokeAsync(null, testContext))).GetAwaiter().GetResult();
}
},
testContext.CancellationTokenSource,
AssemblyCleanupMethodTimeoutMilliseconds,
AssemblyCleanupMethod,
new AssemblyExecutionContextScope(isCleanup: true),
Resource.AssemblyCleanupWasCancelled,
Resource.AssemblyCleanupTimedOut);
}
catch (Exception ex)
{
AssemblyCleanupException = ex;
}
}
// If assemblyCleanup was successful, then don't do anything
if (AssemblyCleanupException is null)
{
return;
}
// If the exception is already a `TestFailedException` we throw it as-is
if (AssemblyCleanupException is TestFailedException)
{
throw AssemblyCleanupException;
}
Exception realException = AssemblyCleanupException.GetRealException();
// special case AssertFailedException to trim off part of the stack trace
string errorMessage = realException is AssertFailedException or AssertInconclusiveException
? realException.Message
: realException.GetFormattedExceptionMessage();
StackTraceInformation? exceptionStackTraceInfo = realException.GetStackTraceInformation();
DebugEx.Assert(AssemblyCleanupMethod.DeclaringType?.Name is not null, "AssemblyCleanupMethod.DeclaringType.Name is null");
throw new TestFailedException(
UnitTestOutcome.Failed,
string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_AssemblyCleanupMethodWasUnsuccesful,
AssemblyCleanupMethod.DeclaringType.Name,
AssemblyCleanupMethod.Name,
errorMessage,
exceptionStackTraceInfo?.ErrorStackTrace),
exceptionStackTraceInfo,
realException);
}
}