Skip to content

JUnit report generation fails when captured standard output contains bad characters #185

@manison

Description

@manison

Describe the bug
I'm testing MTP support for JUnit test reports from #171. When the test writes some "invalid" characters to standard output, report generation fails and the test runner crashes with exception. (In our specific case the test for some yet to investigate reason writes ANSI escape sequences.)

  • Test Framework: MSTest
  • .NET project version, sdk version: net8.0
  • Command line used: dotnet run -- --report-junit --report-junit-filename TestResults.xml
  • OS: Windows

Expected behavior
JUnit XML report is successfully created (e.g. with bad chars encoded as entities).

Other details

Here's a minimal repro project:

namespace JUnitXmlInvalidCharFail
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class JUnitXmlInvalidCharFailTests
    {
        [TestMethod]
        public void Test()
        {
            Console.WriteLine("\x1B[4mHello");
        }
    }
}
<Project Sdk="MSTest.Sdk/3.10.4">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="JunitXml.TestLogger" Version="6.1.1-pre.429" />
  </ItemGroup>

</Project>

Here's the exception:

Unhandled exception. System.ArgumentException: 'exadecimal value 0x1B, is an invalid character.
   at System.Xml.XmlEncodedRawTextWriter.WriteCDataSection(String text)
   at System.Xml.XmlEncodedRawTextWriter.WriteCData(String text)
   at System.Xml.XmlWellFormedWriter.WriteCData(String text)
   at System.Xml.Linq.ElementWriter.WriteElement(XElement e)
   at System.Xml.Linq.XElement.WriteTo(XmlWriter writer)
   at System.Xml.Linq.XContainer.WriteContentTo(XmlWriter writer)
   at System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
   at Microsoft.VisualStudio.TestPlatform.Extension.Junit.Xml.TestLogger.JunitXmlSerializer.Serialize(LoggerConfiguration loggerConfiguration, TestRunConfiguration runConfiguration, List`1 results, List`1 messages)
   at Spekt.TestLogger.Core.TestRunCompleteWorkflow.Complete(ITestRun testRun, IReadOnlyCollection`1 testAttachmentInfos) in /home/runner/work/testlogger/testlogger/src/TestLogger/Core/TestRunCompleteWorkflow.cs:line 43
   at Spekt.TestReporter.TestReporter.OnTestSessionFinishingAsync(SessionUid sessionUid, CancellationToken cancellationToken) in /home/runner/work/testlogger/testlogger/src/TestLogger/TestReporter.cs:line 91
   at Microsoft.Testing.Platform.Hosts.CommonHost.NotifyTestSessionEndAsync(SessionUid sessionUid, BaseMessageBus baseMessageBus, ServiceProvider serviceProvider, CancellationToken cancellationToken) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 213
   at Microsoft.Testing.Platform.Hosts.CommonHost.ExecuteRequestAsync(ProxyOutputDevice outputDevice, ITestSessionContext testSessionInfo, ServiceProvider serviceProvider, BaseMessageBus baseMessageBus, ITestFramework testFramework, ClientInfo client) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 144
   at Microsoft.Testing.Platform.Hosts.ConsoleTestHost.InternalRunAsync() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ConsoleTestHost.cs:line 83
   at Microsoft.Testing.Platform.Hosts.ConsoleTestHost.InternalRunAsync() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ConsoleTestHost.cs:line 115
   at Microsoft.Testing.Platform.Hosts.CommonHost.RunTestAppAsync(CancellationToken testApplicationCancellationToken) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 115
   at Microsoft.Testing.Platform.Hosts.CommonHost.RunAsync() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 38
   at Microsoft.Testing.Platform.Hosts.CommonHost.RunAsync() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 74
   at Microsoft.Testing.Platform.Builder.TestApplication.RunAsync() in /_/src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs:line 222
   at JUnitXmlInvalidCharFail.MicrosoftTestingPlatformEntryPoint.Main(String[] args) in C:\Users\manison\Projects\testlogger.bug\obj\Debug\net8.0\MicrosoftTestingPlatformEntryPoint.cs:line 18
   at JUnitXmlInvalidCharFail.MicrosoftTestingPlatformEntryPoint.<Main>(String[] args)

Additional context
I tried to disable standard output capture with --test-parameter StoreConsoleOutput=false with no success. Is there even way some to pass the parameters to the logger?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions