Skip to content

Commit 3bfe696

Browse files
committed
Fix problems in assemlby loading
1 parent 008f693 commit 3bfe696

File tree

5 files changed

+26
-34
lines changed

5 files changed

+26
-34
lines changed

src/NUnitEngine/nunit.engine.core.tests/DotNetHelperTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void CanParseInputLine(string line, string name, string packageVer
6363
[TestCase("Microsoft.NETCore.App", "8.0.0", "8.0.22")]
6464
[TestCase("Microsoft.NETCore.App", "8.0.0.0", "8.0.22")]
6565
[TestCase("Microsoft.NETCore.App", "8.0.0.100", "8.0.22")]
66-
[TestCase("Microsoft.NETCore.App", "8.0.100", "9.0.11")]
66+
[TestCase("Microsoft.NETCore.App", "8.0.100", "8.0.22")]
6767
[TestCase("Microsoft.AspNetCore.App", "5.0.0", "8.0.22")] // Rather than 8.0.2
6868
[TestCase("Microsoft.AspNetCore.App", "7.0.0", "8.0.22")] // Rather than 8.0.2
6969
[TestCase("Microsoft.AspNetCore.App", "8.0.0", "8.0.22")] // Rather than 8.0.2

src/NUnitEngine/nunit.engine.core/DotNetHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ internal static bool FindBestRuntime(Version targetVersion, IEnumerable<RuntimeI
153153

154154
foreach (var candidate in availableRuntimes)
155155
{
156-
if (candidate.Version >= targetVersion)
156+
if (candidate.Version.Major > targetVersion.Major ||
157+
candidate.Version.Major == targetVersion.Major && candidate.Version.Minor >= candidate.Version.Minor)
157158
if (bestRuntime is null || candidate.Version.Major == bestRuntime.Version.Major)
158159
bestRuntime = candidate;
159160
}

src/NUnitEngine/nunit.engine.core/Drivers/NUnitNetCore31Driver.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22

3+
//#define USE_DEFAULT_ASSEMBLY_LOAD_CONTEXT
4+
35
#if NETCOREAPP3_1_OR_GREATER
46
using System;
57
using System.Linq;
@@ -66,11 +68,19 @@ public string Load(string assemblyPath, IDictionary<string, object> settings)
6668
_testAssembly = AssemblyHelper.FindLoadedAssemblyByPath(assemblyPath);
6769

6870
if (_testAssembly != null)
71+
{
6972
_assemblyLoadContext = AssemblyLoadContext.GetLoadContext(_testAssembly);
73+
log.Debug($" Already loaded in context {_assemblyLoadContext}");
74+
}
7075
else
7176
{
77+
#if USE_DEFAULT_ASSEMBLY_LOAD_CONTEXT
78+
_assemblyLoadContext = AssemblyLoadContext.Default;
79+
#else
7280
_assemblyLoadContext = new AssemblyLoadContext(Path.GetFileNameWithoutExtension(assemblyPath));
81+
#endif
7382
_testAssembly = _assemblyLoadContext.LoadFromAssemblyPath(assemblyPath);
83+
log.Debug($" Loaded into new context {_assemblyLoadContext}");
7484
}
7585

7686
_testAssemblyResolver = new TestAssemblyResolver(_assemblyLoadContext, assemblyPath);
@@ -115,11 +125,6 @@ public string Load(string assemblyPath, IDictionary<string, object> settings)
115125
return ExecuteMethod(LOAD_METHOD) as string;
116126
}
117127

118-
private Assembly _assemblyLoadContext_Resolving(AssemblyLoadContext arg1, AssemblyName arg2)
119-
{
120-
throw new NotImplementedException();
121-
}
122-
123128
/// <summary>
124129
/// Counts the number of test cases for the loaded test assembly
125130
/// </summary>

src/NUnitEngine/nunit.engine.core/Internal/AssemblyHelper.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22

33
using System;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
67
using System.Reflection;
@@ -86,14 +87,5 @@ public static Assembly FindLoadedAssemblyByPath(string assemblyPath)
8687
!string.IsNullOrEmpty(a.Location) &&
8788
StringComparer.OrdinalIgnoreCase.Equals(Path.GetFullPath(a.Location), full));
8889
}
89-
90-
public static Assembly FindLoadedAssemblyByName(AssemblyName assemblyName)
91-
{
92-
return AppDomain.CurrentDomain.GetAssemblies()
93-
.FirstOrDefault(a =>
94-
!a.IsDynamic &&
95-
!string.IsNullOrEmpty(a.Location) &&
96-
a.GetName() == assemblyName);
97-
}
9890
}
9991
}

src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.DependencyModel.Resolution;
77
using System;
88
using System.Collections.Generic;
9+
using System.Diagnostics;
910
using System.IO;
1011
using System.Linq;
1112
using System.Reflection;
@@ -52,7 +53,7 @@ private void InitializeResolutionStrategies(AssemblyLoadContext loadContext, str
5253
foreach (var reference in assemblyDef.MainModule.GetTypeReferences())
5354
{
5455
string fn = reference.FullName;
55-
if (fn.StartsWith("System.Windows.") || fn.StartsWith("PresentationFramework"))
56+
if (fn.StartsWith("System.Windows.") || fn.StartsWith("PresentationFramework") || fn == "WindowsBase")
5657
tryWindowsDesktopFirst = true;
5758
if (fn.StartsWith("Microsoft.AspNetCore."))
5859
tryAspNetCoreFirst = true;
@@ -81,25 +82,18 @@ public void Dispose()
8182
_loadContext.Resolving -= OnResolving;
8283
}
8384

84-
//public Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
85-
//{
86-
// return OnResolving(context, assemblyName);
87-
//}
88-
8985
private Assembly OnResolving(AssemblyLoadContext loadContext, AssemblyName assemblyName)
9086
{
91-
if (loadContext == null) throw new ArgumentNullException("context");
92-
93-
//var runtimeResolverPath = _assemblyDependencyResolver.ResolveAssemblyToPath(assemblyName);
94-
//if (!string.IsNullOrEmpty(runtimeResolverPath) && File.Exists(runtimeResolverPath))
95-
//{
96-
// var loadedAssembly = _loadContext.LoadFromAssemblyPath(runtimeResolverPath);
97-
// if (loadedAssembly != null)
98-
// {
99-
// log.Info($"Assembly {assemblyName} ({loadedAssembly}) is loaded using the deps.json info");
100-
// return loadedAssembly;
101-
// }
102-
//}
87+
var runtimeResolverPath = _assemblyDependencyResolver.ResolveAssemblyToPath(assemblyName);
88+
if (!string.IsNullOrEmpty(runtimeResolverPath) && File.Exists(runtimeResolverPath))
89+
{
90+
var loadedAssembly = _loadContext.LoadFromAssemblyPath(runtimeResolverPath);
91+
if (loadedAssembly != null)
92+
{
93+
log.Info($"Assembly {assemblyName} ({loadedAssembly}) is loaded using the deps.json info");
94+
return loadedAssembly;
95+
}
96+
}
10397

10498
foreach (var strategy in ResolutionStrategies)
10599
if (strategy.TryToResolve(loadContext, assemblyName, out Assembly loadedAssembly))

0 commit comments

Comments
 (0)