Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port issue 1381 to V4 #1571

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/NUnitEngine/nunit.engine.core/DotNet.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 @@ -30,7 +30,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 @@ -168,27 +168,6 @@ private static bool TryLoadFromTrustedPlatformAssemblies(AssemblyLoadContext con
return false;
}

private static string GetDotNetInstallDirectory()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
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 // For now, we assume linux with a fixed path
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 @@ -36,9 +36,9 @@ public static IEnumerable<RuntimeFramework> FindRuntimes(bool forX86)
}
}

private static IEnumerable<string> GetRuntimeDirectories(bool isX86)
private static IEnumerable<string> GetRuntimeDirectories(bool x86)
{
string installDir = GetDotNetInstallDirectory(isX86);
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 isX86)
{
if (Path.DirectorySeparatorChar == '\\')
{
if (isX86)
{
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
Loading