Skip to content

Commit 888b376

Browse files
authored
Merge pull request #1756 from nunit/issue-1754a
Complete issue 1754
2 parents 99d0dee + f480c71 commit 888b376

File tree

15 files changed

+194
-208
lines changed

15 files changed

+194
-208
lines changed

package-tests.cake

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,17 @@ NetCoreRunnerTests.Add(new PackageTest(1, "NoExtensionsInstalled")
413413
ExpectedOutput = new[] { DoesNotContain("Extension:") }
414414
});
415415

416-
AddToBothLists(new PackageTest(1, "ExtensionsInstalledFromAddedDirectory")
416+
StandardRunnerTests.Add(new PackageTest(1, "ExtensionsInstalledFromAddedDirectory")
417417
{
418418
Description = "List Extensions shows extension from added directory",
419-
Arguments = "--extensionDirectory ../../src/TestData/FakeExtensions --list-extensions",
419+
Arguments = "--extensionDirectory fakesv2/net462 --list-extensions",
420+
ExpectedOutput = new[] { Contains("Extension:", exactly: 5) }
421+
});
422+
423+
NetCoreRunnerTests.Add(new PackageTest(1, "ExtensionsInstalledFromAddedDirectory")
424+
{
425+
Description = "List Extensions shows extension from added directory",
426+
Arguments = "--extensionDirectory fakesv2/netstandard2.0 --list-extensions --trace:Debug",
420427
ExpectedOutput = new[] { Contains("Extension:", exactly: 5) }
421428
});
422429

@@ -451,14 +458,14 @@ AddToBothLists(new PackageTest(1, "ExtensionsInstalledFromAddedDirectory")
451458
// ExtensionsNeeded = new[] { Extensions.NUnitProjectLoader }
452459
//});
453460

454-
// V2 Result Writer Test
455-
StandardRunnerTests.Add(new PackageTest(1, "V2ResultWriterTest_Net462")
456-
{
457-
Description = "Run mock-assembly under .NET 4.6.2 and produce V2 output",
458-
Arguments = "testdata/net462/mock-assembly.dll --result=TestResult.xml --result=NUnit2TestResult.xml;format=nunit2",
459-
ExpectedResult = new MockAssemblyExpectedResult("net-4.6.2"),
460-
ExtensionsNeeded = new[] { Extensions.NUnitV2ResultWriter }
461-
});
461+
//// V2 Result Writer Test
462+
//StandardRunnerTests.Add(new PackageTest(1, "V2ResultWriterTest_Net462")
463+
//{
464+
// Description = "Run mock-assembly under .NET 4.6.2 and produce V2 output",
465+
// Arguments = "testdata/net462/mock-assembly.dll --result=TestResult.xml --result=NUnit2TestResult.xml;format=nunit2",
466+
// ExpectedResult = new MockAssemblyExpectedResult("net-4.6.2"),
467+
// ExtensionsNeeded = new[] { Extensions.NUnitV2ResultWriter }
468+
//});
462469

463470
//StandardRunnerTests.Add(new PackageTest(1, "V2ResultWriterTest_Net60")
464471
//{

src/NUnitCommon/nunit.extensibility.tests/ExtensionManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void CreateExtensionManager()
100100

101101
// Find Fake Extensions using alternate start directory
102102
_extensionManager.FindExtensionAssemblies(FAKE_EXTENSIONS_PARENT_DIRECTORY);
103-
_extensionManager.LoadExtensions();
103+
_extensionManager.InstallExtensions();
104104
}
105105

106106
[Test]

src/NUnitCommon/nunit.extensibility/ExtensionManager.cs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ExtensionManager
4747
// List of ExtensionNodes for all extensions discovered.
4848
private readonly List<ExtensionNode> _extensions = new List<ExtensionNode>();
4949

50-
private bool _extensionsAreLoaded;
50+
private bool _extensionsAreInstalled;
5151

5252
// AssemblyTracker is a List of candidate ExtensionAssemblies, with built-in indexing
5353
// by file path and assembly name, eliminating the need to update indices separately.
@@ -173,15 +173,7 @@ public virtual void FindExtensionPoints(params Assembly[] targetAssemblies)
173173
/// <summary>
174174
/// Gets an enumeration of all installed Extensions.
175175
/// </summary>
176-
public IEnumerable<ExtensionNode> Extensions
177-
{
178-
get
179-
{
180-
LoadExtensions();
181-
182-
return _extensions.ToArray();
183-
}
184-
}
176+
public IEnumerable<ExtensionNode> Extensions => _extensions.ToArray();
185177

186178
/// <summary>
187179
/// Find ExtensionAssemblies for a host assembly using
@@ -237,34 +229,33 @@ public void FindExtensionAssemblies(string startDir)
237229
/// </summary>
238230
public IEnumerable<T> GetExtensions<T>()
239231
{
240-
foreach (var node in GetExtensionNodes<T>())
241-
yield return (T)node.ExtensionObject;
232+
foreach (ExtensionNode node in GetExtensionNodes<T>())
233+
if (node.ExtensionObject is T)
234+
yield return (T)node.ExtensionObject;
242235
}
243236

244237
/// <summary>
245238
/// Enable or disable an extension
246239
/// </summary>
247240
public void EnableExtension(string typeName, bool enabled)
248241
{
249-
LoadExtensions();
250-
251242
foreach (var node in _extensions)
252243
if (node.TypeName == typeName)
253244
node.Enabled = enabled;
254245
}
255246

256247
/// <summary>
257-
/// We can only load extensions after all candidate assemblies are identified.
248+
/// We can only install extensions after all candidate assemblies are identified.
258249
/// This method may be called by the user after all "Find" calls are complete.
259250
/// If the user fails to call it and subsequently tries to examine extensions
260251
/// using other ExtensionManager properties or methods, it will be called
261252
/// but calls not going through ExtensionManager may fail.
262253
/// </summary>
263-
public void LoadExtensions()
254+
public void InstallExtensions()
264255
{
265-
if (!_extensionsAreLoaded)
256+
if (!_extensionsAreInstalled)
266257
{
267-
_extensionsAreLoaded = true;
258+
_extensionsAreInstalled = true;
268259

269260
foreach (var candidate in _assemblyTracker)
270261
FindExtensionsInAssembly(candidate);
@@ -280,12 +271,13 @@ public void LoadExtensions()
280271
/// </summary>
281272
public IEnumerable<ExtensionNode> GetExtensionNodes(string path)
282273
{
283-
LoadExtensions();
284-
285-
var ep = GetExtensionPoint(path);
286-
if (ep is not null)
287-
foreach (var node in ep.Extensions)
274+
foreach (var node in _extensions)
275+
if (node.Path == path)
288276
yield return node;
277+
//var ep = GetExtensionPoint(path);
278+
//if (ep is not null)
279+
// foreach (var node in ep.Extensions)
280+
// yield return node;
289281
}
290282

291283
/// <summary>
@@ -294,8 +286,6 @@ public IEnumerable<ExtensionNode> GetExtensionNodes(string path)
294286
/// <param name="path">The identifying path for an ExtensionPoint</param>
295287
public ExtensionNode? GetExtensionNode(string path)
296288
{
297-
LoadExtensions();
298-
299289
var ep = GetExtensionPoint(path);
300290

301291
return ep is not null && ep.Extensions.Count > 0 ? ep.Extensions[0] : null;
@@ -307,8 +297,6 @@ public IEnumerable<ExtensionNode> GetExtensionNodes(string path)
307297
/// <param name="includeDisabled">If true, disabled nodes are included</param>
308298
public IEnumerable<ExtensionNode> GetExtensionNodes<T>(bool includeDisabled = false)
309299
{
310-
LoadExtensions();
311-
312300
var ep = GetExtensionPoint(typeof(T));
313301
if (ep is not null)
314302
foreach (var node in ep.Extensions)

src/NUnitCommon/nunit.extensibility/ExtensionNode.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,26 @@ public IEnumerable<string> GetValues(string name)
113113
/// returns the same object. Use CreateExtensionObject if a new one is
114114
/// needed each time or to specify arguments.
115115
/// </summary>
116-
public object ExtensionObject
116+
public object? ExtensionObject
117117
{
118118
get
119119
{
120120
if (_extensionObject is null)
121-
_extensionObject = CreateExtensionObject();
121+
try
122+
{
123+
_extensionObject = CreateExtensionObject();
124+
Status = ExtensionStatus.Loaded;
125+
}
126+
catch (Exception ex)
127+
{
128+
if (ex is TargetInvocationException)
129+
ex = ex.InnerException!;
130+
131+
Status = ExtensionStatus.Error;
132+
Enabled = false;
133+
Exception = new ExtensibilityException(
134+
$"Error constructing '{TypeName}'.", ex);
135+
}
122136

123137
return _extensionObject;
124138
}

src/NUnitConsole/nunit4-console/ConsoleRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public ConsoleRunner(ITestEngine engine, ConsoleOptions options, ExtendedTextWri
8585
foreach (string extensionDirectory in _options.ExtensionDirectories)
8686
_extensionService.FindExtensionAssemblies(extensionDirectory);
8787

88-
_extensionService.LoadExtensions();
88+
_extensionService.InstallExtensions();
8989

9090
_workDirectory = options.WorkDirectory ?? Directory.GetCurrentDirectory();
9191

src/NUnitConsole/nunit4-console/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,24 @@ public static int Main(string[] args)
8282
if (Options.ShowVersion)
8383
return ConsoleRunner.OK;
8484

85-
if (Options.WarningMessages.Count != 0)
85+
if (Options.WarningMessages.Count > 0)
8686
{
8787
foreach (string message in Options.WarningMessages)
8888
OutWriter.WriteLine(ColorStyle.Warning, message);
8989

9090
OutWriter.WriteLine();
9191
}
9292

93-
using (ITestEngine engine = new TestEngine())
93+
if (Options.ErrorMessages.Count > 0)
9494
{
95-
if (Options.ErrorMessages.Count > 0)
96-
{
97-
foreach (string message in Options.ErrorMessages)
98-
WriteErrorMessage(message);
95+
foreach (string message in Options.ErrorMessages)
96+
WriteErrorMessage(message);
9997

100-
return ConsoleRunner.INVALID_ARG;
101-
}
98+
return ConsoleRunner.INVALID_ARG;
99+
}
102100

101+
using (ITestEngine engine = new TestEngine())
102+
{
103103
if (Options.RuntimeFrameworkSpecified)
104104
{
105105
if (engine.Services.TryGetService<IAvailableRuntimes>(out var availableRuntimes))

src/NUnitEngine/nunit.engine.api/IExtensionService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ public interface IExtensionService
4747
void EnableExtension(string typeName, bool enabled);
4848

4949
/// <summary>
50-
/// If extensions have not yet been loaded, examine all candidate assemblies
50+
/// If extensions have not yet been installed, examine all candidate assemblies
5151
/// and load them. Subsequent calls are ignored.
5252
/// </summary>
5353
/// <remarks>
54-
/// We can only load extensions after all candidate assemblies are identified.
54+
/// We can only install extensions after all candidate assemblies are identified.
5555
/// This method may be called by the user after all "Find" calls are complete.
5656
/// If the user fails to call it and subsequently tries to examine extensions
57-
/// using other ExtensionManager properties or methods, it will be called
58-
/// but calls not going through ExtensionManager may fail.
57+
/// using certain ExtensionManager methods, it is called automatically.
5958
/// </remarks>
60-
void LoadExtensions();
59+
void InstallExtensions();
6160

6261
/// <summary>
6362
/// Get extension objects for all nodes of a given type

src/NUnitEngine/nunit.engine.api/SettingDefinitions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public static class SettingDefinitions
4343
/// </summary>
4444
public static SettingDefinition<string> ConfigurationFile { get; } = new(nameof(ConfigurationFile), string.Empty);
4545

46+
/// <summary>
47+
/// A list of the available configs for this project.
48+
/// </summary>
49+
public static SettingDefinition<string> ConfigNames { get; } = new(nameof(ConfigNames), string.Empty);
50+
4651
///// <summary>
4752
///// Flag (bool) indicating whether tests are being debugged.
4853
///// </summary>
@@ -69,7 +74,6 @@ public static class SettingDefinitions
6974

7075
/// <summary>
7176
/// The maximum number of test agents permitted to run simultaneously.
72-
/// Ignored if the ProcessModel is not set or defaulted to Multiple.
7377
/// </summary>
7478
public static SettingDefinition<int> MaxAgents { get; } = new(nameof(MaxAgents), 0);
7579

src/NUnitEngine/nunit.engine.tests/Services/ServiceManagerTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ public class ServiceManagerTests
99
{
1010
private IService _fakeService;
1111
private ServiceManager _serviceManager;
12+
private ServiceContext _context;
1213

1314
[SetUp]
1415
public void SetUp()
1516
{
16-
_serviceManager = new ServiceManager();
17+
_context = new ServiceContext();
18+
_serviceManager = _context.ServiceManager;
1719

1820
_fakeService = new FakeService();
19-
_serviceManager.AddService(_fakeService);
20-
_serviceManager.AddService(new ExtensionService());
21+
_context.Add(_fakeService);
22+
_context.Add(new ExtensionService());
2123
}
2224

2325
[TearDown]

src/NUnitEngine/nunit.engine/Services/ExtensionService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void EnableExtension(string typeName, bool enabled)
103103
/// If extensions have not yet been loaded, examine all candidate assemblies
104104
/// and load them. Subsequent calls are ignored.
105105
/// </summary>
106-
public void LoadExtensions() => _extensionManager.LoadExtensions();
106+
public void InstallExtensions() => _extensionManager.InstallExtensions();
107107

108108
/// <summary>
109109
/// Get extension objects for all nodes of a given type
@@ -167,6 +167,8 @@ public IEnumerable<ExtensionNode> GetExtensionNodes(string path)
167167

168168
public override void StartService()
169169
{
170+
base.StartService();
171+
170172
try
171173
{
172174
_extensionManager.FindExtensionPoints(

0 commit comments

Comments
 (0)