#include "comhost_test.h"
#include <hostfxr.h>
#include "host_context_test.h"
+#include <utils.h>
namespace
{
const pal::char_t *command = argv[1];
if (pal::strcmp(command, _X("get_hostfxr_path")) == 0)
{
- // args: ... [<assembly_path>] [<hostfxr_to_load>]
- const pal::char_t *assembly_path = nullptr;
+ // args: ... [<explicit_load>] [<assembly_path>] [<hostfxr_to_load>]
+ bool explicit_load = false;
if (argc >= 3)
- assembly_path = argv[2];
+ explicit_load = pal::strcmp(pal::to_lower(pal::string_t{argv[2]}).c_str(), _X("true")) == 0;
+ const pal::char_t *assembly_path = nullptr;
if (argc >= 4)
+ assembly_path = argv[3];
+
+ if (argc >= 5)
{
- pal::string_t to_load = argv[3];
+ pal::string_t to_load = argv[4];
pal::dll_t fxr;
if (!pal::load_library(&to_load, &fxr))
{
}
}
+ decltype(&get_hostfxr_path) get_hostfxr_path_fn;
+ if (explicit_load)
+ {
+ pal::string_t nethost_path;
+ if (!pal::get_own_executable_path(&nethost_path) || !pal::realpath(&nethost_path))
+ {
+ std::cout << "Failed to get path to current executable" << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ nethost_path = get_directory(nethost_path);
+ nethost_path.append(MAKE_LIBNAME("nethost"));
+
+ pal::dll_t nethost;
+ if (!pal::load_library(&nethost_path, &nethost))
+ {
+ std::cout << "Failed to load library: " << tostr(nethost_path).data() << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ get_hostfxr_path_fn = (decltype(get_hostfxr_path_fn))pal::get_symbol(nethost, "get_hostfxr_path");
+ if (get_hostfxr_path_fn == nullptr)
+ {
+ std::cout << "Failed to get get_hostfxr_path export from nethost" << std::endl;
+ return EXIT_FAILURE;
+ }
+ }
+ else
+ {
+ get_hostfxr_path_fn = get_hostfxr_path;
+ }
+
pal::string_t fxr_path;
size_t len = fxr_path.size();
- int res = get_hostfxr_path(nullptr, &len, assembly_path);
+ int res = get_hostfxr_path_fn(nullptr, &len, assembly_path);
if (static_cast<StatusCode>(res) == StatusCode::HostApiBufferTooSmall)
{
fxr_path.resize(len);
- res = get_hostfxr_path(&fxr_path[0], &len, assembly_path);
+ res = get_hostfxr_path_fn(&fxr_path[0], &len, assembly_path);
}
if (static_cast<StatusCode>(res) == StatusCode::Success)
}
[Theory]
- [InlineData(false, true)]
- [InlineData(false, false)]
- [InlineData(true, true)]
- [InlineData(true, false)]
- public void GetHostFxrPath_DotNetRootEnvironment(bool useAssemblyPath, bool isValid)
+ [InlineData(true, false, true)]
+ [InlineData(true, false, false)]
+ [InlineData(true, true, true)]
+ [InlineData(true, true, false)]
+ [InlineData(false, false, true)]
+ [InlineData(false, false, false)]
+ [InlineData(false, true, true)]
+ [InlineData(false, true, false)]
+ public void GetHostFxrPath_DotNetRootEnvironment(bool explicitLoad, bool useAssemblyPath, bool isValid)
{
string dotNetRoot = isValid ? Path.Combine(sharedState.ValidInstallRoot, "dotnet") : sharedState.InvalidInstallRoot;
- CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
+ CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
.CaptureStdErr()
.CaptureStdOut()
.EnvironmentVariable("COREHOST_TRACE", "1")
}
[Theory]
- [InlineData(false, true, false)]
- [InlineData(false, true, true)]
- [InlineData(false, false, false)]
- [InlineData(false, false, true)]
- [InlineData(true, true, false)]
- [InlineData(true, true, true)]
- [InlineData(true, false, false)]
- [InlineData(true, false, true)]
- public void GetHostFxrPath_GlobalInstallation(bool useAssemblyPath, bool useRegisteredLocation, bool isValid)
+ [InlineData(true, false, true, false)]
+ [InlineData(true, false, true, true)]
+ [InlineData(true, false, false, false)]
+ [InlineData(true, false, false, true)]
+ [InlineData(true, true, true, false)]
+ [InlineData(true, true, true, true)]
+ [InlineData(true, true, false, false)]
+ [InlineData(true, true, false, true)]
+ [InlineData(false, false, true, false)]
+ [InlineData(false, false, true, true)]
+ [InlineData(false, false, false, false)]
+ [InlineData(false, false, false, true)]
+ [InlineData(false, true, true, false)]
+ [InlineData(false, true, true, true)]
+ [InlineData(false, true, false, false)]
+ [InlineData(false, true, false, true)]
+ public void GetHostFxrPath_GlobalInstallation(bool explicitLoad, bool useAssemblyPath, bool useRegisteredLocation, bool isValid)
{
// Overide the registry key for self-registered global installs.
// If using the registered location, set the install location value to the valid/invalid root.
registeredInstallLocationOverride.SetInstallLocation(installLocation, sharedState.RepoDirectories.BuildArchitecture);
}
- result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
+ result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
.CaptureStdErr()
.CaptureStdOut()
.EnvironmentVariable("COREHOST_TRACE", "1")
}
}
- [Fact]
- public void GetHostFxrPath_WithAssemblyPath_AppLocalFxr()
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public void GetHostFxrPath_WithAssemblyPath_AppLocalFxr(bool explicitLoad)
{
string appLocalFxrDir = Path.Combine(sharedState.BaseDirectory, "appLocalFxr");
Directory.CreateDirectory(appLocalFxrDir);
File.WriteAllText(assemblyPath, string.Empty);
File.WriteAllText(hostFxrPath, string.Empty);
- Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {assemblyPath}")
+ Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {assemblyPath}")
.CaptureStdErr()
.CaptureStdOut()
.EnvironmentVariable("COREHOST_TRACE", "1")
[Fact]
public void GetHostFxrPath_HostFxrAlreadyLoaded()
{
- Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {sharedState.TestAssemblyPath} {sharedState.ProductHostFxrPath}")
+ Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} false {sharedState.TestAssemblyPath} {sharedState.ProductHostFxrPath}")
.CaptureStdErr()
.CaptureStdOut()
.EnvironmentVariable("COREHOST_TRACE", "1")