-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
304 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#if NETFRAMEWORK | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit.Abstractions; | ||
|
||
namespace Basic.CompilerLog.UnitTests; | ||
|
||
public static class AppDomainUtils | ||
{ | ||
private static readonly object s_lock = new object(); | ||
private static bool s_hookedResolve; | ||
|
||
public static AppDomain Create(string? name = null, string? basePath = null) | ||
{ | ||
name = name ?? "Custom AppDomain"; | ||
basePath = basePath ?? Path.GetDirectoryName(typeof(AppDomainUtils).Assembly.Location); | ||
|
||
lock (s_lock) | ||
{ | ||
if (!s_hookedResolve) | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve += OnResolve; | ||
s_hookedResolve = true; | ||
} | ||
} | ||
|
||
return AppDomain.CreateDomain(name, null, new AppDomainSetup() | ||
{ | ||
ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, | ||
ApplicationBase = basePath | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// When run under xunit without AppDomains all DLLs get loaded via the AssemblyResolve | ||
/// event. In some cases the xunit, AppDomain marshalling, xunit doesn't fully hook | ||
/// the event and we need to do it for our assemblies. | ||
/// </summary> | ||
private static Assembly? OnResolve(object sender, ResolveEventArgs e) | ||
{ | ||
var assemblyName = new AssemblyName(e.Name); | ||
var fullPath = Path.Combine( | ||
Path.GetDirectoryName(typeof(AppDomainUtils).Assembly.Location), | ||
assemblyName.Name + ".dll"); | ||
if (File.Exists(fullPath)) | ||
{ | ||
return Assembly.LoadFrom(fullPath); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public sealed class AppDomainTestOutputHelper : MarshalByRefObject, ITestOutputHelper | ||
{ | ||
public ITestOutputHelper TestOutputHelper { get; } | ||
|
||
public AppDomainTestOutputHelper(ITestOutputHelper testOutputHelper) | ||
{ | ||
TestOutputHelper = testOutputHelper; | ||
} | ||
|
||
public void WriteLine(string message) => | ||
TestOutputHelper.WriteLine(message); | ||
|
||
public void WriteLine(string format, params object[] args) => | ||
TestOutputHelper.WriteLine(format, args); | ||
} | ||
|
||
public sealed class InvokeUtil : MarshalByRefObject | ||
{ | ||
internal void Invoke<T>(string typeName, string methodName, ITestOutputHelper testOutputHelper, T state) | ||
{ | ||
var type = typeof(AppDomainUtils).Assembly.GetType(typeName, throwOnError: false)!; | ||
var member = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)!; | ||
|
||
// A static lambda will still be an instance method so we need to create the closure | ||
// here. | ||
var obj = member.IsStatic | ||
? null | ||
: type.Assembly.CreateInstance(typeName); | ||
|
||
try | ||
{ | ||
member.Invoke(obj, [testOutputHelper, state]); | ||
} | ||
catch (TargetInvocationException ex) | ||
{ | ||
throw new Exception(ex.InnerException.Message); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.