Skip to content

Commit

Permalink
Merge pull request #1285 from nunit/issue-1274
Browse files Browse the repository at this point in the history
Resolve WindowsDesktop and AspNetCore tests correctly
  • Loading branch information
CharliePoole authored Jan 4, 2023
2 parents 4a32d54 + 9c42495 commit ef2055c
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 14 deletions.
1 change: 0 additions & 1 deletion NUnitConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "cake", "cake", "{9BCA00E2-D
cake\header-check.cake = cake\header-check.cake
cake\package-checks.cake = cake\package-checks.cake
cake\package-definitions.cake = cake\package-definitions.cake
cake\package-tests.cake = cake\package-tests.cake
cake\packages.cake = cake\packages.cake
cake\test-results.cake = cake\test-results.cake
cake\utilities.cake = cake\utilities.cake
Expand Down
1 change: 0 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ static string Target; Target = Argument("target", Argument("t", "Default"));
#load cake/header-check.cake
#load cake/package-checks.cake
#load cake/test-results.cake
#load cake/package-tests.cake
#load cake/versioning.cake
#load cake/utilities.cake
#load cake/package-definitions.cake
Expand Down
3 changes: 3 additions & 0 deletions cake/build-settings.cake
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public class BuildSettings
Net50PlusNet60Test,
Net60AspNetCoreTest
};
if (IsRunningOnWindows)
NetCoreRunnerTests.Add(Net60WindowsFormsTest);

AllPackages = new PackageDefinition[] {
ConsoleNuGetPackage = new NUnitConsoleNuGetPackage(this),
ConsoleRunnerNuGetPackage = new NUnitConsoleRunnerNuGetPackage(this),
Expand Down
1 change: 0 additions & 1 deletion cake/package-tests.cake

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
<ApplicationIcon>..\..\..\nunit.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<PropertyGroup>
<Product>NUnit Console</Product>
<AssemblyTitle>NUnit Console Runner ($(TargetFramework))</AssemblyTitle>
Expand Down
5 changes: 0 additions & 5 deletions src/NUnitEngine/nunit-agent/nunit-agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
<RunPostBuildEvent>Always</RunPostBuildEvent>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'!='net20' and '$(TargetFramework)'!='net462'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

<PropertyGroup>
<Product>NUnit Engine</Product>
<AssemblyTitle>NUnit Agent ($(TargetFramework))</AssemblyTitle>
Expand Down
84 changes: 82 additions & 2 deletions src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using Microsoft.Win32;

namespace NUnit.Engine.Internal
{
Expand All @@ -18,13 +20,25 @@ internal sealed class TestAssemblyResolver : IDisposable
private readonly ICompilationAssemblyResolver _assemblyResolver;
private readonly DependencyContext _dependencyContext;
private readonly AssemblyLoadContext _loadContext;
private readonly string _basePath;

private static readonly string INSTALL_DIR = GetDotNetInstallDirectory();
private static readonly string WINDOWS_DESKTOP_DIR = Path.Combine(INSTALL_DIR, "shared", "Microsoft.WindowsDesktop.App");
private static readonly string ASP_NET_CORE_DIR = Path.Combine(INSTALL_DIR, "shared", "Microsoft.AspNetCore.App");
private static readonly List<string> AdditionalFrameworkDirectories;

static TestAssemblyResolver()
{
AdditionalFrameworkDirectories = new List<string>();
if (Directory.Exists(WINDOWS_DESKTOP_DIR))
AdditionalFrameworkDirectories.Add(WINDOWS_DESKTOP_DIR);
if (Directory.Exists(ASP_NET_CORE_DIR))
AdditionalFrameworkDirectories.Add(ASP_NET_CORE_DIR);
}

public TestAssemblyResolver(AssemblyLoadContext loadContext, string assemblyPath)
{
_loadContext = loadContext;
_dependencyContext = DependencyContext.Load(loadContext.LoadFromAssemblyPath(assemblyPath));
_basePath = Path.GetDirectoryName(assemblyPath);

_assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
{
Expand Down Expand Up @@ -64,8 +78,74 @@ private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
}
}

foreach(string frameworkDirectory in AdditionalFrameworkDirectories)
{
var versionDir = FindBestVersionDir(frameworkDirectory, name.Version);
if (versionDir != null)
{
string candidate = Path.Combine(frameworkDirectory, versionDir, name.Name + ".dll");
if (File.Exists(candidate))
return _loadContext.LoadFromAssemblyPath(candidate);
}
}

return null;
}

private static string GetDotNetInstallDirectory()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Running on Windows so use registry
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\dotnet\SetUp\InstalledVersions\x64\sharedHost\");
return (string)key?.GetValue("Path");
}
else
return "/usr/shared/dotnet/";
}

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;
}
}
}
}
#endif

0 comments on commit ef2055c

Please sign in to comment.