Skip to content

Commit

Permalink
Revise api in response to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Nov 1, 2024
1 parent d607ba5 commit 2945f31
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 70 deletions.
6 changes: 0 additions & 6 deletions choco/nunit-console-runner.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<file src = "../LICENSE.txt" target = "tools" />
<file src = "../NOTICES.txt" target = "tools" />
<file src = "VERIFICATION.txt" target = "tools" />
<file src = "nunit.console.choco.addins" target = "tools" />

<file src = "$BIN_DIR$net462/nunit3-console.exe" target="tools" />
<file src = "$BIN_DIR$net462/nunit3-console.exe.config" target="tools" />
Expand All @@ -49,7 +48,6 @@
<file src = "$BIN_DIR$agents/net462/testcentric.engine.metadata.dll" target="tools/agents/net462" />
<file src = "nunit-agent.exe.ignore" target="tools/agents/net462" />
<file src = "nunit-agent-x86.exe.ignore" target="tools/agents/net462" />
<file src = "nunit.console.choco.agent.addins" target = "tools/agents/net462" />

<file src = "$BIN_DIR$agents/netcoreapp3.1/nunit-agent.dll" target="tools/agents/netcoreapp3.1" />
<file src = "$BIN_DIR$agents/netcoreapp3.1/nunit-agent.dll.config" target="tools/agents/netcoreapp3.1" />
Expand All @@ -60,7 +58,6 @@
<file src = "$BIN_DIR$agents/netcoreapp3.1/nunit.engine.core.dll" target="tools/agents/netcoreapp3.1" />
<file src = "$BIN_DIR$agents/netcoreapp3.1/testcentric.engine.metadata.dll" target="tools/agents/netcoreapp3.1" />
<file src = "$BIN_DIR$agents/netcoreapp3.1/Microsoft.Extensions.DependencyModel.dll" target="tools/agents/netcoreapp3.1" />
<file src = "nunit.console.choco.agent.addins" target = "tools/agents/netcoreapp3.1" />

<file src = "$BIN_DIR$agents/net6.0/nunit-agent.dll" target="tools/agents/net6.0" />
<file src = "$BIN_DIR$agents/net6.0/nunit-agent.dll.config" target="tools/agents/net6.0" />
Expand All @@ -71,7 +68,6 @@
<file src = "$BIN_DIR$agents/net6.0/nunit.engine.core.dll" target="tools/agents/net6.0" />
<file src = "$BIN_DIR$agents/net6.0/testcentric.engine.metadata.dll" target="tools/agents/net6.0" />
<file src = "$BIN_DIR$agents/net6.0/Microsoft.Extensions.DependencyModel.dll" target="tools/agents/net6.0" />
<file src = "nunit.console.choco.agent.addins" target = "tools/agents/net6.0" />

<file src = "$BIN_DIR$agents/net7.0/nunit-agent.dll" target="tools/agents/net7.0" />
<file src = "$BIN_DIR$agents/net7.0/nunit-agent.dll.config" target="tools/agents/net7.0" />
Expand All @@ -82,7 +78,6 @@
<file src = "$BIN_DIR$agents/net7.0/nunit.engine.core.dll" target="tools/agents/net7.0" />
<file src = "$BIN_DIR$agents/net7.0/testcentric.engine.metadata.dll" target="tools/agents/net7.0" />
<file src = "$BIN_DIR$agents/net7.0/Microsoft.Extensions.DependencyModel.dll" target="tools/agents/net7.0" />
<file src = "nunit.console.choco.agent.addins" target = "tools/agents/net7.0" />

<file src = "$BIN_DIR$agents/net8.0/nunit-agent.dll" target="tools/agents/net8.0" />
<file src = "$BIN_DIR$agents/net8.0/nunit-agent.dll.config" target="tools/agents/net8.0" />
Expand All @@ -93,6 +88,5 @@
<file src = "$BIN_DIR$agents/net8.0/nunit.engine.core.dll" target="tools/agents/net8.0" />
<file src = "$BIN_DIR$agents/net8.0/testcentric.engine.metadata.dll" target="tools/agents/net8.0" />
<file src = "$BIN_DIR$agents/net8.0/Microsoft.Extensions.DependencyModel.dll" target="tools/agents/net8.0" />
<file src = "nunit.console.choco.agent.addins" target = "tools/agents/net8.0" />
</files>
</package>
7 changes: 0 additions & 7 deletions choco/nunit.console.choco.addins

This file was deleted.

7 changes: 0 additions & 7 deletions choco/nunit.console.choco.agent.addins

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using NUnit.Engine.Internal.FileSystemAccess;
using System.Diagnostics;

