From d7bc9c6fa0b388b14a015f6a1f34fece46b8abe4 Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Sun, 29 Nov 2026 09:11:14 -0800 Subject: [PATCH] Let hosts give extension directories to the engine before it starts Add ITestEngine.ExtensionDirectories, which hosts fill before the engine is initialized. The engine can then find and install all extensions when it starts, so hosts no longer have to add directories to the extension service and finish with a separate InstallExtensions() call. IExtensionService.InstallExtensions() and FindExtensionAssemblies(string) are marked [Obsolete] as warnings, pointing to the new property. They will be removed in 5.0. Co-Authored-By: Claude Opus 5.5 --- .../ExtensionDirectoryApiTests.cs | 42 +++++++++++++++++++ src/NUnit.Engine.Api/IExtensionService.cs | 17 +++++--- src/NUnit.Engine.Api/ITestEngine.cs | 16 +++++++ 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/NUnit.Engine.Api.Tests/ExtensionDirectoryApiTests.cs diff --git a/src/NUnit.Engine.Api.Tests/ExtensionDirectoryApiTests.cs b/src/NUnit.Engine.Api.Tests/ExtensionDirectoryApiTests.cs new file mode 100644 index 0000000..aa1ce42 --- /dev/null +++ b/src/NUnit.Engine.Api.Tests/ExtensionDirectoryApiTests.cs @@ -0,0 +1,42 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +using System; +using System.Collections.Generic; +using System.Reflection; +using NUnit.Framework; + +namespace NUnit.Engine.Api +{ + /// + /// Hosts give extension directories to the engine before it is initialized, + /// instead of adding them to the extension service and then telling it to + /// install. These tests pin that shape of the API. + /// + public class ExtensionDirectoryApiTests + { + [Test] + public void TestEngineHasExtensionDirectoriesList() + { + PropertyInfo property = typeof(ITestEngine).GetProperty("ExtensionDirectories"); + + Assert.That(property, Is.Not.Null, "ITestEngine.ExtensionDirectories is missing"); + Assert.That(property.PropertyType, Is.EqualTo(typeof(IList))); + Assert.That(property.CanRead, Is.True); + Assert.That(property.CanWrite, Is.False, "Hosts add to the list; they don't replace it"); + } + + [TestCase(nameof(IExtensionService.InstallExtensions), new Type[0])] + [TestCase(nameof(IExtensionService.FindExtensionAssemblies), new[] { typeof(string) })] + public void ExtensionServiceSetupMethodsAreObsolete(string name, Type[] parameters) + { + MethodInfo method = typeof(IExtensionService).GetMethod(name, parameters); + Assert.That(method, Is.Not.Null, $"IExtensionService.{name} is missing"); + + var obsolete = method.GetCustomAttribute(); + + Assert.That(obsolete, Is.Not.Null, $"IExtensionService.{name} is not marked [Obsolete]"); + Assert.That(obsolete.IsError, Is.False, "Callers must keep compiling until 5.0"); + Assert.That(obsolete.Message, Does.Contain("ITestEngine.ExtensionDirectories")); + } + } +} diff --git a/src/NUnit.Engine.Api/IExtensionService.cs b/src/NUnit.Engine.Api/IExtensionService.cs index 41b793e..ee274de 100644 --- a/src/NUnit.Engine.Api/IExtensionService.cs +++ b/src/NUnit.Engine.Api/IExtensionService.cs @@ -1,5 +1,6 @@ // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt +using System; using System.Collections.Generic; using NUnit.Extensibility; @@ -27,6 +28,12 @@ public interface IExtensionService /// and using the contained '.addins' files to direct the search. /// /// Path to the initial directory. + /// + /// Obsolete: add the directory to ITestEngine.ExtensionDirectories before the + /// engine is initialized. Once the engine has started, this method throws an + /// InvalidOperationException. It will be removed in 5.0. + /// + [Obsolete("Add the directory to ITestEngine.ExtensionDirectories before the engine is initialized. This method will be removed in 5.0.")] void FindExtensionAssemblies(string initialDirectory); /// @@ -47,15 +54,13 @@ public interface IExtensionService void EnableExtension(string typeName, bool enabled); /// - /// If extensions have not yet been installed, examine all candidate assemblies - /// and load them. Subsequent calls are ignored. + /// Does nothing. The engine installs all extensions when it is initialized, + /// including those in ITestEngine.ExtensionDirectories. /// /// - /// We can only install extensions after all candidate assemblies are identified. - /// This method may be called by the user after all "Find" calls are complete. - /// If the user fails to call it and subsequently tries to examine extensions - /// using certain ExtensionManager methods, it is called automatically. + /// Obsolete: callers can simply remove the call. It will be removed in 5.0. /// + [Obsolete("Extensions are installed when the engine is initialized. Add extra directories to ITestEngine.ExtensionDirectories beforehand, and remove this call. This method will be removed in 5.0.")] void InstallExtensions(); /// diff --git a/src/NUnit.Engine.Api/ITestEngine.cs b/src/NUnit.Engine.Api/ITestEngine.cs index c5ebc83..f1871c8 100644 --- a/src/NUnit.Engine.Api/ITestEngine.cs +++ b/src/NUnit.Engine.Api/ITestEngine.cs @@ -1,6 +1,7 @@ // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt using System; +using System.Collections.Generic; namespace NUnit.Engine { @@ -32,6 +33,21 @@ public interface ITestEngine : IDisposable /// InternalTraceLevel InternalTraceLevel { get; set; } + /// + /// Gets the list of additional directories in which the engine looks for + /// extensions, besides the folders it searches automatically. Add any + /// directories before the engine is initialized: when it initializes, it + /// adds the directories named in the NUNIT_EXTENSION_DIRECTORIES environment + /// variable, finds and installs all extensions, and makes the list read-only. + /// Adding to it afterwards throws an InvalidOperationException. + /// + /// + /// Each directory is searched using the '.addins' files it contains. + /// Accessing initializes the engine if needed, so + /// add directories before using it. + /// + IList ExtensionDirectories { get; } + /// /// Initialize the engine. This includes initializing mono addins, /// setting the trace level and creating the standard set of services