-
Notifications
You must be signed in to change notification settings - Fork 152
/
TestAssemblyResolver.cs
299 lines (250 loc) · 11.8 KB
/
TestAssemblyResolver.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
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
#if NETCOREAPP3_1_OR_GREATER
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using TestCentric.Metadata;
namespace NUnit.Engine.Internal
{
internal sealed class TestAssemblyResolver : IDisposable
{
private static readonly Logger log = InternalTrace.GetLogger(typeof(TestAssemblyResolver));
private readonly AssemblyLoadContext _loadContext;
private static readonly string INSTALL_DIR;
private static readonly string WINDOWS_DESKTOP_DIR;
private static readonly string ASP_NET_CORE_DIR;
// Our Strategies for resolving references
List<ResolutionStrategy> ResolutionStrategies;
static TestAssemblyResolver()
{
INSTALL_DIR = DotNet.GetInstallDirectory();
WINDOWS_DESKTOP_DIR = Path.Combine(INSTALL_DIR, "shared", "Microsoft.WindowsDesktop.App");
ASP_NET_CORE_DIR = Path.Combine(INSTALL_DIR, "shared", "Microsoft.AspNetCore.App");
}
public TestAssemblyResolver(AssemblyLoadContext loadContext, string testAssemblyPath)
{
_loadContext = loadContext;
InitializeResolutionStrategies(loadContext, testAssemblyPath);
_loadContext.Resolving += OnResolving;
}
private void InitializeResolutionStrategies(AssemblyLoadContext loadContext, string testAssemblyPath)
{
// First, looking only at direct references by the test assembly, try to determine if
// this assembly is using WindowsDesktop (either SWF or WPF) and/or AspNetCore.
AssemblyDefinition assemblyDef = AssemblyDefinition.ReadAssembly(testAssemblyPath);
bool isWindowsDesktop = false;
bool isAspNetCore = false;
foreach (var reference in assemblyDef.MainModule.GetTypeReferences())
{
string fn = reference.FullName;
if (fn.StartsWith("System.Windows.") || fn.StartsWith("PresentationFramework"))
isWindowsDesktop = true;
if (fn.StartsWith("Microsoft.AspNetCore."))
isAspNetCore = true;
}
// Initialize the list of ResolutionStrategies in the best order depending on
// what we learned.
ResolutionStrategies = new List<ResolutionStrategy>();
if (isWindowsDesktop && Directory.Exists(WINDOWS_DESKTOP_DIR))
ResolutionStrategies.Add(new AdditionalDirectoryStrategy(WINDOWS_DESKTOP_DIR));
if (isAspNetCore && Directory.Exists(ASP_NET_CORE_DIR))
ResolutionStrategies.Add(new AdditionalDirectoryStrategy(ASP_NET_CORE_DIR));
ResolutionStrategies.Add(new TrustedPlatformAssembliesStrategy());
ResolutionStrategies.Add(new RuntimeLibrariesStrategy(loadContext, testAssemblyPath));
if (!isWindowsDesktop && Directory.Exists(WINDOWS_DESKTOP_DIR))
ResolutionStrategies.Add(new AdditionalDirectoryStrategy(WINDOWS_DESKTOP_DIR));
if (!isAspNetCore && Directory.Exists(ASP_NET_CORE_DIR))
ResolutionStrategies.Add(new AdditionalDirectoryStrategy(ASP_NET_CORE_DIR));
}
public void Dispose()
{
_loadContext.Resolving -= OnResolving;
}
public Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
{
return OnResolving(context, assemblyName);
}
private Assembly OnResolving(AssemblyLoadContext loadContext, AssemblyName assemblyName)
{
if (loadContext == null) throw new ArgumentNullException("context");
Assembly loadedAssembly;
foreach (var strategy in ResolutionStrategies)
if (strategy.TryToResolve(loadContext, assemblyName, out loadedAssembly))
return loadedAssembly;
log.Info("Cannot resolve assembly '{0}'", assemblyName);
return null;
}
#region Nested ResolutionStrategy Classes
public abstract class ResolutionStrategy
{
public abstract bool TryToResolve(
AssemblyLoadContext loadContext, AssemblyName assemblyName, out Assembly loadedAssembly);
}
public class TrustedPlatformAssembliesStrategy : ResolutionStrategy
{
public override bool TryToResolve(
AssemblyLoadContext loadContext, AssemblyName assemblyName, out Assembly loadedAssembly)
{
return TryLoadFromTrustedPlatformAssemblies(loadContext, assemblyName, out loadedAssembly);
}
private static bool TryLoadFromTrustedPlatformAssemblies(
AssemblyLoadContext loadContext, AssemblyName assemblyName, out Assembly loadedAssembly)
{
// https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/default-probing
loadedAssembly = null;
var trustedAssemblies = System.AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") as string;
if (string.IsNullOrEmpty(trustedAssemblies))
{
return false;
}
var separator = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ";" : ":";
foreach (var assemblyPath in trustedAssemblies.Split(separator))
{
var fileName = Path.GetFileNameWithoutExtension(assemblyPath);
if (FileMatchesAssembly(fileName) && File.Exists(assemblyPath))
{
loadedAssembly = loadContext.LoadFromAssemblyPath(assemblyPath);
log.Info("'{0}' assembly is loaded from trusted path '{1}'", assemblyPath, loadedAssembly.Location);
return true;
}
}
return false;
bool FileMatchesAssembly(string fileName) =>
string.Equals(fileName, assemblyName.Name, StringComparison.InvariantCultureIgnoreCase);
}
}
public class RuntimeLibrariesStrategy : ResolutionStrategy
{
private DependencyContext _dependencyContext;
private readonly ICompilationAssemblyResolver _assemblyResolver;
public RuntimeLibrariesStrategy(AssemblyLoadContext loadContext, string testAssemblyPath)
{
_dependencyContext = DependencyContext.Load(loadContext.LoadFromAssemblyPath(testAssemblyPath));
_assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
{
new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(testAssemblyPath)),
new ReferenceAssemblyPathResolver(),
new PackageCompilationAssemblyResolver()
});
}
public override bool TryToResolve(
AssemblyLoadContext loadContext, AssemblyName assemblyName, out Assembly loadedAssembly)
{
foreach (var library in _dependencyContext.RuntimeLibraries)
{
var wrapper = new CompilationLibrary(
library.Type,
library.Name,
library.Version,
library.Hash,
library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
library.Dependencies,
library.Serviceable);
var assemblies = new List<string>();
_assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies);
foreach (var assemblyPath in assemblies)
{
if (assemblyName.Name == Path.GetFileNameWithoutExtension(assemblyPath))
{
loadedAssembly = loadContext.LoadFromAssemblyPath(assemblyPath);
log.Info("'{0}' ({1}) assembly is loaded from runtime libraries {2} dependencies",
assemblyName,
loadedAssembly.Location,
library.Name);
return true;
}
}
}
loadedAssembly = null;
return false;
}
}
public class AdditionalDirectoryStrategy : ResolutionStrategy
{
private string _frameworkDirectory;
public AdditionalDirectoryStrategy(string frameworkDirectory)
{
_frameworkDirectory = frameworkDirectory;
}
public override bool TryToResolve(
AssemblyLoadContext loadContext, AssemblyName assemblyName, out Assembly loadedAssembly)
{
loadedAssembly = null;
if (assemblyName.Version == null)
return false;
var versionDir = FindBestVersionDir(_frameworkDirectory, assemblyName.Version);
if (versionDir != null)
{
string candidate = Path.Combine(_frameworkDirectory, versionDir, assemblyName.Name + ".dll");
if (File.Exists(candidate))
{
loadedAssembly = loadContext.LoadFromAssemblyPath(candidate);
log.Info("'{0}' ({1}) assembly is loaded from AdditionalFrameworkDirectory {2} dependencies with best candidate version {3}",
assemblyName,
loadedAssembly.Location,
_frameworkDirectory,
versionDir);
return true;
}
else
{
log.Debug("Best version dir for {0} is {1}, but there is no {2} file", _frameworkDirectory, versionDir, candidate);
return false;
}
}
return false;
}
}
#endregion
#region HelperMethods
private static string FindBestVersionDir(string libraryDir, Version targetVersion)
{
string target = targetVersion.ToString();
Version bestVersion = new Version(0, 0);
foreach (var subdir in Directory.GetDirectories(libraryDir))
{
Version version;
if (TryGetVersionFromString(Path.GetFileName(subdir), out version))
{
if (version >= targetVersion)
if (bestVersion.Major == 0 || bestVersion > version)
bestVersion = version;
}
}
return bestVersion.Major > 0
? bestVersion.ToString()
: null;
}
private static bool TryGetVersionFromString(string text, out Version newVersion)
{
const string VERSION_CHARS = ".0123456789";
int len = 0;
foreach (char c in text)
{
if (VERSION_CHARS.IndexOf(c) >= 0)
len++;
else
break;
}
try
{
newVersion = new Version(text.Substring(0, len));
return true;
}
catch
{
newVersion = new Version();
return false;
}
}
#endregion
}
}
#endif