From cfca3e38643b234390d49ae4cdb011ea16363b5c Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Mon, 5 Sep 2016 18:43:22 +0300 Subject: [PATCH] 30 Error message "File type is not supported" when uses .nunit configuration file - move all logic related to .nunit config files into MasterTestRunner, ensure that TestPackage is expanded before usage --- .../Runners/AbstractTestRunner.cs | 16 +-- .../Runners/AggregatingTestRunner.cs | 60 ++++++------ .../nunit.engine/Runners/DirectTestRunner.cs | 9 -- .../nunit.engine/Runners/MasterTestRunner.cs | 97 ++++++++++++++----- .../nunit.engine/Services/DriverService.cs | 2 +- 5 files changed, 106 insertions(+), 78 deletions(-) diff --git a/src/NUnitEngine/nunit.engine/Runners/AbstractTestRunner.cs b/src/NUnitEngine/nunit.engine/Runners/AbstractTestRunner.cs index 5602474c8..e6fc72b33 100644 --- a/src/NUnitEngine/nunit.engine/Runners/AbstractTestRunner.cs +++ b/src/NUnitEngine/nunit.engine/Runners/AbstractTestRunner.cs @@ -39,9 +39,9 @@ public abstract class AbstractTestRunner : ITestEngineRunner public AbstractTestRunner(IServiceLocator services, TestPackage package) { Services = services; - TestPackage = package; TestRunnerFactory = Services.GetService(); ProjectService = Services.GetService(); + TestPackage = package; } #region Properties @@ -214,7 +214,7 @@ public AsyncTestEngineResult RunAsync(ITestEventListener listener, TestFilter fi { return RunTestsAsync(listener, filter); } - + #endregion #region IDisposable Members @@ -239,17 +239,5 @@ protected virtual void Dispose(bool disposing) } #endregion - - #region Helper Methods - - protected bool IsProjectPackage(TestPackage package) - { - return package != null - && ProjectService != null - && !string.IsNullOrEmpty(package.FullName) - && ProjectService.CanLoadFrom(package.FullName); - } - - #endregion } } diff --git a/src/NUnitEngine/nunit.engine/Runners/AggregatingTestRunner.cs b/src/NUnitEngine/nunit.engine/Runners/AggregatingTestRunner.cs index 920719754..202856c14 100644 --- a/src/NUnitEngine/nunit.engine/Runners/AggregatingTestRunner.cs +++ b/src/NUnitEngine/nunit.engine/Runners/AggregatingTestRunner.cs @@ -37,7 +37,7 @@ public class AggregatingTestRunner : AbstractTestRunner { // The runners created by the derived class will (at least at the time // of writing this comment) be either TestDomainRunners or ProcessRunners. - protected readonly List _runners = new List(); + private List _runners; // Public for testing purposes public virtual int LevelOfParallelism @@ -45,10 +45,26 @@ public virtual int LevelOfParallelism get { return 1; } } + // Exposed for use by tests + public IList Runners + { + get + { + if (_runners == null) + { + _runners = new List(); + foreach (var subPackage in TestPackage.SubPackages) + { + _runners.Add(CreateRunner(subPackage)); + } + } + + return _runners; + } + } + public AggregatingTestRunner(IServiceLocator services, TestPackage package) : base(services, package) { - foreach (var subPackage in package.SubPackages) - _runners.Add(CreateRunner(subPackage)); } #region AbstractTestRunner Overrides @@ -63,14 +79,10 @@ public override TestEngineResult Explore(TestFilter filter) { var results = new List(); - foreach (ITestEngineRunner runner in _runners) + foreach (ITestEngineRunner runner in Runners) results.Add(runner.Explore(filter)); - TestEngineResult result = ResultHelper.Merge(results); - - return IsProjectPackage(TestPackage) - ? result.MakePackageResult(TestPackage.Name, TestPackage.FullName) - : result; + return ResultHelper.Merge(results); } /// @@ -85,7 +97,7 @@ protected override TestEngineResult LoadPackage() if (packages.Count == 0) packages.Add(TestPackage); - foreach (var runner in _runners) + foreach (var runner in Runners) results.Add(runner.Load()); return ResultHelper.Merge(results); @@ -96,7 +108,7 @@ protected override TestEngineResult LoadPackage() /// public override void UnloadPackage() { - foreach (ITestEngineRunner runner in _runners) + foreach (ITestEngineRunner runner in Runners) runner.Unload(); } @@ -110,7 +122,7 @@ public override int CountTestCases(TestFilter filter) { int count = 0; - foreach (ITestEngineRunner runner in _runners) + foreach (ITestEngineRunner runner in Runners) count += runner.CountTestCases(filter); return count; @@ -132,7 +144,7 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi if (LevelOfParallelism <= 1) { - foreach (ITestEngineRunner runner in _runners) + foreach (ITestEngineRunner runner in Runners) { results.Add(runner.Run(listener, filter)); if (disposeRunners) runner.Dispose(); @@ -143,7 +155,7 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi var workerPool = new ParallelTaskWorkerPool(LevelOfParallelism); var tasks = new List(); - foreach (ITestEngineRunner runner in _runners) + foreach (ITestEngineRunner runner in Runners) { var task = new TestExecutionTask(runner, listener, filter, disposeRunners); tasks.Add(task); @@ -157,13 +169,9 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi results.Add(task.Result()); } - if (disposeRunners) _runners.Clear(); - - TestEngineResult result = ResultHelper.Merge(results); + if (disposeRunners) Runners.Clear(); - return IsProjectPackage(TestPackage) - ? result.MakePackageResult(TestPackage.Name, TestPackage.FullName) - : result; + return ResultHelper.Merge(results); } /// @@ -172,7 +180,7 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi /// If true, cancel any ongoing test threads, otherwise wait for them to complete. public override void StopRun(bool force) { - foreach (var runner in _runners) + foreach (var runner in Runners) runner.StopRun(force); } @@ -180,20 +188,14 @@ protected override void Dispose(bool disposing) { base.Dispose(disposing); - foreach (var runner in _runners) + foreach (var runner in Runners) runner.Dispose(); - _runners.Clear(); + Runners.Clear(); } #endregion - // Exposed for use by tests - public IList Runners - { - get { return _runners; } - } - protected virtual ITestEngineRunner CreateRunner(TestPackage package) { return TestRunnerFactory.MakeTestRunner(package); diff --git a/src/NUnitEngine/nunit.engine/Runners/DirectTestRunner.cs b/src/NUnitEngine/nunit.engine/Runners/DirectTestRunner.cs index 914c3d0a6..c3e1a4f29 100644 --- a/src/NUnitEngine/nunit.engine/Runners/DirectTestRunner.cs +++ b/src/NUnitEngine/nunit.engine/Runners/DirectTestRunner.cs @@ -73,9 +73,6 @@ public override TestEngineResult Explore(TestFilter filter) foreach (IFrameworkDriver driver in _drivers) result.Add(driver.Explore(filter.Text)); - if (IsProjectPackage(TestPackage)) - result = result.MakePackageResult(TestPackage.Name, TestPackage.FullName); - return result; } @@ -115,9 +112,6 @@ protected override TestEngineResult LoadPackage() _drivers.Add(driver); } - if (IsProjectPackage(TestPackage)) - result = result.MakePackageResult(TestPackage.Name, TestPackage.FullName); - return result; } @@ -159,9 +153,6 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi result.Add(driver.Run(listener, filter.Text)); } - if (IsProjectPackage(TestPackage)) - result = result.MakePackageResult(TestPackage.Name, TestPackage.FullName); - if (_assemblyResolver != null) { var packages = TestPackage.SubPackages; diff --git a/src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs b/src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs index ca866ef6b..12b0ef1e7 100644 --- a/src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs +++ b/src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs @@ -22,7 +22,6 @@ // *********************************************************************** using System; -using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; @@ -36,25 +35,26 @@ namespace NUnit.Engine.Runners public class MasterTestRunner : ITestRunner { private const string TEST_RUN_ELEMENT = "test-run"; - - private ITestEngineRunner _engineRunner; - - private IServiceLocator _services; - private IRuntimeFrameworkService _runtimeService; - private ExtensionService _extensionService; - private IProjectService _projectService; - private ITestRunnerFactory _testRunnerFactory; + private readonly ITestEngineRunner _engineRunner; + private readonly IServiceLocator _services; + private readonly IRuntimeFrameworkService _runtimeService; + private readonly ExtensionService _extensionService; + private readonly IProjectService _projectService; + private bool _disposed; public MasterTestRunner(IServiceLocator services, TestPackage package) { + if (services == null) throw new ArgumentNullException("services"); + if (package == null) throw new ArgumentNullException("package"); + _services = services; TestPackage = package; - _testRunnerFactory = _services.GetService(); _projectService = _services.GetService(); _runtimeService = _services.GetService(); _extensionService = _services.GetService(); - _engineRunner = _testRunnerFactory.MakeTestRunner(package); + _engineRunner = _services.GetService().MakeTestRunner(package); + LoadPackage(); } #region Properties @@ -139,7 +139,7 @@ public int CountTestCases(TestFilter filter) /// An XmlNode giving the result of the test execution public XmlNode Run(ITestEventListener listener, TestFilter filter) { - return RunTests(listener, filter).Xml; + return PrepareResult(RunTests(listener, filter)).Xml; } /// @@ -171,7 +171,7 @@ public void StopRun(bool force) /// An XmlNode representing the tests found. public XmlNode Explore(TestFilter filter) { - return _engineRunner.Explore(filter) + return PrepareResult(_engineRunner.Explore(filter)) .Aggregate(TEST_RUN_ELEMENT, TestPackage.Name, TestPackage.FullName).Xml; } @@ -185,8 +185,6 @@ public void Dispose() GC.SuppressFinalize(this); } - private bool _disposed = false; - /// /// Dispose of this object. /// @@ -218,7 +216,7 @@ private void LoadPackage() // Some files in the top level package may be projects. // Expand them so that they contain subprojects for // each contained assembly. - ExpandProjects(); + EnsurePackagesAreExpanded(TestPackage); // Use SelectRuntimeFramework for its side effects. // Info will be left behind in the package about @@ -236,14 +234,51 @@ private void LoadPackage() LoadResult = _engineRunner.Load().Aggregate(TEST_RUN_ELEMENT, TestPackage.Name, TestPackage.FullName); } - private void ExpandProjects() + private TestEngineResult PrepareResult(TestEngineResult result) + { + if (result == null) throw new ArgumentNullException("result"); + + if (!IsProjectPackage(TestPackage)) + { + return result; + } + + return result.MakePackageResult(TestPackage.Name, TestPackage.FullName); + } + + private void EnsurePackagesAreExpanded(TestPackage package) { - foreach (var package in TestPackage.SubPackages) + if (package == null) throw new ArgumentNullException("package"); + + foreach (var subPackage in package.SubPackages) { - string packageName = package.FullName; + EnsurePackagesAreExpanded(subPackage); + } + + if (package.SubPackages.Count == 0 && IsProjectPackage(package)) + { + ExpandProjects(package); + } + } + + private bool IsProjectPackage(TestPackage package) + { + if (package == null) throw new ArgumentNullException("package"); + + return + _projectService != null + && !string.IsNullOrEmpty(package.FullName) + && _projectService.CanLoadFrom(package.FullName); + } + + private void ExpandProjects(TestPackage package) + { + if (package == null) throw new ArgumentNullException("package"); - if (File.Exists(packageName) && _projectService.CanLoadFrom(packageName)) - _projectService.ExpandProjectPackage(package); + string packageName = package.FullName; + if (File.Exists(packageName) && _projectService.CanLoadFrom(packageName)) + { + _projectService.ExpandProjectPackage(package); } } @@ -343,6 +378,11 @@ private static void InsertCommandLineElement(XmlNode resultNode) { var doc = resultNode.OwnerDocument; + if (doc == null) + { + return; + } + XmlNode cmd = doc.CreateElement("command-line"); resultNode.InsertAfter(cmd, null); @@ -356,12 +396,19 @@ private static void InsertFilterElement(XmlNode resultNode, TestFilter filter) var tempNode = XmlHelper.CreateXmlNode(filter.Text); // Don't include it if it's an empty filter - if (tempNode.ChildNodes.Count > 0) + if (tempNode.ChildNodes.Count <= 0) + { + return; + } + + var doc = resultNode.OwnerDocument; + if (doc == null) { - var doc = resultNode.OwnerDocument; - var filterElement = doc.ImportNode(tempNode, true); - resultNode.InsertAfter(filterElement, null); + return; } + + var filterElement = doc.ImportNode(tempNode, true); + resultNode.InsertAfter(filterElement, null); } #endregion diff --git a/src/NUnitEngine/nunit.engine/Services/DriverService.cs b/src/NUnitEngine/nunit.engine/Services/DriverService.cs index 8fbb24642..cc75ce2d2 100644 --- a/src/NUnitEngine/nunit.engine/Services/DriverService.cs +++ b/src/NUnitEngine/nunit.engine/Services/DriverService.cs @@ -38,7 +38,7 @@ namespace NUnit.Engine.Services /// public class DriverService : Service, IDriverService { - IList _factories = new List(); + readonly IList _factories = new List(); #region IDriverService Members