From cc5a7e7640a2fd605aa4cdd2e70df98d7679456f Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Tue, 1 Feb 2022 17:45:21 -0800 Subject: [PATCH 1/2] Make DriverService use ExtensionManager rather than ExtensionService --- .../nunit.engine.core/Services/DriverService.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs b/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs index 8472e6fd2..967edb0bc 100644 --- a/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs +++ b/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs @@ -96,18 +96,17 @@ public IFrameworkDriver GetDriver(AppDomain domain, string assemblyPath, string public override void StartService() { - Guard.OperationValid(ServiceContext != null, "Can't start DriverService outside of a ServiceContext"); - + // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt try { - var extensionService = ServiceContext.GetService(); - if (extensionService != null) + var extensionManager = new ExtensionManager(); + if (extensionManager != null) { - foreach (IDriverFactory factory in extensionService.GetExtensions()) + foreach (IDriverFactory factory in extensionManager.GetExtensions()) _factories.Add(factory); #if NETFRAMEWORK - var node = extensionService.GetExtensionNode("/NUnit/Engine/NUnitV2Driver"); + var node = extensionManager.GetExtensionNode("/NUnit/Engine/NUnitV2Driver"); if (node != null) _factories.Add(new NUnit2DriverFactory(node)); #endif From 1aa0be596929b63ac68f24a0dd9e0e7b4c7dbec5 Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Fri, 11 Feb 2022 16:10:50 -0800 Subject: [PATCH 2/2] Make DriverService no longer a service... just a class --- cake/package-definitions.cake | 4 +- cake/package-tests.cake | 2 +- msi/nunit/nunit.wxs | 2 +- msi/nunit/runner-directories.wxi | 2 +- msi/nunit/runner-features.wxi | 4 +- .../nunit3-console.tests/BadFileTests.cs | 1 - src/NUnitEngine/nunit-agent/Program.cs | 1 - .../Runners/DirectTestRunnerTests.cs | 6 +-- .../Runners/TestEngineRunnerTests.cs | 1 - .../Services/DriverServiceTests.cs | 11 ---- .../nunit.engine.core/CoreEngine.cs | 1 - .../Runners/DirectTestRunner.cs | 10 ++-- .../Services/DriverService.cs | 53 ++++++++----------- .../Services/ExtensionManager.cs | 4 +- .../Runners/MasterTestRunnerTests.cs | 1 - .../XmlTransformResultWriterTests.cs | 1 - src/NUnitEngine/nunit.engine/TestEngine.cs | 1 - 17 files changed, 41 insertions(+), 64 deletions(-) diff --git a/cake/package-definitions.cake b/cake/package-definitions.cake index ea65e4e61..ba3f0268b 100644 --- a/cake/package-definitions.cake +++ b/cake/package-definitions.cake @@ -123,10 +123,10 @@ public void InitializePackageDefinitions(ICakeContext context) checks: new PackageCheck[] { HasDirectory("NUnit.org").WithFiles("LICENSE.txt", "NOTICES.txt", "nunit.ico"), HasDirectory("NUnit.org/nunit-console").WithFiles(CONSOLE_FILES).AndFiles(ENGINE_FILES).AndFile("nunit.bundle.addins"), - HasDirectory("Nunit.org/nunit-console/addins").WithFiles("nunit.core.dll", "nunit.core.interfaces.dll", "nunit.v2.driver.dll", "nunit-project-loader.dll", "vs-project-loader.dll", "nunit-v2-result-writer.dll", "teamcity-event-listener.dll") + //HasDirectory("Nunit.org/nunit-console/addins").WithFiles("nunit.core.dll", "nunit.core.interfaces.dll", "nunit.v2.driver.dll", "nunit-project-loader.dll", "vs-project-loader.dll", "nunit-v2-result-writer.dll", "teamcity-event-listener.dll") }, executable: "NUnit.org/nunit-console/nunit3-console.exe", - // Extensions are not yet built for 4.0 + // TODO: NUnitProjectLoader Extension is not yet built for 4.0 tests: StandardRunnerTests),//.Concat(new[] { NUnitProjectTest })), NUnitConsoleZipPackage = new ZipPackage( diff --git a/cake/package-tests.cake b/cake/package-tests.cake index e28e90434..e2c5aeb38 100644 --- a/cake/package-tests.cake +++ b/cake/package-tests.cake @@ -27,7 +27,7 @@ static PackageTest Net35X86Test = new PackageTest( static PackageTest Net40Test = new PackageTest( "Net40Test", "Run mock-assembly.dll under .NET 4.x", - "net40/mock-assembly.dll", + "net40/mock-assembly.dll --trace:Debug", MockAssemblyExpectedResult(1)); static PackageTest Net40X86Test = new PackageTest( diff --git a/msi/nunit/nunit.wxs b/msi/nunit/nunit.wxs index f7b1866b4..39d1b9c9e 100644 --- a/msi/nunit/nunit.wxs +++ b/msi/nunit/nunit.wxs @@ -21,7 +21,7 @@ - + diff --git a/msi/nunit/runner-directories.wxi b/msi/nunit/runner-directories.wxi index 1f4eab58e..fe797ef27 100644 --- a/msi/nunit/runner-directories.wxi +++ b/msi/nunit/runner-directories.wxi @@ -9,7 +9,7 @@ - + diff --git a/msi/nunit/runner-features.wxi b/msi/nunit/runner-features.wxi index 57684c873..c026703e0 100644 --- a/msi/nunit/runner-features.wxi +++ b/msi/nunit/runner-features.wxi @@ -13,7 +13,7 @@ Title="NUnit Engine" Description="Installs the NUnit engine"> - @@ -42,7 +42,7 @@ Title="TeamCity Event Listener" Description="Provides progress messages when running under TeamCity"> - + --> (); - serviceLocator.GetService().Returns(driverService); - - _directTestRunner = new EmptyDirectTestRunner(serviceLocator, new TestPackage("mock-assembly.dll")); + _directTestRunner = new EmptyDirectTestRunner(new ServiceContext(), new TestPackage("mock-assembly.dll")); + _directTestRunner.DriverService = driverService; } [Test] diff --git a/src/NUnitEngine/nunit.engine.core.tests/Runners/TestEngineRunnerTests.cs b/src/NUnitEngine/nunit.engine.core.tests/Runners/TestEngineRunnerTests.cs index d3a906049..05cf6f6ad 100644 --- a/src/NUnitEngine/nunit.engine.core.tests/Runners/TestEngineRunnerTests.cs +++ b/src/NUnitEngine/nunit.engine.core.tests/Runners/TestEngineRunnerTests.cs @@ -47,7 +47,6 @@ public void Initialize() // Add all services needed by any of our TestEngineRunners _services = new ServiceContext(); _services.Add(new Services.ExtensionService()); - _services.Add(new Services.DriverService()); _services.ServiceManager.StartServices(); var mockAssemblyPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "mock-assembly.dll"); diff --git a/src/NUnitEngine/nunit.engine.core.tests/Services/DriverServiceTests.cs b/src/NUnitEngine/nunit.engine.core.tests/Services/DriverServiceTests.cs index cb76a3872..11908847c 100644 --- a/src/NUnitEngine/nunit.engine.core.tests/Services/DriverServiceTests.cs +++ b/src/NUnitEngine/nunit.engine.core.tests/Services/DriverServiceTests.cs @@ -17,20 +17,9 @@ public class DriverServiceTests [SetUp] public void CreateDriverFactory() { - var serviceContext = new ServiceContext(); - serviceContext.Add(new ExtensionService()); _driverService = new DriverService(); - serviceContext.Add(_driverService); - serviceContext.ServiceManager.StartServices(); } - [Test] - public void ServiceIsStarted() - { - Assert.That(_driverService.Status, Is.EqualTo(ServiceStatus.Started), "Failed to start service"); - } - - #if NET5_0_OR_GREATER [TestCase("mock-assembly.dll", false, typeof(NUnitNetCore31Driver))] [TestCase("mock-assembly.dll", true, typeof(NUnitNetCore31Driver))] diff --git a/src/NUnitEngine/nunit.engine.core/CoreEngine.cs b/src/NUnitEngine/nunit.engine.core/CoreEngine.cs index 9d93787b1..c4e0ef6d3 100644 --- a/src/NUnitEngine/nunit.engine.core/CoreEngine.cs +++ b/src/NUnitEngine/nunit.engine.core/CoreEngine.cs @@ -60,7 +60,6 @@ public void InitializeServices() // Services that depend on other services must be added after their dependencies // For example, ResultService uses ExtensionService, so ExtensionService is added // later. - Services.Add(new DriverService()); Services.Add(new ExtensionService()); } diff --git a/src/NUnitEngine/nunit.engine.core/Runners/DirectTestRunner.cs b/src/NUnitEngine/nunit.engine.core/Runners/DirectTestRunner.cs index 83b90f3ac..c26967e61 100644 --- a/src/NUnitEngine/nunit.engine.core/Runners/DirectTestRunner.cs +++ b/src/NUnitEngine/nunit.engine.core/Runners/DirectTestRunner.cs @@ -39,10 +39,13 @@ public abstract class DirectTestRunner : AbstractTestRunner private readonly List _drivers = new List(); - private ProvidedPathsAssemblyResolver _assemblyResolver; + private readonly ProvidedPathsAssemblyResolver _assemblyResolver; protected AppDomain TestDomain { get; set; } + // Used to inject DriverService for testing + internal IDriverService DriverService { get; set; } + public DirectTestRunner(IServiceLocator services, TestPackage package) : base(services, package) { // Bypass the resolver if not in the default AppDomain. This prevents trying to use the resolver within @@ -101,7 +104,8 @@ protected override TestEngineResult LoadPackage() // found in the terminal nodes. var packagesToLoad = TestPackage.Select(p => !p.HasSubPackages()); - var driverService = Services.GetService(); + if (DriverService == null) + DriverService = new Services.DriverService(); _drivers.Clear(); @@ -120,7 +124,7 @@ protected override TestEngineResult LoadPackage() _assemblyResolver.AddPathFromFile(testFile); } - IFrameworkDriver driver = driverService.GetDriver(TestDomain, testFile, targetFramework, skipNonTestAssemblies); + IFrameworkDriver driver = DriverService.GetDriver(TestDomain, testFile, targetFramework, skipNonTestAssemblies); driver.ID = subPackage.ID; result.Add(LoadDriver(driver, testFile, subPackage)); diff --git a/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs b/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs index 967edb0bc..c03fea6d6 100644 --- a/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs +++ b/src/NUnitEngine/nunit.engine.core/Services/DriverService.cs @@ -16,12 +16,32 @@ namespace NUnit.Engine.Services /// The DriverService provides drivers able to load and run tests /// using various frameworks. /// - public class DriverService : Service, IDriverService + public class DriverService : IDriverService { - static ILogger log = InternalTrace.GetLogger("DriverService"); + static readonly ILogger log = InternalTrace.GetLogger("DriverService"); readonly IList _factories = new List(); + public DriverService() + { + var thisAssembly = Assembly.GetExecutingAssembly(); + var extensionManager = new ExtensionManager(); + + extensionManager.FindExtensionPoints(thisAssembly); + extensionManager.FindExtensions(AssemblyHelper.GetDirectoryName(thisAssembly)); + + foreach (IDriverFactory factory in extensionManager.GetExtensions()) + _factories.Add(factory); + +#if NETFRAMEWORK + var node = extensionManager.GetExtensionNode("/NUnit/Engine/NUnitV2Driver"); + if (node != null) + _factories.Add(new NUnit2DriverFactory(node)); +#endif + + _factories.Add(new NUnit3DriverFactory()); + } + /// /// Get a driver suitable for use with a particular test assembly. /// @@ -93,34 +113,5 @@ public IFrameworkDriver GetDriver(AppDomain domain, string assemblyPath, string return new InvalidAssemblyFrameworkDriver(assemblyPath, string.Format("No suitable tests found in '{0}'.\n" + "Either assembly contains no tests or proper test driver has not been found.", assemblyPath)); } - - public override void StartService() - { - // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt - try - { - var extensionManager = new ExtensionManager(); - if (extensionManager != null) - { - foreach (IDriverFactory factory in extensionManager.GetExtensions()) - _factories.Add(factory); - -#if NETFRAMEWORK - var node = extensionManager.GetExtensionNode("/NUnit/Engine/NUnitV2Driver"); - if (node != null) - _factories.Add(new NUnit2DriverFactory(node)); -#endif - } - - _factories.Add(new NUnit3DriverFactory()); - - Status = ServiceStatus.Started; - } - catch(Exception) - { - Status = ServiceStatus.Error; - throw; - } - } } } diff --git a/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs b/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs index 70f1546d9..4c21ad140 100644 --- a/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs +++ b/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs @@ -21,7 +21,7 @@ namespace NUnit.Engine.Services { public sealed class ExtensionManager : IDisposable { - static readonly Logger log = InternalTrace.GetLogger(typeof(ExtensionService)); + static readonly Logger log = InternalTrace.GetLogger(typeof(ExtensionManager)); static readonly Version ENGINE_VERSION = typeof(ExtensionService).Assembly.GetName().Version; private readonly IFileSystem _fileSystem; @@ -215,6 +215,8 @@ public void FindExtensionPoints(params Assembly[] targetAssemblies) /// private ExtensionPoint DeduceExtensionPointFromType(TypeReference typeRef) { + log.Debug("Trying to deduce ExtensionPoint from " + typeRef.Name); + var ep = GetExtensionPoint(typeRef); if (ep != null) return ep; diff --git a/src/NUnitEngine/nunit.engine.tests/Runners/MasterTestRunnerTests.cs b/src/NUnitEngine/nunit.engine.tests/Runners/MasterTestRunnerTests.cs index f1a22748a..eac27c506 100644 --- a/src/NUnitEngine/nunit.engine.tests/Runners/MasterTestRunnerTests.cs +++ b/src/NUnitEngine/nunit.engine.tests/Runners/MasterTestRunnerTests.cs @@ -97,7 +97,6 @@ public void Initialize() _services.Add(new RuntimeFrameworkService()); _services.Add(new TestAgency()); #endif - _services.Add(new DriverService()); _services.Add(new DefaultTestRunnerFactory()); _services.ServiceManager.StartServices(); diff --git a/src/NUnitEngine/nunit.engine.tests/Services/ResultWriters/XmlTransformResultWriterTests.cs b/src/NUnitEngine/nunit.engine.tests/Services/ResultWriters/XmlTransformResultWriterTests.cs index 53fa0b810..813780edd 100644 --- a/src/NUnitEngine/nunit.engine.tests/Services/ResultWriters/XmlTransformResultWriterTests.cs +++ b/src/NUnitEngine/nunit.engine.tests/Services/ResultWriters/XmlTransformResultWriterTests.cs @@ -21,7 +21,6 @@ public void SetUp() var assemblyPath = GetLocalPath(AssemblyName); var serviceContext = new ServiceContext(); - serviceContext.Add(new DriverService()); serviceContext.Add(new DefaultTestRunnerFactory()); #if NETFRAMEWORK serviceContext.Add(new TestAgency()); diff --git a/src/NUnitEngine/nunit.engine/TestEngine.cs b/src/NUnitEngine/nunit.engine/TestEngine.cs index 7abb33e82..ee0627238 100644 --- a/src/NUnitEngine/nunit.engine/TestEngine.cs +++ b/src/NUnitEngine/nunit.engine/TestEngine.cs @@ -64,7 +64,6 @@ public void Initialize() Services.Add(new RuntimeFrameworkService()); Services.Add(new TestAgency()); #endif - Services.Add(new DriverService()); Services.Add(new ResultService()); Services.Add(new DefaultTestRunnerFactory()); }