Skip to content

Commit e8abaea

Browse files
authored
Merge pull request #1799 from nunit/issue-1795
Eliminate use of our custom AssemblyLoadContext
2 parents 6b31f05 + 6572a7b commit e8abaea

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

NetCoreTests.nunit

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<NUnitProject>
22
<Settings processModel="Default" domainUsage="Default" />
33
<Config name="Debug" appbase="bin/Debug">
4-
<assembly path="testdata/net6.0/mock-assembly.dll" />
54
<assembly path="testdata/net8.0/mock-assembly.dll" />
65
</Config>
76
<Config name="Release" appbase="bin/Release">
8-
<assembly path="testdata/net6.0/mock-assembly.dll" />
97
<assembly path="testdata/net8.0/mock-assembly.dll" />
108
</Config>
119
</NUnitProject>

package-tests.cake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ public static class PackageTests
261261
AllLists.Add(new PackageTest(1, "Net60WPFTest")
262262
{
263263
Description = "Run test using WPF targeting .NET 6.0",
264-
Arguments = "testdata/net6.0-windows/WpfTest.dll",
264+
Arguments = "testdata/net6.0-windows/WpfTest.dll --trace:Debug",
265265
ExpectedResult = new ExpectedResult("Passed") { Assemblies = new[] { new ExpectedAssemblyResult("WpfTest.dll", "netcore-6.0") } }
266266
});
267267

268268
AllLists.Add(new PackageTest(1, "Net80WPFTest")
269269
{
270270
Description = "Run test using WPF targeting .NET 8.0",
271-
Arguments = "testdata/net8.0-windows/WpfTest.dll",
271+
Arguments = "testdata/net8.0-windows/WpfTest.dll --trace:Debug",
272272
ExpectedResult = new ExpectedResult("Passed") { Assemblies = new[] { new ExpectedAssemblyResult("WpfTest.dll", "netcore-8.0") } }
273273
});
274274

@@ -314,9 +314,9 @@ public static class PackageTests
314314

315315
NetCoreRunnerTests.Add(new PackageTest(1, "NUnitProjectTest")
316316
{
317-
Description= "Run NUnit project with mock-assembly.dll targeting .NET 6.0 and 8.0",
317+
Description= "Run NUnit project with mock-assembly.dll targeting 8.0",
318318
Arguments="../../NetCoreTests.nunit --config=Release",
319-
ExpectedResult = new MockAssemblyExpectedResult("netcore-6.0", "netcore-8.0"),
319+
ExpectedResult = new MockAssemblyExpectedResult("netcore-8.0"),
320320
ExtensionsNeeded = new [] { Extensions.NUnitProjectLoader }
321321
});
322322

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NUnit.Engine.Internal;
99
using System.Reflection;
1010
using NUnit.Engine.Extensibility;
11+
using System.Runtime.Loader;
1112

