Skip to content

Commit 2d972df

Browse files
authored
Merge pull request #1489 from nunit/issue-1488
Set `AppContext.BaseDirectory` when running under .NET 8.0
2 parents 9325458 + c0bf856 commit 2d972df

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

NUnitConsole.sln

+8-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "notest-assembly", "src\Test
142142
EndProject
143143
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfApp", "src\TestData\WpfApp\WpfApp.csproj", "{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4}"
144144
EndProject
145-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvalidTestNames", "src\TestData\InvalidTestNames\InvalidTestNames.csproj", "{58E18ACC-1F7E-4395-817E-E7EF943E0C77}"
145+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InvalidTestNames", "src\TestData\InvalidTestNames\InvalidTestNames.csproj", "{58E18ACC-1F7E-4395-817E-E7EF943E0C77}"
146+
EndProject
147+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppContextTest", "src\TestData\AppContextTest\AppContextTest.csproj", "{E43A3E4B-B050-471B-B43C-0DF60FD44376}"
146148
EndProject
147149
Global
148150
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -218,6 +220,10 @@ Global
218220
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Debug|Any CPU.Build.0 = Debug|Any CPU
219221
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Release|Any CPU.ActiveCfg = Release|Any CPU
220222
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Release|Any CPU.Build.0 = Release|Any CPU
223+
{E43A3E4B-B050-471B-B43C-0DF60FD44376}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
224+
{E43A3E4B-B050-471B-B43C-0DF60FD44376}.Debug|Any CPU.Build.0 = Debug|Any CPU
225+
{E43A3E4B-B050-471B-B43C-0DF60FD44376}.Release|Any CPU.ActiveCfg = Release|Any CPU
226+
{E43A3E4B-B050-471B-B43C-0DF60FD44376}.Release|Any CPU.Build.0 = Release|Any CPU
221227
EndGlobalSection
222228
GlobalSection(SolutionProperties) = preSolution
223229
HideSolutionNode = FALSE
@@ -251,6 +257,7 @@ Global
251257
{81E63A90-3191-4E99-92FF-01F9B1D3E3C5} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
252258
{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
253259
{58E18ACC-1F7E-4395-817E-E7EF943E0C77} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
260+
{E43A3E4B-B050-471B-B43C-0DF60FD44376} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
254261
EndGlobalSection
255262
GlobalSection(ExtensibilityGlobals) = postSolution
256263
SolutionGuid = {D8E4FC26-5422-4C51-8BBC-D1AC0A578711}

package-tests.cake

+12-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ StandardRunnerTests.Add(new PackageTest(
250250
MockAssemblySolutionResult,
251251
KnownExtensions.VSProjectLoader.SetVersion("3.9.0")));
252252

253-
// Special Cases
253+
//////////////////////////////////////////////////////////////////////
254+
// SPECIAL CASES
255+
//////////////////////////////////////////////////////////////////////
254256

255257
StandardRunnerTests.Add(new PackageTest(
256258
1, "InvalidTestNameTest_Net462",
@@ -287,3 +289,12 @@ AddToBothLists(new PackageTest(
287289
new ExpectedAssemblyResult("InvalidTestNames.dll", "netcore-8.0")
288290
}
289291
}));
292+
293+
StandardRunnerTests.Add(new PackageTest(
294+
1, "AppContextBaseDirectory_NET80",
295+
"Test Setting the BaseDirectory to match test assembly location under .NET 8.0",
296+
"testdata/net8.0/AppContextTest.dll",
297+
new ExpectedResult("Passed")
298+
{
299+
Assemblies = new ExpectedAssemblyResult[] { new ExpectedAssemblyResult("AppContextTest.dll", "netcore-8.0") }
300+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+
using System;
4+
using System.IO;
5+
using NUnit.Framework;
6+
7+
namespace NUnit.Engine.Core.Tests
8+
{
9+
public class AppContextTest
10+
{
11+
[Test]
12+
public void VerifyBasePath()
13+
{
14+
var thisAssembly = GetType().Assembly;
15+
var expectedPath = Path.GetDirectoryName(GetType().Assembly.Location);
16+
17+
Assert.That(AppContext.BaseDirectory, Is.SamePath(expectedPath));
18+
}
19+
}
20+
}

src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public TestAssemblyLoadContext(string testAssemblyPath)
2424
_resolver = new TestAssemblyResolver(this, testAssemblyPath);
2525
_basePath = Path.GetDirectoryName(testAssemblyPath);
2626
_runtimeResolver = new AssemblyDependencyResolver(testAssemblyPath);
27+
#if NET8_0_OR_GREATER
28+
AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", _basePath);
29+
#endif
2730
}
2831

2932
protected override Assembly Load(AssemblyName name)
@@ -37,7 +40,6 @@ protected override Assembly Load(AssemblyName name)
3740
return loadedAssembly;
3841
}
3942

40-
4143
var runtimeResolverPath = _runtimeResolver.ResolveAssemblyToPath(name);
4244
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
4345
File.Exists(runtimeResolverPath))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+
using System;
4+
using System.IO;
5+
using NUnit.Framework;
6+
7+
namespace NUnit.Engine.Core.Tests
8+
{
9+
public class AppContextTest
10+
{
11+
[Test]
12+
public void VerifyBasePath()
13+
{
14+
var thisAssembly = GetType().Assembly;
15+
var expectedPath = Path.GetDirectoryName(GetType().Assembly.Location);
16+
17+
Assert.That(AppContext.BaseDirectory, Is.SamePath(expectedPath));
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net462;netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
5+
<OutputPath>..\..\..\bin\$(Configuration)\testdata\</OutputPath>
6+
</PropertyGroup>
7+
8+
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
9+
<PackageReference Include="nunit" Version="3.14.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup Condition="'$(TargetFramework)'!='netcoreapp3.1'">
13+
<PackageReference Include="nunit" Version="4.2.2" />
14+
</ItemGroup>
15+
16+
</Project>

0 commit comments

Comments
 (0)