From af4a7f888b9bc05605f98a2784f734d5b172c820 Mon Sep 17 00:00:00 2001 From: codito Date: Wed, 5 Jul 2023 10:52:58 +0530 Subject: [PATCH] Illegal xml char (#46) * Use xml sanitizer from testlogger core. * Use DisplayName for reporting test case name. --------- Co-authored-by: Michael J Conrad <32316111+Siphonophora@users.noreply.github.com> --- scripts/dependencies.props | 2 +- .../Xunit.Xml.TestLogger.TestAdapter.csproj | 1 + .../XunitXmlSerializer.cs | 24 ++++++++++--------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/scripts/dependencies.props b/scripts/dependencies.props index c7142c8..a4e432c 100644 --- a/scripts/dependencies.props +++ b/scripts/dependencies.props @@ -4,7 +4,7 @@ 1.3.2 15.7.2 4.9.0 - 3.0.86 + 3.0.122 15.5.0 diff --git a/src/Xunit.Xml.TestLogger.TestAdapter/Xunit.Xml.TestLogger.TestAdapter.csproj b/src/Xunit.Xml.TestLogger.TestAdapter/Xunit.Xml.TestLogger.TestAdapter.csproj index 503e5c6..d4cdf6c 100644 --- a/src/Xunit.Xml.TestLogger.TestAdapter/Xunit.Xml.TestLogger.TestAdapter.csproj +++ b/src/Xunit.Xml.TestLogger.TestAdapter/Xunit.Xml.TestLogger.TestAdapter.csproj @@ -29,4 +29,5 @@ + diff --git a/src/Xunit.Xml.TestLogger/XunitXmlSerializer.cs b/src/Xunit.Xml.TestLogger/XunitXmlSerializer.cs index df7eb2f..740fba6 100644 --- a/src/Xunit.Xml.TestLogger/XunitXmlSerializer.cs +++ b/src/Xunit.Xml.TestLogger/XunitXmlSerializer.cs @@ -39,6 +39,8 @@ public class XunitXmlSerializer : ITestResultSerializer { "Test Method Cleanup Failure", "test-method-cleanup" } }; + public IInputSanitizer InputSanitizer { get; } = new InputSanitizerXml(); + public string Serialize( LoggerConfiguration loggerConfiguration, TestRunConfiguration runConfiguration, @@ -167,8 +169,8 @@ private static XElement CreateErrorElement(TestResultInfo result) private static XElement CreateFailureElement(string exceptionType, string message, string stackTrace) { XElement failureElement = new XElement("failure", new XAttribute("exception-type", exceptionType)); - failureElement.Add(new XElement("message", message.ReplaceInvalidXmlChar())); - failureElement.Add(new XElement("stack-trace", stackTrace.ReplaceInvalidXmlChar())); + failureElement.Add(new XElement("message", message)); + failureElement.Add(new XElement("stack-trace", stackTrace)); return failureElement; } @@ -249,7 +251,7 @@ private static XElement CreateTestElement(TestResultInfo result) { var element = new XElement( "test", - new XAttribute("name", result.TestCase.DisplayName.ReplaceInvalidXmlChar()), + new XAttribute("name", result.DisplayName), new XAttribute("type", result.FullTypeName), new XAttribute("method", result.Method), new XAttribute("time", result.Duration.TotalSeconds.ToString("F7", CultureInfo.InvariantCulture)), @@ -265,33 +267,33 @@ private static XElement CreateTestElement(TestResultInfo result) else if (m.Category == "skipReason") { // Using the self-defined category skipReason for now - element.Add(new XElement("reason", new XCData(m.Text.ReplaceInvalidXmlChar()))); + element.Add(new XElement("reason", new XCData(m.Text))); } } if (!string.IsNullOrWhiteSpace(stdOut.ToString())) { - element.Add(new XElement("output", stdOut.ToString().ReplaceInvalidXmlChar())); + element.Add(new XElement("output", stdOut.ToString())); } - var fileName = result.TestCase.CodeFilePath; + var fileName = result.CodeFilePath; if (!string.IsNullOrWhiteSpace(fileName)) { element.Add(new XElement("source-file", fileName)); - element.Add(new XElement("source-line", result.TestCase.LineNumber)); + element.Add(new XElement("source-line", result.LineNumber)); } if (result.Outcome == TestOutcome.Failed) { element.Add(new XElement( "failure", - new XElement("message", result.ErrorMessage.ReplaceInvalidXmlChar()), - new XElement("stack-trace", result.ErrorStackTrace.ReplaceInvalidXmlChar()))); + new XElement("message", result.ErrorMessage), + new XElement("stack-trace", result.ErrorStackTrace))); } - if (result.TestCase.Traits != null) + if (result.Traits != null) { - var traits = from trait in result.TestCase.Traits + var traits = from trait in result.Traits select new XElement("trait", new XAttribute("name", trait.Name), new XAttribute("value", trait.Value)); element.Add(new XElement("traits", traits)); }