Skip to content

Commit

Permalink
Fix tests on non-Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Apr 25, 2024
1 parent 5332dd9 commit c0c56c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/installer/tests/Assets/Projects/HostApiInvokerApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static void MainCore(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine(string.Join(Environment.NewLine, args));
if (args.Length == 0)
{
throw new Exception("Invalid number of arguments passed");
}

// If requested, test multilevel lookup using fake Global SDK directories:
// 1. using a fake ProgramFiles location
Expand All @@ -39,22 +43,30 @@ public static void MainCore(string[] args)
string testMultilevelLookupProgramFiles = Environment.GetEnvironmentVariable("TEST_MULTILEVEL_LOOKUP_PROGRAM_FILES");
string testMultilevelLookupSelfRegistered = Environment.GetEnvironmentVariable("TEST_MULTILEVEL_LOOKUP_SELF_REGISTERED");

string hostfxrPath;
if (testMultilevelLookupProgramFiles != null && testMultilevelLookupSelfRegistered != null)
{
Environment.SetEnvironmentVariable("_DOTNET_TEST_GLOBALLY_REGISTERED_PATH", testMultilevelLookupSelfRegistered);
Environment.SetEnvironmentVariable("ProgramFiles", testMultilevelLookupProgramFiles);
Environment.SetEnvironmentVariable("ProgramFiles(x86)", testMultilevelLookupProgramFiles);
Environment.SetEnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP", "1");
hostfxrPath = AppContext.GetData("HOSTFXR_PATH_TEST_BEHAVIOR") as string;
}
else
{
// never rely on machine state in test if we're not faking the multi-level lookup
Environment.SetEnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP", "0");
hostfxrPath = AppContext.GetData("HOSTFXR_PATH") as string;
}

if (args.Length == 0)
if (hostfxrPath is not null)
{
throw new Exception("Invalid number of arguments passed");
NativeLibrary.SetDllImportResolver(typeof(Program).Assembly, (libraryName, assembly, searchPath) =>
{
return libraryName == nameof(HostFXR.hostfxr)
? NativeLibrary.Load(libraryName, assembly, searchPath)
: default;
});
}

string apiToTest = args[0];
Expand Down
17 changes: 8 additions & 9 deletions src/installer/tests/HostActivation.Tests/NativeHostApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ public void Hostfxr_resolve_frameworks_for_runtime_config(bool isMissing)
result.Should()
.ReturnStatusCode(api, Constants.ErrorCode.Success)
.And.ReturnResolvedFramework(requested.Name, requested.Version, GetFrameworkPath(requested.Name, requested.Version, dotnet.BinPath));

}
}
}
Expand Down Expand Up @@ -784,14 +783,6 @@ public class SharedTestState : IDisposable

public SharedTestState()
{
HostApiInvokerApp = TestApp.CreateFromBuiltAssets("HostApiInvokerApp");

if (!OperatingSystem.IsWindows())
{
// On non-Windows, we can't just P/Invoke to already loaded hostfxr, so copy it next to the app dll.
File.Copy(Binaries.HostFxr.FilePath, Path.Combine(HostApiInvokerApp.Location, Binaries.HostFxr.FileName));
}

// Make a copy of the built .NET, as we will enable test-only behaviour
copiedDotnet = TestArtifact.CreateFromCopy(nameof(NativeHostApis), TestContext.BuiltDotNet.BinPath);
TestBehaviorEnabledDotNet = new DotNetCli(copiedDotnet.Location);
Expand All @@ -800,6 +791,14 @@ public SharedTestState()
// as we just delete the entire copy after the tests run.
_ = TestOnlyProductBehavior.Enable(TestBehaviorEnabledDotNet.GreatestVersionHostFxrFilePath);

HostApiInvokerApp = TestApp.CreateFromBuiltAssets("HostApiInvokerApp");

// On non-Windows, we can't just P/Invoke to already loaded hostfxr, so provide the app with
// paths to hostfxr so that it can handle resolving the library.
RuntimeConfig.FromFile(HostApiInvokerApp.RuntimeConfigJson)
.WithProperty("HOSTFXR_PATH", TestContext.BuiltDotNet.GreatestVersionHostFxrFilePath)
.WithProperty("HOSTFXR_PATH_TEST_BEHAVIOR", TestBehaviorEnabledDotNet.GreatestVersionHostFxrFilePath);

SdkAndFrameworkFixture = new SdkAndFrameworkFixture();
}

Expand Down

0 comments on commit c0c56c4

Please sign in to comment.