Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DriverService a non-service #1138

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cake/package-definitions.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion cake/package-tests.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion msi/nunit/nunit.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<?include runner-directories.wxi ?>

<!-- Components and files -->
<?include addin-files.wxi ?>
<!--<?include addin-files.wxi ?>-->
<?include console-files.wxi ?>
<?include engine-files.wxi ?>
<?include utility-files.wxi ?>
Expand Down
2 changes: 1 addition & 1 deletion msi/nunit/runner-directories.wxi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</Directory>
<DirectoryRef Id="INSTALLDIR">
<Directory Id="CONSOLE_BIN" Name="nunit-console">
<Directory Id="ADDINS" Name="addins" />
<!--<Directory Id="ADDINS" Name="addins" />-->
<Directory Id="AGENTS" Name="agents">
<Directory Id="NET20_AGENT_DIR" Name="net20" />
<Directory Id="NET40_AGENT_DIR" Name="net40" />
Expand Down
4 changes: 2 additions & 2 deletions msi/nunit/runner-features.wxi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Title="NUnit Engine"
Description="Installs the NUnit engine">
<ComponentGroupRef Id="NUNIT.ENGINE" />
<Feature Id="ADDINS.NUNIT.PROJECT.LOADER"
<!--<Feature Id="ADDINS.NUNIT.PROJECT.LOADER"
Level="1"
Title="NUnit Project Loader"
Description="Allows you to load NUnit Project files">
Expand Down Expand Up @@ -42,7 +42,7 @@
Title="TeamCity Event Listener"
Description="Provides progress messages when running under TeamCity">
<ComponentGroupRef Id="TEAMCITY.EVENT.LISTENER" />
</Feature>
</Feature>-->
</Feature>

<Feature Id="NUNIT.CONSOLE"
Expand Down
1 change: 0 additions & 1 deletion src/NUnitConsole/nunit3-console.tests/BadFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void MissingFileTest(string filename, string message)
var services = new ServiceContext();
services.Add(new DefaultTestRunnerFactory());
services.Add(new ExtensionService());
services.Add(new DriverService());
#if NET35
services.Add(new RuntimeFrameworkService());
services.Add(new TestAgency());
Expand Down
1 change: 0 additions & 1 deletion src/NUnitEngine/nunit-agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public static void Main(string[] args)

// Custom Service Initialization
engine.Services.Add(new ExtensionService());
engine.Services.Add(new DriverService());

// Initialize Services
log.Info("Initializing Services");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public void Initialize()
string.Empty,
false).ReturnsForAnyArgs(_driver);

var serviceLocator = Substitute.For<IServiceLocator>();
serviceLocator.GetService<IDriverService>().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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
1 change: 0 additions & 1 deletion src/NUnitEngine/nunit.engine.core/CoreEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
10 changes: 7 additions & 3 deletions src/NUnitEngine/nunit.engine.core/Runners/DirectTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ public abstract class DirectTestRunner : AbstractTestRunner

private readonly List<IFrameworkDriver> _drivers = new List<IFrameworkDriver>();

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
Expand Down Expand Up @@ -101,7 +104,8 @@ protected override TestEngineResult LoadPackage()
// found in the terminal nodes.
var packagesToLoad = TestPackage.Select(p => !p.HasSubPackages());

var driverService = Services.GetService<IDriverService>();
if (DriverService == null)
DriverService = new Services.DriverService();

_drivers.Clear();

Expand All @@ -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));
Expand Down
54 changes: 22 additions & 32 deletions src/NUnitEngine/nunit.engine.core/Services/DriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@ namespace NUnit.Engine.Services
/// The DriverService provides drivers able to load and run tests
/// using various frameworks.
/// </summary>
public class DriverService : Service, IDriverService
public class DriverService : IDriverService
{
static ILogger log = InternalTrace.GetLogger("DriverService");
static readonly ILogger log = InternalTrace.GetLogger("DriverService");

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

public DriverService()
{
var thisAssembly = Assembly.GetExecutingAssembly();
var extensionManager = new ExtensionManager();

extensionManager.FindExtensionPoints(thisAssembly);
extensionManager.FindExtensions(AssemblyHelper.GetDirectoryName(thisAssembly));

foreach (IDriverFactory factory in extensionManager.GetExtensions<IDriverFactory>())
_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());
}

/// <summary>
/// Get a driver suitable for use with a particular test assembly.
/// </summary>
Expand Down Expand Up @@ -93,35 +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()
{
Guard.OperationValid(ServiceContext != null, "Can't start DriverService outside of a ServiceContext");

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());

Status = ServiceStatus.Started;
}
catch(Exception)
{
Status = ServiceStatus.Error;
throw;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -215,6 +215,8 @@ public void FindExtensionPoints(params Assembly[] targetAssemblies)
/// </summary>
private ExtensionPoint DeduceExtensionPointFromType(TypeReference typeRef)
{
log.Debug("Trying to deduce ExtensionPoint from " + typeRef.Name);

var ep = GetExtensionPoint(typeRef);
if (ep != null)
return ep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion src/NUnitEngine/nunit.engine/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down