Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape invalid XML chars in testCase and testSuite names #284

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void toXml(XMLStreamWriter xml, TestData test) throws XMLStreamException
}

xml.writeStartElement("testcase");
xml.writeAttribute("name", name);
xml.writeAttribute("name", escapeIllegalCharacters(name));
xml.writeAttribute("classname", LegacyReportingUtils.getClassName(testPlan, id));

/* @Nullable */ Duration maybeDuration = test.getDuration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import static com.github.bazel_contrib.contrib_rules_jvm.junit5.SafeXml.escapeIllegalCharacters;
import static com.github.bazel_contrib.contrib_rules_jvm.junit5.SafeXml.writeTextElement;

import java.util.Collection;
Expand All @@ -19,7 +20,7 @@ public void toXml(XMLStreamWriter xml, TestData suite, Collection<TestData> test
throws XMLStreamException {
xml.writeStartElement("testsuite");

xml.writeAttribute("name", suite.getId().getLegacyReportingName());
xml.writeAttribute("name", escapeIllegalCharacters(suite.getId().getLegacyReportingName()));
xml.writeAttribute("tests", String.valueOf(tests.size()));

int errors = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,38 @@ public void ensureOutputsAreProperlyEscaped() {
assertEquals("&#27;[31mAlso bad!&#27;[0m", text);
}

@Test
public void ensureTestCaseNamesAreProperlyEscaped() {
var testDescriptor = new StubbedTestDescriptor(createId("Weird\bname"));
var identifier = TestIdentifier.from(testDescriptor);

var testCaseData = new TestData(identifier);
testCaseData.mark(TestExecutionResult.successful());

Document xml = generateTestXml(Mockito.mock(TestPlan.class), testCaseData);

Node item = xml.getElementsByTagName("testcase").item(0);
String testName = item.getAttributes().getNamedItem("name").getNodeValue();

assertEquals("[engine:mocked]/[class:ExampleTest]/[method:Weird&#8;name", testName);
}

@Test
public void ensureTestSuiteNamesAreProperlyEscaped() {
var testDescriptor = new StubbedTestDescriptor(createId("Weird\bname"));
var identifier = TestIdentifier.from(testDescriptor);

var testSuiteData = new TestData(identifier);
testSuiteData.mark(TestExecutionResult.successful());

Document xml = generateTestXml(Mockito.mock(TestPlan.class), testSuiteData, List.of());

Node item = xml.getElementsByTagName("testsuite").item(0);
String testName = item.getAttributes().getNamedItem("name").getNodeValue();

assertEquals("[engine:mocked]/[class:ExampleTest]/[method:Weird&#8;name()]", testName);
}

private Document generateTestXml(TestPlan testPlan, TestData testCase) {
return generateDocument(xml -> new TestCaseXmlRenderer(testPlan).toXml(xml, testCase));
}
Expand Down
Loading