Skip to content

Commit a707e94

Browse files
authored
Fix Excessive Encoding in Test Logs (#92286)
* Removed special encoding that was rendering the test logs near impossible to read properly. * Adjusted the offending test to print the invalid character's hex code instead, and fixed it alongside its sibling test because they didn't handle all correct/incorrect cases properly. * Added special handling for illegal XML characters in the test results' XML logs. * Simplified the sanitizing algorithm to one pass, as per Dan's feedback.
1 parent e500dad commit a707e94

File tree

3 files changed

+89
-22
lines changed

3 files changed

+89
-22
lines changed

src/tests/Common/XUnitWrapperLibrary/TestSummary.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.IO;
65
using System.Collections.Generic;
6+
using System.IO;
77
using System.Text;
88
using System.Xml;
9+
910
namespace XUnitWrapperLibrary;
1011

1112
public class TestSummary
@@ -43,8 +44,14 @@ public string ToXmlString()
4344
testResultSb.Append($@"<test name=""{Name}"" type=""{ContainingTypeName}"""
4445
+ $@" method=""{MethodName}"" time=""{Duration.TotalSeconds:F6}""");
4546

47+
// GH dotnet/runtime issue #92092: It is possible for tests to have
48+
// illegal XML characters in their output. So, we have to sanitize said
49+
// output before writing it down to the XML log because otherwise, the
50+
// python XML parser crashes. The way we clean it is by replacing said
51+
// characters with their hexadecimal notation in SanitizeOutput().
52+
4653
string outputElement = !string.IsNullOrWhiteSpace(Output)
47-
? $"<output><![CDATA[{XmlConvert.EncodeName(Output)}]]></output>"
54+
? $"<output><![CDATA[{SanitizeOutput(Output)}]]></output>"
4855
: string.Empty;
4956

5057
if (Exception is not null)
@@ -95,12 +102,36 @@ public string ToXmlString()
95102

96103
return testResultSb.ToString();
97104
}
105+
106+
private string SanitizeOutput(string output)
107+
{
108+
StringBuilder sanitizedOutput = new StringBuilder();
109+
110+
// Check each character in the given test's output:
111+
// * If it's a legal XML character, then write it as is.
112+
// * Otherwise, write it in its hexadecimal notation.
113+
114+
foreach (char ch in output)
115+
{
116+
if (XmlConvert.IsXmlChar(ch))
117+
{
118+
sanitizedOutput.Append(ch);
119+
}
120+
else
121+
{
122+
int charCode = Convert.ToInt32(ch);
123+
sanitizedOutput.AppendFormat("_0x{0}_", charCode.ToString("X"));
124+
}
125+
}
126+
127+
return sanitizedOutput.ToString();
128+
}
98129
}
99130

100-
public int PassedTests { get; private set; }
101-
public int FailedTests { get; private set; }
102-
public int SkippedTests { get; private set; }
103-
public int TotalTests { get; private set; }
131+
public int PassedTests { get; private set; } = 0;
132+
public int FailedTests { get; private set; } = 0;
133+
public int SkippedTests { get; private set; } = 0;
134+
public int TotalTests { get; private set; } = 0;
104135

105136
private readonly List<TestResult> _testResults = new();
106137
private DateTime _testRunStart = DateTime.Now;

src/tests/JIT/Regression/VS-ia64-JIT/M00/b119026/bug.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,50 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
//
44

5+
using System;
56
using Xunit;
7+
68
public class test
79
{
810
static short si16;
911
static uint su32;
12+
1013
[Fact]
11-
public static void TestEntryPoint()
14+
public static int TestEntryPoint()
1215
{
13-
si16 = -1;
14-
su32 = (uint)si16;
15-
System.Console.WriteLine(su32);
16-
if (su32 == uint.MaxValue)
16+
int exitCode = -1;
17+
short i16 = -1;
18+
uint u32 = (uint) i16;
19+
20+
Console.WriteLine(u32);
21+
22+
if (u32 == uint.MaxValue)
23+
{
1724
System.Console.WriteLine("Pass");
25+
exitCode = 100;
26+
}
1827
else
28+
{
1929
System.Console.WriteLine("Fail");
20-
short i16 = -1;
21-
uint u32 = (uint)i16;
22-
System.Console.WriteLine(u32);
23-
if (u32 == uint.MaxValue)
30+
exitCode = 101;
31+
}
32+
33+
si16 = -1;
34+
su32 = (uint) si16;
35+
36+
Console.WriteLine(su32);
37+
38+
if (su32 == uint.MaxValue)
2439
{
2540
System.Console.WriteLine("Pass");
2641
}
2742
else
2843
{
2944
System.Console.WriteLine("Fail");
45+
if (exitCode == 100)
46+
exitCode = 101;
3047
}
48+
49+
return exitCode;
3150
}
3251
}

src/tests/JIT/Regression/VS-ia64-JIT/M00/b119026/charbug.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,50 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
//
44

5+
using System;
56
using Xunit;
7+
68
public class test
79
{
810
static sbyte si8;
911
static char sc;
12+
1013
[Fact]
1114
public static int TestEntryPoint()
1215
{
16+
int exitCode = -1;
1317
sbyte i8 = -1;
14-
char c = (char)i8;
15-
System.Console.WriteLine("{0}: {1}", c, ((ushort)c));
18+
char c = (char) i8;
19+
20+
Console.WriteLine("{0:X}: {1}", Convert.ToUInt32(c), ((ushort)c));
21+
1622
if (c == char.MaxValue)
17-
System.Console.WriteLine("Pass");
23+
{
24+
Console.WriteLine("Pass");
25+
exitCode = 100;
26+
}
1827
else
19-
System.Console.WriteLine("Fail");
28+
{
29+
Console.WriteLine("Fail");
30+
exitCode = 101;
31+
}
32+
2033
si8 = -1;
2134
sc = (char)si8;
22-
System.Console.WriteLine("{0}: {1}", sc, ((ushort)sc));
35+
36+
Console.WriteLine("{0:X}: {1}", Convert.ToUInt32(sc), ((ushort)sc));
37+
2338
if (sc == char.MaxValue)
2439
{
2540
System.Console.WriteLine("Pass");
26-
return 100;
2741
}
2842
else
2943
{
3044
System.Console.WriteLine("Fail");
31-
return 1;
45+
if (exitCode == 100)
46+
exitCode = 101;
3247
}
48+
49+
return exitCode;
3350
}
3451
}

0 commit comments

Comments
 (0)