namespace NUnit.Engine.Services
namespace NUnit.Engine.Services.Tests
{
public class ExtensionManagerTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -51,12 +52,12 @@ public void CreateService()
[Test]
public void StartServiceInitializesExtensionManager()
{
var workingDir = AssemblyHelper.GetDirectoryName(typeof(ExtensionService).Assembly);
Assembly hostAssembly = typeof(ExtensionService).Assembly;

_extensionService.StartService();

_extensionManager.ReceivedWithAnyArgs().FindExtensionPoints(typeof(ExtensionService).Assembly, typeof(ITestEngine).Assembly);
_extensionManager.Received().FindExtensions(workingDir);
_extensionManager.Received().FindStandardExtensions(hostAssembly);
Assert.That(_extensionService.Status, Is.EqualTo(ServiceStatus.Started));
}

Expand Down
63 changes: 37 additions & 26 deletions src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace NUnit.Engine.Services
public class ExtensionManager : IExtensionManager
{
static readonly Logger log = InternalTrace.GetLogger(typeof(ExtensionManager));
static readonly Assembly THIS_ASSEMBLY = Assembly.GetExecutingAssembly();
static readonly Version ENGINE_VERSION = THIS_ASSEMBLY.GetName().Version;

private readonly IFileSystem _fileSystem;
//private readonly IAddinsFileReader _addinsReader;
Expand Down Expand Up @@ -138,15 +136,18 @@ public void FindExtensions(string startDir)
}

/// <summary>
/// Find and install extensions starting from a given base directory,
/// and using the provided list of patterns to direct the search using
/// a built-in algorithm.
/// Find and install standard extensions for a host assembly using
/// a built-in algorithm that searches in certain known locations.
/// </summary>
/// <param name="initialDirectory">Path to the initial directory.</param>
/// <param name="patterns">A list of patterns used to identify potential candidates.</param>
public void FindExtensions(string startDir, string[] patterns)
/// <param name="hostAssembly">An assembly that supports extensions.</param>
public void FindStandardExtensions(Assembly hostAssembly)
{
FindExtensionAssemblies(_fileSystem.GetDirectory(startDir), patterns);
bool isChocolateyPackage = System.IO.File.Exists(Path.Combine(hostAssembly.Location, "VERIFICATION.txt"));
string[] extensionPatterns = isChocolateyPackage
? new[] { "nunit-extension-*/**/tools/", "nunit-extension-*/**/tools/*/" }
: new[] { "NUnit.Extension.*/**/tools/", "NUnit.Extension.*/**/tools/*/" };

FindExtensionAssemblies(hostAssembly, extensionPatterns);

foreach (var candidate in _assemblies)
FindExtensionsInAssembly(candidate);
Expand Down Expand Up @@ -265,16 +266,15 @@ private void FindExtensionAssemblies(IDirectory initialDirectory)
}

/// <summary>
/// Find candidate extension assemblies starting from a given base directory
/// and using the provided list of patterns to direct the search using
/// Find candidate extension assemblies for a given host assembly,
/// using the provided list of patterns to direct the search using
/// a built-in algorithm.
/// </summary>
/// <param name="initialDirectory">Path to the initial directory.</param>
/// <param name="hostAssembly">An assembly that supports extensions</param>
/// <param name="patterns">A list of patterns used to identify potential candidates.</param>
private void FindExtensionAssemblies(IDirectory initialDirectory, string[] patterns)
private void FindExtensionAssemblies(Assembly hostAssembly, string[] patterns)
{
// Start looking two levels above initial directory
var startDir = initialDirectory.Parent.Parent;
IDirectory startDir = _fileSystem.GetDirectory(AssemblyHelper.GetDirectoryName(hostAssembly));

while (startDir != null)
{
Expand Down Expand Up @@ -459,18 +459,29 @@ internal void FindExtensionsInAssembly(ExtensionAssembly assembly)
}
#endif

foreach (var type in assembly.MainModule.GetTypes())
foreach (var extensionType in assembly.MainModule.GetTypes())
{
CustomAttribute extensionAttr = type.GetAttribute("NUnit.Engine.Extensibility.ExtensionAttribute");
CustomAttribute extensionAttr = extensionType.GetAttribute("NUnit.Engine.Extensibility.ExtensionAttribute");

if (extensionAttr == null)
continue;

object versionArg = extensionAttr.GetNamedArgument("EngineVersion");
if (versionArg != null && new Version((string)versionArg) > ENGINE_VERSION)
continue;
// TODO: This is a remnant of older code. In principle, this should be generalized
// to something like "HostVersion". However, this can safely remain until
// we separate ExtensionManager into its own assembly.
string versionArg = extensionAttr.GetNamedArgument("EngineVersion") as string;
if (versionArg != null)
{
Assembly THIS_ASSEMBLY = Assembly.GetExecutingAssembly();
Version ENGINE_VERSION = THIS_ASSEMBLY.GetName().Version;
if (new Version(versionArg) > ENGINE_VERSION)
{
log.Warning($" Ignoring {extensionType.Name}. It requires version {versionArg}.");
continue;
}
}

var node = new ExtensionNode(assembly.FilePath, assembly.AssemblyVersion, type.FullName, assemblyTargetFramework)
var node = new ExtensionNode(assembly.FilePath, assembly.AssemblyVersion, extensionType.FullName, assemblyTargetFramework)
{
Path = extensionAttr.GetNamedArgument("Path") as string,
Description = extensionAttr.GetNamedArgument("Description") as string
Expand All @@ -479,9 +490,9 @@ internal void FindExtensionsInAssembly(ExtensionAssembly assembly)
object enabledArg = extensionAttr.GetNamedArgument("Enabled");
node.Enabled = enabledArg == null || (bool)enabledArg;

log.Info(" Found ExtensionAttribute on Type " + type.Name);
log.Info(" Found ExtensionAttribute on Type " + extensionType.Name);

foreach (var attr in type.GetAttributes("NUnit.Engine.Extensibility.ExtensionPropertyAttribute"))
foreach (var attr in extensionType.GetAttributes("NUnit.Engine.Extensibility.ExtensionPropertyAttribute"))
{
string name = attr.ConstructorArguments[0].Value as string;
string value = attr.ConstructorArguments[1].Value as string;
Expand All @@ -498,12 +509,12 @@ internal void FindExtensionsInAssembly(ExtensionAssembly assembly)
ExtensionPoint ep;
if (node.Path == null)
{
ep = DeduceExtensionPointFromType(type);
ep = DeduceExtensionPointFromType(extensionType);
if (ep == null)
{
string msg = string.Format(
"Unable to deduce ExtensionPoint for Type {0}. Specify Path on ExtensionAttribute to resolve.",
type.FullName);
extensionType.FullName);
throw new NUnitEngineException(msg);
}

Expand All @@ -517,7 +528,7 @@ internal void FindExtensionsInAssembly(ExtensionAssembly assembly)
{
string msg = string.Format(
"Unable to locate ExtensionPoint for Type {0}. The Path {1} cannot be found.",
type.FullName,
extensionType.FullName,
node.Path);
throw new NUnitEngineException(msg);
}
Expand Down
20 changes: 5 additions & 15 deletions src/NUnitEngine/nunit.engine.core/Services/ExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ namespace NUnit.Engine.Services
/// </summary>
public class ExtensionService : Service, IExtensionService
{
private static readonly Assembly THIS_ASSEMBLY = typeof(ExtensionService).Assembly;

private static readonly string[] CHOCO_PATTERNS = new[] {
"nunit-extension-*/**/tools/", "nunit-extension-*/**/tools/*/" };
private static readonly string[] NUGET_PATTERNS = new[] {
"NUnit.Extension.*/**/tools/", "NUnit.Extension.*/**/tools/*/" };

private readonly IExtensionManager _extensionManager;

public ExtensionService()
Expand Down Expand Up @@ -83,16 +76,13 @@ public void EnableExtension(string typeName, bool enabled)

public override void StartService()
{
Assembly thisAssembly = Assembly.GetExecutingAssembly();
Assembly apiAssembly = typeof(ITestEngine).Assembly;

try
{
_extensionManager.FindExtensionPoints(
Assembly.GetExecutingAssembly(),
typeof(ITestEngine).Assembly);

var initialDirectory = AssemblyHelper.GetDirectoryName(THIS_ASSEMBLY);
bool isChocolateyPackage = System.IO.File.Exists(Path.Combine(THIS_ASSEMBLY.Location, "VERIFICATION.txt"));

_extensionManager.FindExtensions(initialDirectory, isChocolateyPackage ? CHOCO_PATTERNS : NUGET_PATTERNS);
_extensionManager.FindExtensionPoints(thisAssembly, apiAssembly);
_extensionManager.FindStandardExtensions(thisAssembly);

Status = ServiceStatus.Started;
}
Expand Down
10 changes: 4 additions & 6 deletions src/NUnitEngine/nunit.engine.core/Services/IExtensionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ public interface IExtensionManager : IDisposable
void FindExtensions(string initialDirectory);

/// <summary>
/// Find and install extensions starting from a given base directory,
/// and using the provided list of patterns to direct the search using
/// a built-in algorithm.
/// Find and install standard extensions for a host assembly using
/// a built-in algorithm that searches in certain known locations.
/// </summary>
/// <param name="initialDirectory">Path to the initial directory.</param>
/// <param name="patterns">A list of patterns used to identify potential candidates.</param>
void FindExtensions(string initialDirectory, string[] patterns);
/// <param name="hostAssembly">An assembly that supports NUnit extensions.</param>
void FindStandardExtensions(Assembly hostAssembly);

IExtensionPoint GetExtensionPoint(string path);

Expand Down

0 comments on commit 2945f31

Please sign in to comment.