fix(testing): escape XML-invalid characters in JUnit reports
A theory case containing a control character crashed the JUnit writer after every test had passed, failing the Data test step on all runners. Also clears a stale rejected certificate fingerprint once a later handshake is accepted or the fingerprint is trusted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+48
-12
No files matched your search
@@ -253,8 +253,8 @@ public static class TestRunner
|
||||
foreach (var r in results.OrderBy(r => r.Test.FullName, StringComparer.Ordinal))
|
||||
{
|
||||
w.WriteStartElement("testcase");
|
||||
w.WriteAttributeString("classname", r.Test.Type.FullName);
|
||||
w.WriteAttributeString("name", r.Test.FullName[(r.Test.Type.FullName!.Length + 1)..]);
|
||||
w.WriteAttributeString("classname", Clean(r.Test.Type.FullName!));
|
||||
w.WriteAttributeString("name", Clean(r.Test.FullName[(r.Test.Type.FullName!.Length + 1)..]));
|
||||
w.WriteAttributeString("time", r.Duration.TotalSeconds.ToString("F3", CultureInfo.InvariantCulture));
|
||||
if (r.Outcome == TestOutcome.Failed)
|
||||
{
|
||||
@@ -276,7 +276,29 @@ public static class TestRunner
|
||||
w.WriteEndDocument();
|
||||
}
|
||||
|
||||
private static string Clean(string s) => new(s.Where(XmlConvert.IsXmlChar).ToArray());
|
||||
/// <summary>Escapes characters XML cannot carry (theory cases may contain control characters) as <c>\uXXXX</c>.</summary>
|
||||
private static string Clean(string s)
|
||||
{
|
||||
StringBuilder? sb = null;
|
||||
for (var i = 0; i < s.Length; i++)
|
||||
{
|
||||
var c = s[i];
|
||||
if (XmlConvert.IsXmlChar(c))
|
||||
{
|
||||
sb?.Append(c);
|
||||
continue;
|
||||
}
|
||||
if (char.IsHighSurrogate(c) && i + 1 < s.Length && XmlConvert.IsXmlSurrogatePair(s[i + 1], c))
|
||||
{
|
||||
sb?.Append(c).Append(s[i + 1]);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
sb ??= new StringBuilder(s, 0, i, s.Length + 8);
|
||||
sb.Append(CultureInfo.InvariantCulture, $"\\u{(int)c:X4}");
|
||||
}
|
||||
return sb?.ToString() ?? s;
|
||||
}
|
||||
|
||||
private sealed class Options
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user