Skip to content

Commit

Permalink
Merge pull request #1550 from nunit/port-1507
Browse files Browse the repository at this point in the history
Port issue 1507 from 3.19 release
  • Loading branch information
CharliePoole authored Dec 21, 2024
2 parents 7d32a0d + 0bf571e commit 3887e1b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
51 changes: 40 additions & 11 deletions src/NUnitConsole/nunit4-console.tests/ConsoleRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using NSubstitute;
using NUnit.ConsoleRunner;
using NUnit.ConsoleRunner.Options;
using NUnit.Engine;
using NUnit.Engine.Extensibility;
Expand All @@ -14,27 +16,46 @@ namespace NUnit.ConsoleRunner
{
class ConsoleRunnerTests
{
private ITestEngine _testEngine;
private IResultService _resultService;

[SetUp]
public void SetUp()
{
_testEngine = Substitute.For<ITestEngine>();
_resultService = new FakeResultService();

_testEngine.Services.GetService<IResultService>().Returns(_resultService);
}

[Test]
public void ThrowsNUnitEngineExceptionWhenTestResultsAreNotWriteable()
{
using (var testEngine = new TestEngine())
{
testEngine.Services.Add(new FakeResultService());
testEngine.Services.Add(new TestFilterService());
testEngine.Services.Add(Substitute.For<IService, IExtensionService>());
((FakeResultService)_resultService).ThrowsUnauthorizedAccessException = true;

var consoleRunner = new ConsoleRunner(testEngine, ConsoleMocks.Options("mock-assembly.dll"), new ColorConsoleWriter());
var consoleRunner = new ConsoleRunner(_testEngine, ConsoleMocks.Options("mock-assembly.dll"), new ColorConsoleWriter());

var ex = Assert.Throws<NUnitEngineException>(() => { consoleRunner.Execute(); });
Assert.That(ex, Has.Message.EqualTo("The path specified in --result TestResult.xml could not be written to"));
}
var ex = Assert.Throws<NUnitEngineException>(() => { consoleRunner.Execute(); });
Assert.That(ex, Has.Message.EqualTo("The path specified in --result TestResult.xml could not be written to"));
}

[Test]
public void ThrowsNUnitExceptionWhenTeamcityOptionIsSpecifiedButNotAvailable()
{
var ex = Assert.Throws<NUnitEngineException>(
() => new ConsoleRunner(_testEngine, ConsoleMocks.Options("mock-assembly.dll", "--teamcity"), new ColorConsoleWriter()));

Assert.That(ex, Has.Message.Contains("teamcity"));
}
}

internal class FakeResultService : Service, IResultService
{
public bool ThrowsUnauthorizedAccessException;

public string[] Formats
{

get
{
return new[] { "nunit3" };
Expand All @@ -43,15 +64,23 @@ public string[] Formats

public IResultWriter GetResultWriter(string format, object[] args)
{
return new FakeResultWriter();
return new FakeResultWriter(this);
}
}

internal class FakeResultWriter : IResultWriter
{
private FakeResultService _service;

public FakeResultWriter(FakeResultService service)
{
_service = service;
}

public void CheckWritability(string outputPath)
{
throw new UnauthorizedAccessException();
if (_service.ThrowsUnauthorizedAccessException)
throw new UnauthorizedAccessException();
}

public void WriteResultFile(XmlNode resultNode, string outputPath)
Expand Down
19 changes: 17 additions & 2 deletions src/NUnitConsole/nunit4-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class ConsoleRunner
// ourselves so as to stay in that range.
private const int MAXIMUM_RETURN_CODE_ALLOWED = 100; // In case we are running on Unix

private const string EVENT_LISTENER_EXTENSION_PATH = "/NUnit/Engine/TypeExtensions/ITestEventListener";
private const string TEAMCITY_EVENT_LISTENER = "NUnit.Engine.Listeners.TeamCityEventListener";

private const string INDENT4 = " ";
private const string INDENT6 = " ";
private const string INDENT8 = " ";
Expand Down Expand Up @@ -62,12 +65,24 @@ public ConsoleRunner(ITestEngine engine, ConsoleOptions options, ExtendedTextWri
_filterService = _engine.Services.GetService<ITestFilterService>();
_extensionService = _engine.Services.GetService<IExtensionService>();

// TODO: Exit with error if any of the services are not found

if (_options.TeamCity)
{
bool teamcityInstalled = false;
foreach (var node in _extensionService.GetExtensionNodes(EVENT_LISTENER_EXTENSION_PATH))
if (teamcityInstalled = node.TypeName == TEAMCITY_EVENT_LISTENER)
break;

if (!teamcityInstalled) throw new NUnitEngineException("Option --teamcity specified but the extension is not installed.");
}

// Enable TeamCityEventListener immediately, before the console is redirected
_extensionService?.EnableExtension("NUnit.Engine.Listeners.TeamCityEventListener", _options.TeamCity);
_extensionService.EnableExtension("NUnit.Engine.Listeners.TeamCityEventListener", _options.TeamCity);
}

/// <summary>
/// Executes tests according to the provided commandline options.
/// Executes tests according to the provided command-line options.
/// </summary>
/// <returns></returns>
public int Execute()
Expand Down
19 changes: 17 additions & 2 deletions src/NUnitConsole/nunit4-netcore-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class ConsoleRunner
// ourselves so as to stay in that range.
private const int MAXIMUM_RETURN_CODE_ALLOWED = 100; // In case we are running on Unix

private const string EVENT_LISTENER_EXTENSION_PATH = "/NUnit/Engine/TypeExtensions/ITestEventListener";
private const string TEAMCITY_EVENT_LISTENER = "NUnit.Engine.Listeners.TeamCityEventListener";

private const string INDENT4 = " ";
private const string INDENT6 = " ";
private const string INDENT8 = " ";
Expand Down Expand Up @@ -62,12 +65,24 @@ public ConsoleRunner(ITestEngine engine, ConsoleOptions options, ExtendedTextWri
_filterService = _engine.Services.GetService<ITestFilterService>();
_extensionService = _engine.Services.GetService<IExtensionService>();

// TODO: Exit with error if any of the services are not found

if (_options.TeamCity)
{
bool teamcityInstalled = false;
foreach (var node in _extensionService.GetExtensionNodes(EVENT_LISTENER_EXTENSION_PATH))
if (teamcityInstalled = node.TypeName == TEAMCITY_EVENT_LISTENER)
break;

if (!teamcityInstalled) throw new NUnitEngineException("Option --teamcity specified but the extension is not installed.");
}

// Enable TeamCityEventListener immediately, before the console is redirected
_extensionService?.EnableExtension("NUnit.Engine.Listeners.TeamCityEventListener", _options.TeamCity);
_extensionService.EnableExtension("NUnit.Engine.Listeners.TeamCityEventListener", _options.TeamCity);
}

/// <summary>
/// Executes tests according to the provided commandline options.
/// Executes tests according to the provided command-line options.
/// </summary>
/// <returns></returns>
public int Execute()
Expand Down

0 comments on commit 3887e1b

Please sign in to comment.