Skip to content

Commit

Permalink
Honor DOTNET_ROOT environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Oct 5, 2024
1 parent 79cb823 commit f05b3d9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 44 deletions.
66 changes: 66 additions & 0 deletions src/NUnitEngine/nunit.engine.core/DotNetHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using Microsoft.Win32;
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace NUnit.Engine
{
public static class DotNet
{
public static string GetInstallDirectory() => Environment.Is64BitProcess
? GetX64InstallDirectory() : GetX86InstallDirectory();

public static string GetInstallDirectory(bool x86) => x86
? GetX86InstallDirectory() : GetX64InstallDirectory();

private static string _x64InstallDirectory;
public static string GetX64InstallDirectory()
{
if (_x64InstallDirectory == null)
_x64InstallDirectory = Environment.GetEnvironmentVariable("DOTNET_ROOT");

if (_x64InstallDirectory == null)
{
#if NETFRAMEWORK
if (Path.DirectorySeparatorChar == '\\')
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\dotnet\SetUp\InstalledVersions\x64\sharedHost\");
_x64InstallDirectory = (string)key?.GetValue("Path");
}
else
_x64InstallDirectory = "/usr/shared/dotnet/";
}

return _x64InstallDirectory;
}

private static string _x86InstallDirectory;
public static string GetX86InstallDirectory()
{
if (_x86InstallDirectory == null)
_x86InstallDirectory = Environment.GetEnvironmentVariable("DOTNET_ROOT_X86");

if (_x86InstallDirectory == null)
{
#if NETFRAMEWORK
if (Path.DirectorySeparatorChar == '\\')
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\dotnet\SetUp\InstalledVersions\x86\");
_x86InstallDirectory = (string)key?.GetValue("InstallLocation");
}
else
_x86InstallDirectory = "/usr/shared/dotnet/";
}

return _x86InstallDirectory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal sealed class TestAssemblyResolver : IDisposable

static TestAssemblyResolver()
{
INSTALL_DIR = GetDotNetInstallDirectory();
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");
}
Expand Down Expand Up @@ -248,26 +248,6 @@ public override bool TryToResolve(

#region HelperMethods

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

private static string FindBestVersionDir(string libraryDir, Version targetVersion)
{
string target = targetVersion.ToString();
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine/Services/AgentProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public AgentProcess(TestAgency agency, TestPackage package, Guid agentId)
if (Path.DirectorySeparatorChar != '\\')
throw new Exception("Running .NET Core as X86 is currently only supported on Windows");

var x86_dotnet_exe = @"C:\Program Files (x86)\dotnet\dotnet.exe";
var x86_dotnet_exe = Path.Combine(DotNet.GetX86InstallDirectory(), "dotnet.exe");
if (!File.Exists(x86_dotnet_exe))
throw new Exception("The X86 version of dotnet.exe is not installed");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static IEnumerable<RuntimeFramework> FindRuntimes(bool x86)

private static IEnumerable<string> GetRuntimeDirectories(bool x86)
{
string installDir = GetDotNetInstallDirectory(x86);
string installDir = DotNet.GetInstallDirectory(x86);

if (installDir != null && Directory.Exists(installDir) &&
File.Exists(Path.Combine(installDir, "dotnet.exe")))
Expand Down Expand Up @@ -110,27 +110,6 @@ private static bool TryGetVersionFromString(string text, out Version newVersion)
return false;
}
}

internal static string GetDotNetInstallDirectory(bool x86)
{
if (Path.DirectorySeparatorChar == '\\')
{
if (x86)
{
RegistryKey key =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\dotnet\SetUp\InstalledVersions\x86\");
return (string)key?.GetValue("InstallLocation");
}
else
{
RegistryKey key =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\dotnet\SetUp\InstalledVersions\x64\sharedHost\");
return (string)key?.GetValue("Path");
}
}
else
return "/usr/shared/dotnet/";
}
}
}
#endif

0 comments on commit f05b3d9

Please sign in to comment.