Skip to content

Commit c91592a

Browse files
author
Paul van Brenk
committed
Clean up AssemblyResolver code
1 parent 469bea0 commit c91592a

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

Nodejs/Product/TestAdapter/AssemblyResolver.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
namespace Microsoft.NodejsTools.TestAdapter
99
{
10-
internal sealed class AssemblyResolver : IDisposable
10+
internal sealed class AssemblyResolver
1111
{
12-
public AssemblyResolver()
12+
private const string ResolveHandlerKey = "JavaScriptUnitTest_ResolveHandler";
13+
private static AssemblyResolver resolver;
14+
15+
private AssemblyResolver()
1316
{
1417
// Use the setup API to find the VS install Dir, then build paths to the Private and Public Assemblies folders
1518
var installPath = GetVSInstallDir();
@@ -24,9 +27,19 @@ public AssemblyResolver()
2427
AppDomain.CurrentDomain.AssemblyResolve += this.OnAssemblyResolve;
2528
}
2629

30+
public static void SetupHandler()
31+
{
32+
var handler = AppDomain.CurrentDomain.GetData(ResolveHandlerKey);
33+
if(handler == null)
34+
{
35+
resolver = new AssemblyResolver();
36+
AppDomain.CurrentDomain.SetData(ResolveHandlerKey, "set");
37+
}
38+
}
39+
2740
private readonly string[] probePaths;
2841

29-
internal static string GetVSInstallDir()
42+
private static string GetVSInstallDir()
3043
{
3144
var vsTestFrameworkAssembly = typeof(ITestExecutor).Assembly;
3245
var testAdapterPath = vsTestFrameworkAssembly.Location;
@@ -58,10 +71,5 @@ private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
5871
}
5972
return null;
6073
}
61-
62-
public void Dispose()
63-
{
64-
AppDomain.CurrentDomain.AssemblyResolve -= this.OnAssemblyResolve;
65-
}
6674
}
6775
}

Nodejs/Product/TestAdapter/JavaScriptTestDiscoverer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@ namespace Microsoft.NodejsTools.TestAdapter
1919
[DefaultExecutorUri(NodejsConstants.ExecutorUriString)]
2020
public partial class JavaScriptTestDiscoverer : ITestDiscoverer
2121
{
22-
internal static AssemblyResolver AssemblyResolver { get; set; }
23-
2422
public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
2523
{
26-
if (JavaScriptTestDiscoverer.AssemblyResolver == null)
27-
{
28-
JavaScriptTestDiscoverer.AssemblyResolver = new AssemblyResolver();
29-
}
24+
AssemblyResolver.SetupHandler();
3025
this.DiscoverTestsCore(sources, discoveryContext, logger, discoverySink);
3126
}
3227

Nodejs/Product/TestAdapter/JavaScriptTestExecutor.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public class JavaScriptTestExecutor : ITestExecutor
1313

1414
public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
1515
{
16-
this.EnsureInitialized();
16+
AssemblyResolver.SetupHandler();
1717
this.worker = new TestExecutorWorker(runContext, frameworkHandle);
1818
this.worker.RunTests(tests);
1919
}
2020

2121
public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
2222
{
23-
this.EnsureInitialized();
23+
AssemblyResolver.SetupHandler();
2424
this.worker = new TestExecutorWorker(runContext, frameworkHandle);
2525
this.worker.RunTests(sources, new JavaScriptTestDiscoverer());
2626
}
@@ -29,13 +29,5 @@ public void Cancel()
2929
{
3030
this.worker?.Cancel();
3131
}
32-
33-
private void EnsureInitialized()
34-
{
35-
if (JavaScriptTestDiscoverer.AssemblyResolver == null)
36-
{
37-
JavaScriptTestDiscoverer.AssemblyResolver = new AssemblyResolver();
38-
}
39-
}
4032
}
4133
}

Nodejs/Product/TestAdapter/PackageJsonTestDiscoverer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public sealed class PackageJsonTestDiscoverer : ITestDiscoverer
1818
{
1919
public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
2020
{
21+
AssemblyResolver.SetupHandler();
2122
foreach (var source in sources)
2223
{
2324
// we're only interested in package.json files here.

Nodejs/Product/TestAdapter/PackageJsonTestExecutor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public void Cancel()
1818

1919
public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
2020
{
21+
AssemblyResolver.SetupHandler();
2122
this.worker = new TestExecutorWorker(runContext, frameworkHandle);
2223
this.worker.RunTests(sources, new PackageJsonTestDiscoverer());
2324
}
2425

2526
public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
2627
{
28+
AssemblyResolver.SetupHandler();
2729
this.worker = new TestExecutorWorker(runContext, frameworkHandle);
2830
this.worker.RunTests(tests);
2931
}

0 commit comments

Comments
 (0)