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

#30 Error message "File type is not supported" when uses .nunit configuration file - ensure that TestPackage is expanded before usage #52

Merged
merged 1 commit into from
Sep 8, 2016
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
16 changes: 2 additions & 14 deletions src/NUnitEngine/nunit.engine/Runners/AbstractTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public abstract class AbstractTestRunner : ITestEngineRunner
public AbstractTestRunner(IServiceLocator services, TestPackage package)
{
Services = services;
TestPackage = package;
TestRunnerFactory = Services.GetService<ITestRunnerFactory>();
ProjectService = Services.GetService<IProjectService>();
TestPackage = package;
}

#region Properties
Expand Down Expand Up @@ -214,7 +214,7 @@ public AsyncTestEngineResult RunAsync(ITestEventListener listener, TestFilter fi
{
return RunTestsAsync(listener, filter);
}

#endregion

#region IDisposable Members
Expand All @@ -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
}
}
60 changes: 31 additions & 29 deletions src/NUnitEngine/nunit.engine/Runners/AggregatingTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,34 @@ 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<ITestEngineRunner> _runners = new List<ITestEngineRunner>();
private List<ITestEngineRunner> _runners;

// Public for testing purposes
public virtual int LevelOfParallelism
{
get { return 1; }
}

// Exposed for use by tests
public IList<ITestEngineRunner> Runners
{
get
{
if (_runners == null)
{
_runners = new List<ITestEngineRunner>();
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
Expand All @@ -63,14 +79,10 @@ public override TestEngineResult Explore(TestFilter filter)
{
var results = new List<TestEngineResult>();

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

/// <summary>
Expand All @@ -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);
Expand All @@ -96,7 +108,7 @@ protected override TestEngineResult LoadPackage()
/// </summary>
public override void UnloadPackage()
{
foreach (ITestEngineRunner runner in _runners)
foreach (ITestEngineRunner runner in Runners)
runner.Unload();
}

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -143,7 +155,7 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi
var workerPool = new ParallelTaskWorkerPool(LevelOfParallelism);
var tasks = new List<TestExecutionTask>();

foreach (ITestEngineRunner runner in _runners)
foreach (ITestEngineRunner runner in Runners)
{
var task = new TestExecutionTask(runner, listener, filter, disposeRunners);
tasks.Add(task);
Expand All @@ -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);
}

/// <summary>
Expand All @@ -172,28 +180,22 @@ protected override TestEngineResult RunTests(ITestEventListener listener, TestFi
/// <param name="force">If true, cancel any ongoing test threads, otherwise wait for them to complete.</param>
public override void StopRun(bool force)
{
foreach (var runner in _runners)
foreach (var runner in Runners)
runner.StopRun(force);
}

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<ITestEngineRunner> Runners
{
get { return _runners; }
}

protected virtual ITestEngineRunner CreateRunner(TestPackage package)
{
return TestRunnerFactory.MakeTestRunner(package);
Expand Down
9 changes: 0 additions & 9 deletions src/NUnitEngine/nunit.engine/Runners/DirectTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -115,9 +112,6 @@ protected override TestEngineResult LoadPackage()
_drivers.Add(driver);
}

if (IsProjectPackage(TestPackage))
result = result.MakePackageResult(TestPackage.Name, TestPackage.FullName);

return result;
}

Expand Down Expand Up @@ -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;
Expand Down
97 changes: 72 additions & 25 deletions src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
Expand All @@ -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<ITestRunnerFactory>();
_projectService = _services.GetService<IProjectService>();
_runtimeService = _services.GetService<IRuntimeFrameworkService>();
_extensionService = _services.GetService<ExtensionService>();
_engineRunner = _testRunnerFactory.MakeTestRunner(package);
_engineRunner = _services.GetService<ITestRunnerFactory>().MakeTestRunner(package);
LoadPackage();
}

#region Properties
Expand Down Expand Up @@ -139,7 +139,7 @@ public int CountTestCases(TestFilter filter)
/// <returns>An XmlNode giving the result of the test execution</returns>
public XmlNode Run(ITestEventListener listener, TestFilter filter)
{
return RunTests(listener, filter).Xml;
return PrepareResult(RunTests(listener, filter)).Xml;
}

/// <summary>
Expand Down Expand Up @@ -171,7 +171,7 @@ public void StopRun(bool force)
/// <returns>An XmlNode representing the tests found.</returns>
public XmlNode Explore(TestFilter filter)
{
return _engineRunner.Explore(filter)
return PrepareResult(_engineRunner.Explore(filter))
.Aggregate(TEST_RUN_ELEMENT, TestPackage.Name, TestPackage.FullName).Xml;
}

Expand All @@ -185,8 +185,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

private bool _disposed = false;

/// <summary>
/// Dispose of this object.
/// </summary>
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine/Services/DriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace NUnit.Engine.Services
/// </summary>
public class DriverService : Service, IDriverService
{
IList<IDriverFactory> _factories = new List<IDriverFactory>();
readonly IList<IDriverFactory> _factories = new List<IDriverFactory>();

#region IDriverService Members

Expand Down