-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from TestCentric/issue-36
Issue 36
- Loading branch information
Showing
9 changed files
with
317 additions
and
148 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
144 changes: 0 additions & 144 deletions
144
src/TestCentric.Agent.Core/Internal/TestAssemblyResolver.cs
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/TestCentric.Agent.Core/Resolvers/AdditionalRuntimesResolutionStrategy.cs
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,58 @@ | ||
// *********************************************************************** | ||
// Copyright (c) Charlie Poole and TestCentric contributors. | ||
// Licensed under the MIT License. See LICENSE file in root directory. | ||
// *********************************************************************** | ||
|
||
#if NETCOREAPP3_1_OR_GREATER | ||
|
||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace TestCentric.Engine.Internal | ||
{ | ||
internal abstract class AdditionalRuntimesResolutionStrategy : ResolutionStrategy | ||
{ | ||
private static readonly Logger log = InternalTrace.GetLogger(nameof(AdditionalRuntimesResolutionStrategy)); | ||
|
||
protected abstract string RuntimeName { get; } | ||
|
||
public AdditionalRuntimesResolutionStrategy(TestAssemblyResolver resolver) : base(resolver) { } | ||
|
||
public override Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyName assemblyName) | ||
{ | ||
// This strategy requires a version, which may not be present | ||
if (assemblyName.Version == null) | ||
return null; | ||
|
||
var runtimeDir = DotNetRuntimes.GetBestRuntime(RuntimeName, assemblyName.Version).Location; | ||
if (runtimeDir != null) | ||
{ | ||
string candidate = Path.Combine(runtimeDir, assemblyName.Name + ".dll"); | ||
if (File.Exists(candidate)) | ||
{ | ||
log.Debug($"{RuntimeName} Resolved to {candidate}"); | ||
return LoadContext.LoadFromAssemblyPath(candidate); | ||
} | ||
} | ||
|
||
log.Debug($"{RuntimeName} Failed!"); | ||
return null; | ||
} | ||
} | ||
|
||
internal class AspNetCoreResolutionStrategy : AdditionalRuntimesResolutionStrategy | ||
{ | ||
public AspNetCoreResolutionStrategy(TestAssemblyResolver resolver) : base(resolver) { } | ||
|
||
protected override string RuntimeName => "Microsoft.AspNetCore.App"; | ||
} | ||
|
||
internal class WindowsDesktopResolutionStrategy : AdditionalRuntimesResolutionStrategy | ||
{ | ||
public WindowsDesktopResolutionStrategy(TestAssemblyResolver resolver) : base(resolver) { } | ||
|
||
protected override string RuntimeName => "Microsoft.WindowsDesktop.App"; | ||
} | ||
} | ||
#endif |
File renamed without changes.
48 changes: 48 additions & 0 deletions
48
src/TestCentric.Agent.Core/Resolvers/ResolutionStrategy.cs
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,48 @@ | ||
// *********************************************************************** | ||
// Copyright (c) Charlie Poole and TestCentric contributors. | ||
// Licensed under the MIT License. See LICENSE file in root directory. | ||
// *********************************************************************** | ||
|
||
#if NETCOREAPP3_1_OR_GREATER | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using TestCentric.Engine.Internal; | ||
|
||
namespace TestCentric.Engine.Internal | ||
{ | ||
public abstract class ResolutionStrategy | ||
{ | ||
private int _totalCalls; | ||
|
||
internal TestAssemblyLoadContext LoadContext { get; } | ||
internal string TestAssemblyPath { get; } | ||
|
||
|
||
internal ResolutionStrategy(TestAssemblyResolver resolver) | ||
{ | ||
LoadContext = resolver.LoadContext; | ||
TestAssemblyPath = resolver.TestAssemblyPath; | ||
} | ||
|
||
public bool TryLoadAssembly(AssemblyLoadContext context, AssemblyName assemblyName, out Assembly loadedAssembly) | ||
{ | ||
_totalCalls++; | ||
|
||
loadedAssembly = TryLoadAssembly(context, assemblyName); | ||
return loadedAssembly != null; | ||
} | ||
|
||
public abstract Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyName assemblyName); | ||
|
||
public void WriteReport() | ||
{ | ||
Console.WriteLine(); | ||
Console.WriteLine(GetType().Name); | ||
Console.WriteLine(); | ||
Console.WriteLine($"Total Calls: {_totalCalls}"); | ||
} | ||
} | ||
} | ||
#endif |
69 changes: 69 additions & 0 deletions
69
src/TestCentric.Agent.Core/Resolvers/RuntimeLibraryResolutionStrategy.cs
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,69 @@ | ||
// *********************************************************************** | ||
// Copyright (c) Charlie Poole and TestCentric contributors. | ||
// Licensed under the MIT License. See LICENSE file in root directory. | ||
// *********************************************************************** | ||
|
||
#if NETCOREAPP3_1_OR_GREATER | ||
|
||
using Microsoft.Extensions.DependencyModel.Resolution; | ||
using Microsoft.Extensions.DependencyModel; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using System.Linq; | ||
|
||
namespace TestCentric.Engine.Internal | ||
{ | ||
internal class RuntimeLibraryResolutionStrategy : ResolutionStrategy | ||
{ | ||
private static readonly Logger log = InternalTrace.GetLogger(nameof(RuntimeLibraryResolutionStrategy)); | ||
|
||
private List<CompilationLibrary> _libraries = new List<CompilationLibrary>(); | ||
|
||
public RuntimeLibraryResolutionStrategy(TestAssemblyResolver resolver) : base(resolver) | ||
{ | ||
var dependencyContext = DependencyContext.Load(LoadContext.LoadFromAssemblyPath(TestAssemblyPath)); | ||
if (dependencyContext != null) | ||
foreach (var library in dependencyContext.RuntimeLibraries) | ||
_libraries.Add( | ||
new CompilationLibrary( | ||
library.Type, | ||
library.Name, | ||
library.Version, | ||
library.Hash, | ||
library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), | ||
library.Dependencies, | ||
library.Serviceable)); | ||
} | ||
|
||
public override Assembly TryLoadAssembly(AssemblyLoadContext context, AssemblyName assemblyName) | ||
{ | ||
foreach (var library in _libraries) | ||
{ | ||
var assemblies = new List<string>(); | ||
var assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[] | ||
{ | ||
new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(TestAssemblyPath)), | ||
new ReferenceAssemblyPathResolver(), | ||
new PackageCompilationAssemblyResolver() | ||
}); | ||
|
||
assemblyResolver.TryResolveAssemblyPaths(library, assemblies); | ||
|
||
foreach (var assemblyPath in assemblies) | ||
{ | ||
if (assemblyName.Name == Path.GetFileNameWithoutExtension(assemblyPath)) | ||
{ | ||
log.Debug($"Resolved to {assemblyPath}"); | ||
return LoadContext.LoadFromAssemblyPath(assemblyPath); | ||
} | ||
} | ||
} | ||
|
||
log.Debug("Failed!"); | ||
return null; | ||
} | ||
} | ||
} | ||
#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
Oops, something went wrong.