Skip to content

Commit

Permalink
Fix error in use of --extensionDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Feb 10, 2025
1 parent 060d480 commit 3c06abb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package-tests.cake
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ AddToBothLists(new PackageTest(
new ExpectedResult("Passed") { Assemblies = new[] { new ExpectedAssemblyResult("WpfTest.dll", "netcore-8.0") } }));

//////////////////////////////////////////////////////////////////////
// RUN TESTS USING EACH OF OUR EXTENSIONS
// TESTS USING EACH OF OUR EXTENSIONS
//////////////////////////////////////////////////////////////////////

// NUnit Project Loader Tests
Expand Down Expand Up @@ -287,7 +287,7 @@ StandardRunnerTests.Add(new PackageTest(
StandardRunnerTests.Add(new PackageTest(
1, "V2FrameworkDriverTest",
"Run mock-assembly-v2 using the V2 Driver out of process",
"v2-tests/net462/mock-assembly-v2.dll --list-extensions",
"v2-tests/net462/mock-assembly-v2.dll",
new ExpectedResult("Failed")
{
Total = 28,
Expand Down
4 changes: 2 additions & 2 deletions src/NUnitConsole/nunit3-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ConsoleRunner

public ConsoleRunner(ITestEngine engine, ConsoleOptions options, ExtendedTextWriter writer)
{
Guard.ArgumentNotNull(_engine = engine, nameof(engine));
Guard.ArgumentNotNull(_engine = engine, nameof(engine));
Guard.ArgumentNotNull(_options = options, nameof(options));
Guard.ArgumentNotNull(_outWriter = writer, nameof(writer));

Expand All @@ -71,7 +71,7 @@ public ConsoleRunner(ITestEngine engine, ConsoleOptions options, ExtendedTextWri
foreach (string extensionDirectory in extensionPath.Split(new[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries))
_extensionService.FindExtensionAssemblies(extensionDirectory);

foreach (string extensionDirectory in _options.ExtensionDirectories)
foreach (string extensionDirectory in _options.ExtensionDirectories)
_extensionService.FindExtensionAssemblies(extensionDirectory);

_workDirectory = options.WorkDirectory;
Expand Down
40 changes: 25 additions & 15 deletions src/NUnitEngine/nunit.engine.core/Services/DriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class DriverService : Service, IDriverService
{
static ILogger log = InternalTrace.GetLogger("DriverService");

readonly IList<IDriverFactory> _factories = new List<IDriverFactory>();
private ExtensionService _extensionService;
private IList<IDriverFactory> _factories;

/// <summary>
/// Get a driver suitable for use with a particular test assembly.
Expand Down Expand Up @@ -55,6 +56,9 @@ public IFrameworkDriver GetDriver(AppDomain domain, string assemblyPath, string
" test assemblies are not supported by this version of the engine");
}

if (_factories == null)
InitializeDriverFactories();

try
{
using (var assemblyDef = AssemblyDefinition.ReadAssembly(assemblyPath))
Expand Down Expand Up @@ -104,20 +108,7 @@ public override void StartService()

try
{
var extensionService = ServiceContext.GetService<ExtensionService>();
if (extensionService != null)
{
foreach (IDriverFactory factory in extensionService.GetExtensions<IDriverFactory>())
_factories.Add(factory);

#if NETFRAMEWORK
var node = extensionService.GetExtensionNode("/NUnit/Engine/NUnitV2Driver");
if (node != null)
_factories.Add(new NUnit2DriverFactory(node));
#endif
}

_factories.Add(new NUnit3DriverFactory());
_extensionService = ServiceContext.GetService<ExtensionService>();

Status = ServiceStatus.Started;
}
Expand All @@ -127,5 +118,24 @@ public override void StartService()
throw;
}
}

private void InitializeDriverFactories()
{
_factories = new List<IDriverFactory>();

if (_extensionService != null)
{
foreach (IDriverFactory factory in _extensionService.GetExtensions<IDriverFactory>())
_factories.Add(factory);

#if NETFRAMEWORK
var node = _extensionService.GetExtensionNode("/NUnit/Engine/NUnitV2Driver");
if (node != null)
_factories.Add(new NUnit2DriverFactory(node));
#endif
}

_factories.Add(new NUnit3DriverFactory());
}
}
}
52 changes: 27 additions & 25 deletions src/NUnitEngine/nunit.engine/Services/ProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ namespace NUnit.Engine.Services
/// </summary>
public class ProjectService : Service, IProjectService
{
Dictionary<string, ExtensionNode> _extensionIndex = new Dictionary<string, ExtensionNode>();
Dictionary<string, ExtensionNode> _extensionIndex;
ExtensionService _extensionService;

public bool CanLoadFrom(string path)
{
if (_extensionIndex == null)
InitializeExtensionIndex();

ExtensionNode node = GetNodeForPath(path);
if (node != null)
{
Expand Down Expand Up @@ -84,40 +88,38 @@ public override void StartService()
{
try
{
var extensionService = ServiceContext.GetService<ExtensionService>();
_extensionService = ServiceContext.GetService<ExtensionService>();

if (extensionService == null)
Status = ServiceStatus.Started;
else if (extensionService.Status != ServiceStatus.Started)
Status = ServiceStatus.Error;
else
{
Status = ServiceStatus.Started;

foreach (var node in extensionService.GetExtensionNodes<IProjectLoader>())
{
foreach (string ext in node.GetValues("FileExtension"))
{
if (ext != null)
{
if (_extensionIndex.ContainsKey(ext))
throw new NUnitEngineException(string.Format("ProjectLoader extension {0} is already handled by another extension.", ext));

_extensionIndex.Add(ext, node);
}
}
}
}
Status = _extensionService == null || _extensionService.Status == ServiceStatus.Started
? ServiceStatus.Started : ServiceStatus.Error;
}
catch
{
// TODO: Should we just ignore any addin that doesn't load?
Status = ServiceStatus.Error;
throw;
}
}
}

private void InitializeExtensionIndex()
{
_extensionIndex = new Dictionary<string, ExtensionNode>();

foreach (var node in _extensionService.GetExtensionNodes<IProjectLoader>())
{
foreach (string ext in node.GetValues("FileExtension"))
{
if (ext != null)
{
if (_extensionIndex.ContainsKey(ext))
throw new NUnitEngineException(string.Format("ProjectLoader extension {0} is already handled by another extension.", ext));

_extensionIndex.Add(ext, node);
}
}
}
}

private IProject LoadFrom(string path)
{
if (File.Exists(path))
Expand Down

0 comments on commit 3c06abb

Please sign in to comment.