Skip to content

Commit de2add5

Browse files
committed
Throw exception if --teamcity is specified but the extension is not present
1 parent d238d33 commit de2add5

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

build.cake

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Load the recipe
2-
#load nuget:?package=NUnit.Cake.Recipe&version=1.2.0
2+
#load nuget:?package=NUnit.Cake.Recipe&version=1.2.1-dev00002
33
// Comment out above line and uncomment below for local tests of recipe changes
44
//#load ../NUnit.Cake.Recipe/recipe/*.cake
55

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
22

33
using System;
4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Xml;
67
using NSubstitute;
@@ -14,25 +15,43 @@ namespace NUnit.ConsoleRunner.Tests
1415
{
1516
class ConsoleRunnerTests
1617
{
18+
private ITestEngine _testEngine;
19+
private IResultService _resultService;
20+
21+
[SetUp]
22+
public void Setup()
23+
{
24+
_testEngine = Substitute.For<ITestEngine>();
25+
_resultService = new FakeResultService();
26+
27+
_testEngine.Services.GetService<IResultService>().Returns(_resultService);
28+
}
29+
1730
[Test]
1831
public void ThrowsNUnitEngineExceptionWhenTestResultsAreNotWriteable()
1932
{
20-
using (var testEngine = new TestEngine())
21-
{
22-
testEngine.Services.Add(new FakeResultService());
23-
testEngine.Services.Add(new TestFilterService());
24-
testEngine.Services.Add(Substitute.For<IService, IExtensionService>());
33+
((FakeResultService)_resultService).ThrowsUnauthorizedAccessException = true;
2534

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

28-
var ex = Assert.Throws<NUnitEngineException>(() => { consoleRunner.Execute(); });
29-
Assert.That(ex, Has.Message.EqualTo("The path specified in --result TestResult.xml could not be written to"));
30-
}
37+
var ex = Assert.Throws<NUnitEngineException>(() => { consoleRunner.Execute(); });
38+
Assert.That(ex, Has.Message.EqualTo("The path specified in --result TestResult.xml could not be written to"));
39+
}
40+
41+
[Test]
42+
public void ThrowsNUnitExceptionWhenTeamcityOptionIsSpecifiedButNotAvailable()
43+
{
44+
var ex = Assert.Throws<NUnitEngineException>(
45+
() => new ConsoleRunner(_testEngine, ConsoleMocks.Options("mock-assembly.dll", "--teamcity"), new ColorConsoleWriter()));
46+
47+
Assert.That(ex, Has.Message.Contains("teamcity"));
3148
}
3249
}
3350

3451
internal class FakeResultService : Service, IResultService
3552
{
53+
public bool ThrowsUnauthorizedAccessException;
54+
3655
public string[] Formats
3756
{
3857
get
@@ -43,25 +62,33 @@ public string[] Formats
4362

4463
public IResultWriter GetResultWriter(string format, object[] args)
4564
{
46-
return new FakeResultWriter();
65+
return new FakeResultWriter(this);
4766
}
48-
}
4967

50-
internal class FakeResultWriter : IResultWriter
51-
{
52-
public void CheckWritability(string outputPath)
68+
class FakeResultWriter : IResultWriter
5369
{
54-
throw new UnauthorizedAccessException();
55-
}
70+
private FakeResultService _service;
5671

57-
public void WriteResultFile(XmlNode resultNode, string outputPath)
58-
{
59-
throw new System.NotImplementedException();
60-
}
72+
public FakeResultWriter(FakeResultService service)
73+
{
74+
_service = service;
75+
}
6176

62-
public void WriteResultFile(XmlNode resultNode, TextWriter writer)
63-
{
64-
throw new System.NotImplementedException();
77+
public void CheckWritability(string outputPath)
78+
{
79+
if (_service.ThrowsUnauthorizedAccessException)
80+
throw new UnauthorizedAccessException();
81+
}
82+
83+
public void WriteResultFile(XmlNode resultNode, string outputPath)
84+
{
85+
throw new System.NotImplementedException();
86+
}
87+
88+
public void WriteResultFile(XmlNode resultNode, TextWriter writer)
89+
{
90+
throw new System.NotImplementedException();
91+
}
6592
}
6693
}
6794
}

src/NUnitConsole/nunit3-console/ConsoleRunner.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class ConsoleRunner
2525
// ourselves so as to stay in that range.
2626
private const int MAXIMUM_RETURN_CODE_ALLOWED = 100; // In case we are running on Unix
2727

28+
private const string EVENT_LISTENER_EXTENSION_PATH = "/NUnit/Engine/TypeExtensions/ITestEventListener";
29+
private const string TEAMCITY_EVENT_LISTENER = "NUnit.Engine.Listeners.TeamCityEventListener";
30+
2831
public static readonly int OK = 0;
2932
public static readonly int INVALID_ARG = -1;
3033
public static readonly int INVALID_ASSEMBLY = -2;
@@ -58,8 +61,19 @@ public ConsoleRunner(ITestEngine engine, ConsoleOptions options, ExtendedTextWri
5861
_filterService = _engine.Services.GetService<ITestFilterService>();
5962
_extensionService = _engine.Services.GetService<IExtensionService>();
6063

64+
// TODO: Exit with error if any of the services are not found
65+
66+
if (_options.TeamCity)
67+
{
68+
bool teamcityInstalled = false;
69+
foreach (var node in _extensionService.GetExtensionNodes(EVENT_LISTENER_EXTENSION_PATH))
70+
if (teamcityInstalled = node.TypeName == TEAMCITY_EVENT_LISTENER)
71+
break;
72+
if (!teamcityInstalled) throw new NUnitEngineException("Option --teamcity specified but the extension is not installed.");
73+
}
74+
6175
// Enable TeamCityEventListener immediately, before the console is redirected
62-
_extensionService?.EnableExtension("NUnit.Engine.Listeners.TeamCityEventListener", _options.TeamCity);
76+
_extensionService.EnableExtension("NUnit.Engine.Listeners.TeamCityEventListener", _options.TeamCity);
6377
}
6478

6579
/// <summary>

0 commit comments

Comments
 (0)