Skip to content

Commit 02f2382

Browse files
committed
Resolve test projects libs from its own directory
1 parent 7b7aa78 commit 02f2382

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Core/Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<PackageReference Include="Microsoft.Build" Version="16.5.0">
1111
<ExcludeAssets>runtime</ExcludeAssets>
1212
</PackageReference>
13-
<PackageReference Include="xunit.core" Version="2.4.1" />
1413
<PackageReference Include="xunit.runner.utility" Version="2.4.1" />
1514
</ItemGroup>
1615

Core/Test/ProjectTestRunner.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Reflection;
5+
using System.Runtime.CompilerServices;
46
using System.Text;
57
using System.Threading;
68
using TestMyCode.CSharp.Core.Data;
@@ -34,6 +36,8 @@ public void RunAssemblyTests(Assembly assembly)
3436
using ManualResetEvent testsCompled = new ManualResetEvent(false);
3537
using AssemblyRunner runner = AssemblyRunner.WithoutAppDomain(assembly.Location);
3638

39+
AppDomain.CurrentDomain.AssemblyResolve += this.CreateTestAssemblyResolver(assembly);
40+
3741
runner.OnTestFailed += info =>
3842
{
3943
this.AddTestResult(MethodTestResult.FromFail(info));
@@ -62,6 +66,25 @@ public void RunAssemblyTests(Assembly assembly)
6266
SpinWait.SpinUntil(() => runner.Status == AssemblyRunnerStatus.Idle);
6367
}
6468

69+
private ResolveEventHandler CreateTestAssemblyResolver(Assembly assembly)
70+
{
71+
return (sender, args) =>
72+
{
73+
AssemblyName name = new AssemblyName(args.Name!);
74+
75+
string assemblyName = $"{name.Name}.dll";
76+
string dir = Path.GetDirectoryName(assembly.Location)!;
77+
78+
string assemblyFile = Path.Combine(dir, assemblyName);
79+
if (File.Exists(assemblyFile))
80+
{
81+
return Assembly.LoadFile(assemblyFile);
82+
}
83+
84+
return null;
85+
};
86+
}
87+
6588
private void AddTestResult(MethodTestResult result)
6689
{
6790
//We lock here to ensure that we don't have any race conditions

0 commit comments

Comments
 (0)