nspec/NSpec

XUnitFormatter not emitting with Unicode byte order mark

BennieCopeland opened this issue · 3 comments

Trying to use this to import test results into TFS. Since the generated XML file is listed as using encoding="utf-16", but not generating a BOM, TFS is complaining.

I ended up changing the output code to look like the following to fix it:

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

@BennieCopeland we're adding a specific test for this issue, which basically reads actual BOM from XML output file and compares that with UTF16 encoding BOM in memory:

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

    byte[] expected = encoding.GetPreamble();

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

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

        actual.ShouldBeEquivalentTo(expected);
    }
}

Only to find that this test passes both with fixed code as well as original code: generated XML file in both cases starts with [255, 254] bytes.

Are you able to double check if original generated XML was actually missing BOM, or maybe it was missing something else?

Has said, not sure this really addresses the original problem: at least from unit tests, the change was not impacting on behaviour. Anyway, currently PR #181 closes this.