From c0bf8561def2811e1244d2d28e96fc90a15a2972 Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Mon, 30 Sep 2024 08:16:24 -0700 Subject: [PATCH] Set `AppContext.BaseDirectory` when running under .NET 8.0 --- NUnitConsole.sln | 9 ++++++++- package-tests.cake | 13 +++++++++++- .../nunit.engine.core.tests/AppContextTest.cs | 20 +++++++++++++++++++ .../Internal/TestAssemblyLoadContext.cs | 4 +++- src/TestData/AppContextTest/AppContextTest.cs | 20 +++++++++++++++++++ .../AppContextTest/AppContextTest.csproj | 16 +++++++++++++++ 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/NUnitEngine/nunit.engine.core.tests/AppContextTest.cs create mode 100644 src/TestData/AppContextTest/AppContextTest.cs create mode 100644 src/TestData/AppContextTest/AppContextTest.csproj diff --git a/NUnitConsole.sln b/NUnitConsole.sln index c197b9a6a..8e9350066 100644 --- a/NUnitConsole.sln +++ b/NUnitConsole.sln @@ -142,7 +142,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "notest-assembly", "src\Test EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfApp", "src\TestData\WpfApp\WpfApp.csproj", "{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvalidTestNames", "src\TestData\InvalidTestNames\InvalidTestNames.csproj", "{58E18ACC-1F7E-4395-817E-E7EF943E0C77}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InvalidTestNames", "src\TestData\InvalidTestNames\InvalidTestNames.csproj", "{58E18ACC-1F7E-4395-817E-E7EF943E0C77}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppContextTest", "src\TestData\AppContextTest\AppContextTest.csproj", "{E43A3E4B-B050-471B-B43C-0DF60FD44376}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -218,6 +220,10 @@ Global {58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Debug|Any CPU.Build.0 = Debug|Any CPU {58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Release|Any CPU.ActiveCfg = Release|Any CPU {58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Release|Any CPU.Build.0 = Release|Any CPU + {E43A3E4B-B050-471B-B43C-0DF60FD44376}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E43A3E4B-B050-471B-B43C-0DF60FD44376}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E43A3E4B-B050-471B-B43C-0DF60FD44376}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E43A3E4B-B050-471B-B43C-0DF60FD44376}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -251,6 +257,7 @@ Global {81E63A90-3191-4E99-92FF-01F9B1D3E3C5} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F} {6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F} {58E18ACC-1F7E-4395-817E-E7EF943E0C77} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F} + {E43A3E4B-B050-471B-B43C-0DF60FD44376} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D8E4FC26-5422-4C51-8BBC-D1AC0A578711} diff --git a/package-tests.cake b/package-tests.cake index bf0465e05..728891fd3 100644 --- a/package-tests.cake +++ b/package-tests.cake @@ -250,7 +250,9 @@ StandardRunnerTests.Add(new PackageTest( MockAssemblySolutionResult, KnownExtensions.VSProjectLoader.SetVersion("3.9.0"))); -// Special Cases +////////////////////////////////////////////////////////////////////// +// SPECIAL CASES +////////////////////////////////////////////////////////////////////// StandardRunnerTests.Add(new PackageTest( 1, "InvalidTestNameTest_Net462", @@ -287,3 +289,12 @@ AddToBothLists(new PackageTest( new ExpectedAssemblyResult("InvalidTestNames.dll", "netcore-8.0") } })); + +StandardRunnerTests.Add(new PackageTest( + 1, "AppContextBaseDirectory_NET80", + "Test Setting the BaseDirectory to match test assembly location under .NET 8.0", + "testdata/net8.0/AppContextTest.dll", + new ExpectedResult("Passed") + { + Assemblies = new ExpectedAssemblyResult[] { new ExpectedAssemblyResult("AppContextTest.dll", "netcore-8.0") } + })); diff --git a/src/NUnitEngine/nunit.engine.core.tests/AppContextTest.cs b/src/NUnitEngine/nunit.engine.core.tests/AppContextTest.cs new file mode 100644 index 000000000..e9b96d3ec --- /dev/null +++ b/src/NUnitEngine/nunit.engine.core.tests/AppContextTest.cs @@ -0,0 +1,20 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +using System; +using System.IO; +using NUnit.Framework; + +namespace NUnit.Engine.Core.Tests +{ + public class AppContextTest + { + [Test] + public void VerifyBasePath() + { + var thisAssembly = GetType().Assembly; + var expectedPath = Path.GetDirectoryName(GetType().Assembly.Location); + + Assert.That(AppContext.BaseDirectory, Is.SamePath(expectedPath)); + } + } +} diff --git a/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs b/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs index 66a218e47..3dd54cd52 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs @@ -24,6 +24,9 @@ public TestAssemblyLoadContext(string testAssemblyPath) _resolver = new TestAssemblyResolver(this, testAssemblyPath); _basePath = Path.GetDirectoryName(testAssemblyPath); _runtimeResolver = new AssemblyDependencyResolver(testAssemblyPath); +#if NET8_0_OR_GREATER + AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", _basePath); +#endif } protected override Assembly Load(AssemblyName name) @@ -37,7 +40,6 @@ protected override Assembly Load(AssemblyName name) return loadedAssembly; } - var runtimeResolverPath = _runtimeResolver.ResolveAssemblyToPath(name); if (string.IsNullOrEmpty(runtimeResolverPath) == false && File.Exists(runtimeResolverPath)) diff --git a/src/TestData/AppContextTest/AppContextTest.cs b/src/TestData/AppContextTest/AppContextTest.cs new file mode 100644 index 000000000..e9b96d3ec --- /dev/null +++ b/src/TestData/AppContextTest/AppContextTest.cs @@ -0,0 +1,20 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +using System; +using System.IO; +using NUnit.Framework; + +namespace NUnit.Engine.Core.Tests +{ + public class AppContextTest + { + [Test] + public void VerifyBasePath() + { + var thisAssembly = GetType().Assembly; + var expectedPath = Path.GetDirectoryName(GetType().Assembly.Location); + + Assert.That(AppContext.BaseDirectory, Is.SamePath(expectedPath)); + } + } +} diff --git a/src/TestData/AppContextTest/AppContextTest.csproj b/src/TestData/AppContextTest/AppContextTest.csproj new file mode 100644 index 000000000..2ff431284 --- /dev/null +++ b/src/TestData/AppContextTest/AppContextTest.csproj @@ -0,0 +1,16 @@ + + + + net462;netcoreapp3.1;net6.0;net8.0 + ..\..\..\bin\$(Configuration)\testdata\ + + + + + + + + + + +