Skip to content

Commit

Permalink
Fixes incorrect XML header for encoding #1322 (#1323)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 21, 2022
1 parent 0034460 commit 8f48e9d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since v2.5.2:

- Bug fixes:
- Fixed incorrect XML header for encoding by @BernieWhite.
[#1322](https://github.com/microsoft/PSRule/issues/1322)

## v2.5.2

What's changed since v2.5.1:
Expand Down
13 changes: 5 additions & 8 deletions src/PSRule/Pipeline/Output/NUnit3OutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
Expand All @@ -14,13 +15,8 @@ namespace PSRule.Pipeline.Output
{
internal sealed class NUnit3OutputWriter : SerializationOutputWriter<InvokeResult>
{
private readonly StringBuilder _Builder;

internal NUnit3OutputWriter(PipelineWriter inner, PSRuleOption option)
: base(inner, option)
{
_Builder = new StringBuilder();
}
: base(inner, option) { }

public override void WriteObject(object sendToPipeline, bool enumerateCollection)
{
Expand All @@ -37,7 +33,8 @@ protected override string Serialize(InvokeResult[] o)
Encoding = Encoding.UTF8, // Consider using: Option.Output.GetEncoding()
// Consider using: Indent = true,
};
using var xml = XmlWriter.Create(_Builder, settings);
using var writer = new OutputStringWriter(Option);
using var xml = XmlWriter.Create(writer, settings);
xml.WriteStartDocument(standalone: false);

float time = o.Sum(r => r.Time);
Expand Down Expand Up @@ -107,7 +104,7 @@ protected override string Serialize(InvokeResult[] o)
xml.WriteEndElement();
xml.WriteEndDocument();
xml.Flush();
return _Builder.ToString();
return writer.ToString();
}

private static void VisitFixture(XmlWriter xml, TestFixture fixture)
Expand Down
21 changes: 21 additions & 0 deletions src/PSRule/Pipeline/Output/OutputStringWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.IO;
using System.Text;
using PSRule.Configuration;

namespace PSRule.Pipeline.Output
{
internal sealed class OutputStringWriter : StringWriter
{
private readonly Encoding _Encoding;

public OutputStringWriter(PSRuleOption option)
{
_Encoding = option.Output.GetEncoding();
}

public override Encoding Encoding => _Encoding;
}
}
2 changes: 2 additions & 0 deletions tests/PSRule.Tests/OutputWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ public void NUnit3()
var doc = new XmlDocument();
doc.LoadXml(s);

var declaration = doc.ChildNodes.Item(0) as XmlDeclaration;
Assert.Equal("utf-8", declaration.Encoding);
var xml = doc["test-results"]["test-suite"].OuterXml.Replace(Environment.NewLine, "\r\n");
Assert.Equal("<test-suite type=\"TestFixture\" name=\"TestObject1\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0\" asserts=\"3\" description=\"\"><results><test-case description=\"This is rule 001.\" name=\"TestObject1 -- rule-001\" time=\"0\" asserts=\"0\" success=\"True\" result=\"Success\" executed=\"True\" /><test-case description=\"This is rule 002.\" name=\"TestObject1 -- rule-002\" time=\"0\" asserts=\"0\" success=\"False\" result=\"Failure\" executed=\"True\"><failure><message><![CDATA[Recommendation for rule 002\r\n]]></message><stack-trace><![CDATA[]]></stack-trace></failure></test-case><test-case description=\"This is rule 002.\" name=\"TestObject1 -- rule-002\" time=\"0\" asserts=\"0\" success=\"False\" result=\"Failure\" executed=\"True\"><failure><message><![CDATA[Recommendation for rule 002\r\n]]></message><stack-trace><![CDATA[]]></stack-trace></failure></test-case><test-case description=\"Synopsis &quot;with quotes&quot;.\" name=\"TestObject1 -- rule-002\" time=\"0\" asserts=\"0\" success=\"False\" result=\"Failure\" executed=\"True\"><failure><message><![CDATA[Recommendation for rule 002\r\n]]></message><stack-trace><![CDATA[]]></stack-trace></failure></test-case></results></test-suite>", xml);
}
Expand Down

0 comments on commit 8f48e9d

Please sign in to comment.