Skip to content

Commit

Permalink
Backport new test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
joca-bt committed Nov 26, 2024
1 parent 279a154 commit bb1a99e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test-runner/jars/junit-platform-console-standalone-1.10.0.jar filter=lfs diff=lfs merge=lfs -text
test-runner/jars/junit-platform-console-standalone-1.11.3.jar filter=lfs diff=lfs merge=lfs -text
2 changes: 1 addition & 1 deletion test-runner/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
java_import(
name = "junit5",
jars = [
"jars/junit-platform-console-standalone-1.10.0.jar",
"jars/junit-platform-console-standalone-1.11.3.jar",
],
visibility = ["//visibility:public"],
)
Expand Down
3 changes: 0 additions & 3 deletions test-runner/jars/junit-platform-console-standalone-1.10.0.jar

This file was deleted.

Binary file not shown.
71 changes: 38 additions & 33 deletions test-runner/src/main/java/extrarulesjava/testrunner/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import java.util.List;

class Arguments {
private static final String TESTBRIDGE_TEST_ONLY = "TESTBRIDGE_TEST_ONLY";
private static final String XML_OUTPUT_FILE = "XML_OUTPUT_FILE";

public static String[] process(String[] args) {
List<String> arguments = new ArrayList<>();

arguments.add("execute");

addDetails(arguments);
addDisableBanner(arguments);
addFailIfNoTests(arguments);
addReportsDir(arguments);
addSelector(arguments, args[0]);
addSelectors(arguments, args[0]);

return arguments.toArray(new String[arguments.size()]);
}
Expand All @@ -33,42 +32,48 @@ private static void addFailIfNoTests(List<String> arguments) {
}

private static void addReportsDir(List<String> arguments) {
Path file = Path.of(System.getenv(XML_OUTPUT_FILE));
arguments.add("--reports-dir=" + file.getParent());
Path dir = Path.of(System.getenv("XML_OUTPUT_FILE")).getParent();
arguments.add("--reports-dir=%s".formatted(dir));
}

/*
* Bazel's --test_filter option specifies which tests to run. This option is provided to the
* test runner through the environment variable TESTBRIDGE_TEST_ONLY.
/**
* Adds selectors based on the filter expression when the --test_filter option is provided.
*
* The --test_filter option specifies which tests to run. This option is provided to the test
* runner through the TESTBRIDGE_TEST_ONLY environment variable.
*
* Add a selector based on the filter expression when --test_filter is provided. Supported
* filter expressions:
* - package,
* - package.class,
* - package.class#method.
* Limitations:
* - Does not support parameterized tests.
* - Does not support nested tests.
* - Assumes that the filter expression is valid.
*/
private static void addSelector(List<String> arguments, String jar) {
String filterExpression = System.getenv(TESTBRIDGE_TEST_ONLY);

if (filterExpression != null) {
if (filterExpression.contains("#")) {
arguments.add("--select-method=" + filterExpression);
} else if (isClass(filterExpression)) {
arguments.add("--select-class=" + filterExpression);
} else {
arguments.add("--select-package=" + filterExpression);
private static void addSelectors(List<String> arguments, String jar) {
String filterExpression = System.getenv("TESTBRIDGE_TEST_ONLY");

if (filterExpression == null) {
arguments.add("--scan-classpath=%s".formatted(jar));
return;
}

filterExpression = adjustForIntellijIdea(filterExpression);

for (var component : filterExpression.split("\\|(?![^()]+\\))")) {
String[] names = component.split("#");

String clazz = names[0];

if (names.length == 1) {
arguments.add("--select-class=%s".formatted(clazz));
continue;
}

for (var method : names[1].replaceAll("^\\(|\\)$", "").split("\\|")) {
arguments.add("--select-method=%s#%s".formatted(clazz, method));
}
} else {
arguments.add("--scan-classpath=" + jar);
}
}

private static boolean isClass(String name) {
try {
Class.forName(name);
return true;
} catch (Exception exception) {
return false;
}
private static String adjustForIntellijIdea(String filterExpression) {
return filterExpression.replaceAll("[#$](\\||$)", "$1");
}
}
30 changes: 16 additions & 14 deletions test-runner/src/main/java/extrarulesjava/testrunner/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
Expand All @@ -17,31 +18,31 @@
import static javax.xml.transform.OutputKeys.INDENT;

class Reports {
private static final String XML_OUTPUT_FILE = "XML_OUTPUT_FILE";

public static void process() {
try {
Path file = Path.of(System.getenv(XML_OUTPUT_FILE));
Path file = Path.of(System.getenv("XML_OUTPUT_FILE"));
Document document = parseAndMergeReports(file.getParent());
writeReport(file, document);
writeReport(document, file);
} catch (Exception exception) {
String message = "Error processing reports.";
String message = "Could not process reports.";
throw new JUnitException(message, exception);
}
}

/*
* Add the display name to the name, but only when it starts with [, otherwise IntelliJ IDEA's
* navigation will break. Remove parentheses and everything after from the name for the same
* reason.
/**
* Fixes the name of a testcase:
* - Appends the display name for parameterized tests.
* - Removes parentheses, as IntelliJ IDEA's navigation will break if the name contains
* parentheses.
*/
private static void fixTestcaseName(Element testcase) {
String name = testcase.getAttribute("name");
String displayName = getTestcaseDisplayName(testcase);

name = name.substring(0, name.indexOf('('));

if (!displayName.equals(name) && displayName.startsWith("[")) {
// Parameterized test?
if (displayName.startsWith("[")) {
name = "%s %s".formatted(name, displayName);
}

Expand All @@ -56,7 +57,7 @@ private static void fixTestcaseName(Element testcase) {
*/
private static String getTestcaseDisplayName(Element testcase) {
Element systemOut = (Element) testcase.getElementsByTagName("system-out").item(0);
String[] properties = systemOut.getFirstChild().getNodeValue().trim().split("\n", -1);
String[] properties = systemOut.getFirstChild().getNodeValue().trim().split("\n");
return properties[1].substring(14);
}

Expand All @@ -69,13 +70,14 @@ private static Document parseAndMergeReports(Path dir) throws Exception {
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document = factory.newDocumentBuilder().newDocument();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.createElement("testsuites");

document.appendChild(root);

for (var file : files) {
Document subdocument = factory.newDocumentBuilder().parse(file.toFile());
Document subdocument = builder.parse(file.toFile());
NodeList testsuites = subdocument.getElementsByTagName("testsuite");

for (int i = 0, sizei = testsuites.getLength(); i < sizei; i++) {
Expand All @@ -94,7 +96,7 @@ private static Document parseAndMergeReports(Path dir) throws Exception {
return document;
}

private static void writeReport(Path file, Document document) throws Exception {
private static void writeReport(Document document, Path file) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public static void main(String[] args) {
PrintWriter err = new PrintWriter(System.err, true, UTF_8);

args = Arguments.process(args);

CommandResult<?> result = ConsoleLauncher.run(out, err, args);

Reports.process();

System.exit(result.getExitCode());
Expand Down

0 comments on commit bb1a99e

Please sign in to comment.