-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathNUnitFrameworkApi2018.cs
More file actions
306 lines (259 loc) · 11.7 KB
/
Copy pathNUnitFrameworkApi2018.cs
File metadata and controls
306 lines (259 loc) · 11.7 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
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using NUnit.Common;
#if NETCOREAPP
using System.Runtime.Loader;
using NUnit.Engine.Internal;
#endif
namespace NUnit.Engine.Drivers
{
/// <summary>
/// This is the revised API, designed for use with .NET Core. It first
/// appears in our source code in 2018. This implementation is modified
/// to make it work under the .NET Framework as well as .NET Core. It
/// may be used for NUnit 3.10 or higher.
/// </summary>
#if NETFRAMEWORK
public class NUnitFrameworkApi2018 : MarshalByRefObject, NUnitFrameworkApi
#else
public class NUnitFrameworkApi2018 : NUnitFrameworkApi
#endif
{
private static readonly Logger log = InternalTrace.GetLogger(nameof(NUnitFrameworkApi2018));
private const string LOAD_MESSAGE = "Method called without calling Load first. Possible error in runner.";
private const string INVALID_FRAMEWORK_MESSAGE = "Running tests against this version of the framework using this driver is not supported. Please update NUnit.Framework to the latest version.";
private const string FAILED_TO_LOAD_ASSEMBLY = "Failed to load assembly ";
private const string FAILED_TO_LOAD_NUNIT = "Failed to load the NUnit Framework in the test assembly";
private const string CONTROLLER_TYPE = "NUnit.Framework.Api.FrameworkController";
private readonly string _driverId;
private readonly AssemblyName _nunitRef;
private object? _frameworkController;
private Type? _frameworkControllerType;
#if NETCOREAPP
private AssemblyLoadContext? _assemblyLoadContext;
private TestAssemblyResolver? _testAssemblyResolver;
private Assembly? _testAssembly;
private Assembly? _frameworkAssembly;
internal List<ResolutionStrategy>? ResolutionStrategies => _testAssemblyResolver?.ResolutionStrategies;
#endif
private string? _testAssemblyPath;
public NUnitFrameworkApi2018(string driverId, AssemblyName nunitRef)
{
Guard.ArgumentNotNull(driverId);
Guard.ArgumentNotNull(nunitRef);
_driverId = driverId;
_nunitRef = nunitRef;
}
public string Load(string testAssemblyPath, IDictionary<string, object> settings)
{
Guard.ArgumentNotNull(testAssemblyPath);
Guard.ArgumentNotNull(settings);
Guard.ArgumentValid(File.Exists(testAssemblyPath), "Framework driver called with a file name that doesn't exist.", nameof(testAssemblyPath));
log.Info($"Loading {testAssemblyPath} - see separate log file");
_testAssemblyPath = Path.GetFullPath(testAssemblyPath);
var idPrefix = _driverId + "-";
bool useDefaultAssemblyLoadContext = false;
if (settings.TryGetValue(SettingDefinitions.UseDefaultAssemblyLoadContext, out var val))
useDefaultAssemblyLoadContext = (bool)val;
#if NETFRAMEWORK
try
{
log.Debug("Creating FrameworkController");
_frameworkController = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(
_nunitRef.FullName,
CONTROLLER_TYPE,
false,
0,
null,
new object[] { _testAssemblyPath, idPrefix, settings },
null,
null).ShouldNotBeNull();
log.Debug("Created FrameworkController");
}
catch (Exception ex)
{
log.Debug($"Got Exception {ex}");
string msg = $"Failed to load {_nunitRef.FullName}\r\n Codebase: {_nunitRef.CodeBase}";
throw new Exception(msg, ex);
}
_frameworkControllerType = _frameworkController?.GetType();
log.Debug($"Created FrameworkController {_frameworkControllerType?.Name}");
var controllerAssembly = _frameworkControllerType?.Assembly?.GetName();
log.Debug($"Controller assembly is {controllerAssembly}");
#else
try
{
_testAssembly = AssemblyHelper.FindLoadedAssemblyByPath(_testAssemblyPath);
if (_testAssembly is not null)
{
_assemblyLoadContext = AssemblyLoadContext.GetLoadContext(_testAssembly);
log.Debug($" Already loaded in context {_assemblyLoadContext}");
}
else
{
_assemblyLoadContext = useDefaultAssemblyLoadContext
? AssemblyLoadContext.Default
: new AssemblyLoadContext(Path.GetFileNameWithoutExtension(testAssemblyPath));
_testAssembly = _assemblyLoadContext.LoadFromAssemblyPath(testAssemblyPath);
log.Debug($" Loaded into new context {_assemblyLoadContext}");
}
_testAssemblyResolver = new TestAssemblyResolver(_assemblyLoadContext.ShouldNotBeNull(), testAssemblyPath);
}
catch (Exception e)
{
var msg = $"Failed to load test assembly {testAssemblyPath}";
log.Error(msg);
throw new NUnitEngineException(msg, e);
}
log.Debug($"Loaded {testAssemblyPath}");
try
{
_frameworkAssembly = LoadAssembly(_nunitRef);
}
catch (Exception e)
{
log.Error($"{FAILED_TO_LOAD_NUNIT}\r\n{e}");
throw new NUnitEngineException(FAILED_TO_LOAD_NUNIT, e);
}
log.Debug("Loaded nunit.framework");
_frameworkController = CreateInstance(CONTROLLER_TYPE, _testAssembly, idPrefix, settings);
if (_frameworkController is null)
{
log.Error(INVALID_FRAMEWORK_MESSAGE);
throw new NUnitEngineException(INVALID_FRAMEWORK_MESSAGE);
}
#endif
_frameworkControllerType = _frameworkController?.GetType();
log.Debug($"Created FrameworkController {_frameworkControllerType?.Name}");
log.Debug($"Loaded {testAssemblyPath}");
return (string)ExecuteMethod(LOAD_METHOD);
}
public int CountTestCases(string filter)
{
CheckLoadWasCalled();
object? count = ExecuteMethod(COUNT_METHOD, filter);
return count is not null ? (int)count : 0;
}
public string Run(ITestEventListener? listener, string filter)
{
CheckLoadWasCalled();
log.Info("Running {0} - see separate log file", Path.GetFileName(_testAssemblyPath.ShouldNotBeNull()));
Action<string>? callback = listener is not null ? listener.OnTestEvent : null;
return (string)ExecuteMethod(RUN_METHOD, [typeof(Action<string>), typeof(string)], callback, filter);
}
public void RunAsync(Action<string>? callback, string filter)
{
CheckLoadWasCalled();
log.Info("Running {0} - see separate log file", Path.GetFileName(_testAssemblyPath.ShouldNotBeNull()));
ExecuteMethod(RUN_ASYNC_METHOD, [typeof(Action<string>), typeof(string)], callback, filter);
}
public void RequestStop()
{
ExecuteMethod(STOP_RUN_METHOD, false);
}
public void ForcedStop()
{
ExecuteMethod(STOP_RUN_METHOD, true);
}
public bool ForcedStopSupported => _nunitRef.Version.ShouldNotBeNull().Major is 3 or 4;
public string Explore(string filter)
{
CheckLoadWasCalled();
log.Info("Exploring {0} - see separate log file", Path.GetFileName(_testAssemblyPath.ShouldNotBeNull()));
return (string)ExecuteMethod(EXPLORE_METHOD, filter);
}
private void CheckLoadWasCalled()
{
if (_frameworkController is null)
throw new InvalidOperationException(LOAD_MESSAGE);
}
#if NETCOREAPP
private object CreateInstance(string typeName, params object?[]? args)
{
var type = _frameworkAssembly.ShouldNotBeNull().GetType(typeName, throwOnError: true)!;
return Activator.CreateInstance(type, args)!;
}
private Assembly LoadAssembly(string assemblyPath)
{
Assembly assembly;
try
{
assembly = _assemblyLoadContext?.LoadFromAssemblyPath(assemblyPath)!;
if (assembly is null)
throw new Exception("LoadFromAssemblyPath returned null");
}
catch (Exception e)
{
var msg = string.Format(FAILED_TO_LOAD_ASSEMBLY + assemblyPath);
log.Error(msg);
throw new NUnitEngineException(msg, e);
}
log.Debug($"Loaded {assemblyPath}");
return assembly;
}
private Assembly LoadAssembly(AssemblyName assemblyName)
{
Assembly assembly;
try
{
assembly = _assemblyLoadContext?.LoadFromAssemblyName(assemblyName)!;
if (assembly is null)
throw new Exception("LoadFromAssemblyName returned null");
}
catch (Exception e)
{
log.Error($"{FAILED_TO_LOAD_ASSEMBLY}\r\n{e}");
throw new NUnitEngineException(FAILED_TO_LOAD_NUNIT, e);
}
log.Debug($"Loaded {assemblyName.FullName}");
return assembly;
}
#endif
// API methods with no overloads
private static readonly string LOAD_METHOD = "LoadTests";
private static readonly string EXPLORE_METHOD = "ExploreTests";
private static readonly string COUNT_METHOD = "CountTests";
private static readonly string STOP_RUN_METHOD = "StopRun";
// Execute methods with no overloads
private object ExecuteMethod(string methodName, params object?[] args)
{
log.Debug($"Calling API method {methodName} with args {string.Join("+", args)}");
var method = _frameworkControllerType.ShouldNotBeNull().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
return ExecuteMethod(method, args);
}
// API methods with overloads
private static readonly string RUN_METHOD = "RunTests";
// Framework RunAsync method is not public
private static readonly string RUN_ASYNC_METHOD = "RunTests";
// Execute overloaded methods specifying argument types
private object ExecuteMethod(string methodName, Type[] ptypes, params object?[] args)
{
log.Debug($"Calling API method {methodName} with arg types {string.Join<Type>("+", ptypes)}");
var method = _frameworkControllerType.ShouldNotBeNull().GetMethod(methodName, ptypes);
return ExecuteMethod(method, args);
}
private object ExecuteMethod(MethodInfo? method, params object?[] args)
{
if (method is null)
throw new NUnitEngineException(INVALID_FRAMEWORK_MESSAGE);
log.Debug($"Executing {method.DeclaringType}.{method.Name}");
#if NETFRAMEWORK
return method.Invoke(_frameworkController, args).ShouldNotBeNull();
#else
//using (_assemblyLoadContext.ShouldNotBeNull().EnterContextualReflection())
//{
return method.Invoke(_frameworkController, args).ShouldNotBeNull();
//}
#endif
}
#if NETFRAMEWORK
public override object InitializeLifetimeService()
{
return null!;
}
#endif
}
}