Skip to content

Commit

Permalink
nunit#1 Handling of TeamCity messages is different between the engine…
Browse files Browse the repository at this point in the history
… and nunitlite - add tests for nunitlite
  • Loading branch information
Nikolay Pianikov authored and Nikolay Pianikov committed Sep 28, 2016
1 parent 7d7d47a commit 76efde8
Show file tree
Hide file tree
Showing 13 changed files with 1,135 additions and 134 deletions.
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Task("InitializeForIntegrationTests")
CleanDirectories(TEST_NUNIT_DIR + "**/*.*");
CleanDirectories(TEST_PACKAGES_DIR + "**/*.*");

NuGetInstall(new [] {"NUnit.Runners", "NUnit"}, new NuGetInstallSettings()
NuGetInstall(new [] {"NUnit.Runners", "NUnit", "NUnitLite"}, new NuGetInstallSettings()
{
OutputDirectory = TEST_NUNIT_DIR,
Source = PRERELEASE_PACKAGE_SOURCE,
Expand Down
1 change: 1 addition & 0 deletions restore.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tools\nuget.exe install NUnit.Runners -Prerelease -Source "https://www.myget.org/F/nunit/api/v2" -OutputDirectory packages
143 changes: 12 additions & 131 deletions src/extension/TeamCityEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace NUnit.Engine.Listeners
public class TeamCityEventListener : ITestEventListener
{
private static readonly ServiceMessageWriter ServiceMessageWriter = new ServiceMessageWriter();
private readonly TeamCityMessageConverter _messageConverter = new TeamCityMessageConverter();
private readonly TextWriter _outWriter;
private readonly Dictionary<string, string> _refs = new Dictionary<string, string>();
private readonly Dictionary<string, int> _blockCounters = new Dictionary<string, int>();
Expand All @@ -63,138 +64,27 @@ public void OnTestEvent(string report)
doc.LoadXml(report);

var testEvent = doc.FirstChild;
RegisterMessage(testEvent);
}

#endregion

public void RegisterMessage(XmlNode testEvent)
{
if (testEvent == null) throw new ArgumentNullException("testEvent");

var messageName = testEvent.Name;
if (string.IsNullOrEmpty(messageName))
TeamCityMessageConverter.NUnitMessage message;
if (!TeamCityMessageConverter.NUnitMessage.TryParse(testEvent, out message))
{
return;
}

messageName = messageName.ToLowerInvariant();
if (messageName == "start-run")
{
_refs.Clear();
return;
}

var fullName = testEvent.GetAttribute("fullname");
if (string.IsNullOrEmpty(fullName))
{
return;
}

var id = testEvent.GetAttribute("id");
if (id == null)
{
id = string.Empty;
}

var parentId = testEvent.GetAttribute("parentId");
var flowId = ".";
if (parentId != null)
{
// NUnit 3 case
string rootId;
flowId = TryFindRootId(parentId, out rootId) ? rootId : id;
}
else
{
// NUnit 2 case
if (!string.IsNullOrEmpty(id))
{
var idParts = id.Split('-');
if (idParts.Length == 2)
{
flowId = idParts[0];
}
}
}

string testFlowId;
if (id != flowId && parentId != null)
{
testFlowId = id;
}
else
var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
testFlowId = flowId;
if (testFlowId == null)
foreach (var tcMessage in _messageConverter.ConvertMessage(message))
{
testFlowId = id;
ServiceMessageWriter.Write(writer, tcMessage);
}
}

switch (messageName.ToLowerInvariant())
{
case "start-suite":
_refs[id] = parentId;
StartSuiteCase(parentId, flowId, fullName);
break;

case "test-suite":
_refs.Remove(id);
TestSuiteCase(parentId, flowId, fullName);
break;

case "start-test":
_refs[id] = parentId;
CaseStartTest(id, flowId, parentId, testFlowId, fullName);
break;

case "test-case":
try
{
if (!_refs.Remove(id))
{
// When test without starting
CaseStartTest(id, flowId, parentId, testFlowId, fullName);
}

var result = testEvent.GetAttribute("result");
if (string.IsNullOrEmpty(result))
{
break;
}

switch (result.ToLowerInvariant())
{
case "passed":
OnTestFinished(testFlowId, testEvent, fullName);
break;

case "inconclusive":
OnTestInconclusive(testFlowId, testEvent, fullName);
break;

case "skipped":
OnTestSkipped(testFlowId, testEvent, fullName);
break;

case "failed":
OnTestFailed(testFlowId, testEvent, fullName);
break;
}
}
finally
{
if (id != flowId && parentId != null)
{
OnFlowFinished(id);
}
}

break;
}
_outWriter.WriteLine(sb.ToString());
}

#endregion

private void CaseStartTest(string id, string flowId, string parentId, string testFlowId, string fullName)
{
if (id != flowId && parentId != null)
Expand Down Expand Up @@ -282,7 +172,7 @@ private bool TryFindRootId(string id, out string rootId)

private void TrySendOutput(string flowId, XmlNode message, string fullName)
{
if (message == null) throw new ArgumentNullException("message");
if (message == null) throw new ArgumentNullException("message");

var output = message.SelectSingleNode("output");
if (output == null)
Expand Down Expand Up @@ -415,15 +305,6 @@ private void OnTestInconclusive(string flowId, XmlNode message, string fullName)
new ServiceMessageAttr(ServiceMessageAttr.Names.FlowId, flowId)));
}

private void Write(ServiceMessage serviceMessage)
{
var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
ServiceMessageWriter.Write(writer, serviceMessage);
}

_outWriter.WriteLine(sb.ToString());
}

}
}
Loading

0 comments on commit 76efde8

Please sign in to comment.