Skip to content

Commit

Permalink
XUnitFormatter fixes for issues #176, #177 and #178 (#181)
Browse files Browse the repository at this point in the history
* Closes #177 and #178
* refactor(formatters): pack `using()` blocks
* test(formatters): add test case for issue #177
* test(formatters): XUnitFormatter output XML must start with BOM
* chore(test): don't mix TestFixture classes in XUnitFormatter test
* fix(formatters): XUnitFormatter throws when no options are set
* Closes #176
  • Loading branch information
BennieCopeland authored and BrainCrumbz committed Apr 3, 2017
1 parent a61a3f3 commit 06a1ba4
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 4 deletions.
17 changes: 13 additions & 4 deletions sln/src/NSpec/Domain/Formatters/XUnitFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace NSpec.Domain.Formatters
{
public class XUnitFormatter : IFormatter
{
public XUnitFormatter()
{
Options = new Dictionary<string, string>();
}

public void Write(ContextCollection contexts)
{
StringBuilder sb = new StringBuilder();
Expand All @@ -25,18 +29,23 @@ public void Write(ContextCollection contexts)
xml.WriteAttributeString("skip", contexts.Pendings().Count().ToString());

contexts.Do(c => this.BuildContext(xmlWrapper, c));

xml.WriteEndElement();
xml.Flush();

var results = sb.ToString();
bool didWriteToFile = false;
if (Options.ContainsKey("file"))
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), Options["file"]);

using (StreamWriter ostream = File.CreateText(filePath))
using (var stream = new FileStream(filePath, FileMode.Create))
using (var writer = new StreamWriter(stream, Encoding.Unicode))
{
ostream.WriteLine(results);
writer.WriteLine(results);
Console.WriteLine("Test results published to: {0}".With(filePath));
}

didWriteToFile = true;
}
if (didWriteToFile && Options.ContainsKey("console"))
Expand Down Expand Up @@ -113,4 +122,4 @@ private void ComposePartialName(Context context, StringBuilder contextName)
contextName.Append(context.Name);
}
}
}
}
139 changes: 139 additions & 0 deletions sln/test/NSpec.Tests/describe_XUnitFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using FluentAssertions;
using NSpec.Domain;
using NSpec.Domain.Formatters;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace NSpec.Tests
{
public abstract class describe_XUnitFormatter_base
{
protected class xunit_formatter_sample_spec : nspec
{
void a_context_with_a_pending_example()
{
it["pending example"] = todo;
}

void a_context_with_a_grandchild_example()
{
context["a context with an example"] = () =>
{
it["is passing"] = () => Assert.That(true, Is.True);
};
}

void a_context_without_an_example() { }
}
}

[TestFixture]
public class describe_XUnitFormatter : describe_XUnitFormatter_base
{
[SetUp]
public void Setup()
{
formatter = new XUnitFormatter();

string outDirPath = Path.Combine(
Path.GetTempPath(),
"NSpec.Tests",
nameof(describe_XUnitFormatter));

Directory.CreateDirectory(outDirPath);

outFilePath = Path.Combine(
outDirPath,
Path.ChangeExtension(Path.GetRandomFileName(), "xml"));

formatter.Options = new Dictionary<string, string>()
{
{ "file", outFilePath },
};

var invocation = new RunnerInvocation(
dll: typeof(describe_XUnitFormatter).GetTypeInfo().Assembly.Location,
tags: typeof(xunit_formatter_sample_spec).Name,
formatter: formatter,
failFast: false);

contexts = invocation.Run();
}

[TearDown]
public void TearDown()
{
if (File.Exists(outFilePath))
{
File.Delete(outFilePath);
}
}

[Test]
public void all_output_is_flushed_to_file()
{
string actual = File.ReadAllText(outFilePath);

actual.Should().EndWith("</testsuite></testsuites>\r\n");
}

[Test]
public void output_file_starts_with_utf16_bom()
{
var utf16Encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: true);

byte[] expected = utf16Encoding.GetPreamble();

byte[] actual = new byte[expected.Length];

using (var fstream = new FileStream(outFilePath, FileMode.Open))
{
fstream.Read(actual, 0, actual.Length);

actual.ShouldBeEquivalentTo(expected);
}
}

XUnitFormatter formatter;
string outFilePath;
ContextCollection contexts;
}

[TestFixture]
public class describe_XUnitFormatter_without_options : describe_XUnitFormatter_base
{
[SetUp]
public void Setup()
{
formatter = new XUnitFormatter();

Run(typeof(xunit_formatter_sample_spec));
}

[Test]
public void writing_does_not_throw()
{
formatter.Write(contextCollection);
}

// TODO refactor this with WhenRunningSpecs.when_running_specs

protected void Run(params Type[] types)
{
var tagsFilter = new Tags().Parse("");

var builder = new ContextBuilder(new SpecFinder(types), tagsFilter, new DefaultConventions());

contextCollection = builder.Contexts();

contextCollection.Build();
}

XUnitFormatter formatter;
ContextCollection contextCollection;
}
}

0 comments on commit 06a1ba4

Please sign in to comment.