1213
namespace NUnit.Engine.Drivers
1314
{
@@ -38,7 +39,8 @@ public class NUnitNetCore31Driver : IFrameworkDriver
3839
Assembly _frameworkAssembly;
3940
object _frameworkController;
4041
Type _frameworkControllerType;
41-
TestAssemblyLoadContext _assemblyLoadContext;
42+
AssemblyLoadContext _assemblyLoadContext;
43+
TestAssemblyResolver _testAssemblyResolver;
4244

4345
/// <summary>
4446
/// An id prefix that will be passed to the test framework and used as part of the
@@ -54,11 +56,17 @@ public class NUnitNetCore31Driver : IFrameworkDriver
5456
/// <returns>An XML string representing the loaded test</returns>
5557
public string Load(string assemblyPath, IDictionary<string, object> settings)
5658
{
59+
//if (assemblyPath.EndsWith("WpfTest.dll"))
60+
// System.Diagnostics.Debugger.Launch();
61+
5762
log.Debug($"Loading {assemblyPath}");
5863
var idPrefix = string.IsNullOrEmpty(ID) ? "" : ID + "-";
5964

6065
assemblyPath = Path.GetFullPath(assemblyPath); //AssemblyLoadContext requires an absolute path
61-
_assemblyLoadContext = new TestAssemblyLoadContext(assemblyPath);
66+
//_assemblyLoadContext = new TestAssemblyLoadContext(assemblyPath);
67+
//_assemblyLoadContext = AssemblyLoadContext.Default;
68+
_assemblyLoadContext = new AssemblyLoadContext(assemblyPath);
69+
_testAssemblyResolver = new TestAssemblyResolver(_assemblyLoadContext, assemblyPath);
6270

6371
try
6472
{
@@ -104,6 +112,11 @@ public string Load(string assemblyPath, IDictionary<string, object> settings)
104112
return ExecuteMethod(LOAD_METHOD) as string;
105113
}
106114

115+
private Assembly _assemblyLoadContext_Resolving(AssemblyLoadContext arg1, AssemblyName arg2)
116+
{
117+
throw new NotImplementedException();
118+
}
119+
107120
/// <summary>
108121
/// Counts the number of test cases for the loaded test assembly
109122
/// </summary>

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

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

3-
#if NETCOREAPP3_1_OR_GREATER
3+
#if NETCOREAPP3_1_OR_GREATER && false
44

55
using System.Reflection;
66
using System.Runtime.InteropServices;

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Reflection;
1212
using System.Runtime.InteropServices;
1313
using System.Runtime.Loader;
14+
using System.Xml.Linq;
1415
using TestCentric.Metadata;
1516

1617
namespace NUnit.Engine.Internal
@@ -20,13 +21,20 @@ internal sealed class TestAssemblyResolver : IDisposable
2021
private static readonly Logger log = InternalTrace.GetLogger(typeof(TestAssemblyResolver));
2122

2223
private readonly AssemblyLoadContext _loadContext;
24+
private readonly string _basePath;
25+
private readonly AssemblyDependencyResolver _assemblyDependencyResolver;
2326

2427
// Our Strategies for resolving references
2528
List<ResolutionStrategy> ResolutionStrategies;
2629

2730
public TestAssemblyResolver(AssemblyLoadContext loadContext, string testAssemblyPath)
2831
{
2932
_loadContext = loadContext;
33+
_basePath = Path.GetDirectoryName(testAssemblyPath);
34+
_assemblyDependencyResolver = new AssemblyDependencyResolver(testAssemblyPath);
35+
#if NET8_0_OR_GREATER
36+
AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", _basePath);
37+
#endif
3038

3139
InitializeResolutionStrategies(loadContext, testAssemblyPath);
3240

@@ -73,19 +81,48 @@ public void Dispose()
7381
_loadContext.Resolving -= OnResolving;
7482
}
7583

76-
public Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
77-
{
78-
return OnResolving(context, assemblyName);
79-
}
84+
//public Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
85+
//{
86+
// return OnResolving(context, assemblyName);
87+
//}
8088

8189
private Assembly OnResolving(AssemblyLoadContext loadContext, AssemblyName assemblyName)
8290
{
8391
if (loadContext == null) throw new ArgumentNullException("context");
8492

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+
//}
103+
85104
foreach (var strategy in ResolutionStrategies)
86105
if (strategy.TryToResolve(loadContext, assemblyName, out Assembly loadedAssembly))
87106
return loadedAssembly;
88107

108+
// Load assemblies that are dependencies, and in the same folder as the test assembly,
109+
// but are not fully specified in test assembly deps.json file. This happens when the
110+
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
111+
// reference is a projectReference and has the same output directory as the parent.
112+
foreach (var extension in new string[] { ".dll", ".exe" })
113+
{
114+
string assemblyPath = Path.Combine(_basePath, assemblyName.Name + extension);
115+
if (File.Exists(assemblyPath))
116+
{
117+
var loadedAssembly = _loadContext.LoadFromAssemblyPath(assemblyPath);
118+
if (loadedAssembly != null)
119+
{
120+
log.Info($"Assembly {assemblyName.Name} ({assemblyPath}) is loaded using base path");
121+
return loadedAssembly;
122+
}
123+
}
124+
}
125+
89126
log.Info("Cannot resolve assembly '{0}'", assemblyName);
90127
return null;
91128
}

src/NUnitEngine/nunit.engine.core/Runners/DirectTestRunner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.IO;
67
using NUnit.Engine.Extensibility;
78
using NUnit.Engine.Internal;

0 commit comments

Comments
 (0)