-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for transforming Checkstyle XML output to JUnit XML (#283)
Bazel will natively create a `test.xml` for all test runners if they do not create one themselves. For the checkstyle test-runner it would create a test.xml that captures the system-out. ```xml <testsuites> <testsuite name="server-common/server-common-checkstyleexec" tests="1" failures="0" errors="1"> <testcase name="server-common/server-common-checkstyleexec" status="run" duration="5" time="5"><error message="exited with error code 1"></error></testcase> <system-out> Generated test.log (if the file is not UTF-8, then this may be unreadable): <![CDATA[exec ${PAGER:-/usr/bin/less} "$0" || exit 1 Executing tests from //server-common:server-common-checkstyle ----------------------------------------------------------------------------- Checkstyle ends with 1 errors. Starting audit... [ERROR] server-common/src/main/java/org/apache/kafka/queue/KafkaEventQueue.java:30:1: Disallowed import - io.confluent.kafka.util.OpenTelemetryManager. [ImportControl] Audit done.]]> </system-out> </testsuite> </testsuites> ``` JUnit is a well establish format and well known by many CI programs. Having the checkstyle rule itself create a more curated JUnit XML file is in the user's best interest. In this PR, we modify the checkstyle rules to emit a `test.xml` itself, stopping Bazel from implicitly doing so. We have checkstyle emit the XML format _always_ and then use a very minimal Java tool and a provided default XLST (XML stylesheet) to transform the Checkstyle XML to one following JUnit's XML specification. ```xml <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite package="Checkstyle" name="src/main/java/io/confluent/utils/HelloWorldUtils.java" tests="2" errors="2"> <testcase name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck" classname="src/main/java/io/confluent/utils/HelloWorldUtils.java"> <error type="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck" message="Line 3: Using the '.*' form of import should be avoided - java.io.*."/> </testcase> <testcase name="com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck" classname="src/main/java/io/confluent/utils/HelloWorldUtils.java"> <error type="com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck" message="Line 7: Missing a Javadoc comment."/> </testcase> </testsuite> </testsuites> ```
- Loading branch information
Showing
5 changed files
with
118 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | ||
<xsl:output encoding="UTF-8" method="xml"></xsl:output> | ||
|
||
<xsl:template match="/"> | ||
<testsuites> | ||
<testsuite package="Checkstyle"> | ||
<xsl:attribute name="name"> | ||
<xsl:value-of select="//checkstyle/file/@name" /> | ||
</xsl:attribute> | ||
<xsl:attribute name="tests"> | ||
<xsl:value-of select="count(.//error)" /> | ||
</xsl:attribute> | ||
<xsl:attribute name="errors"> | ||
<xsl:value-of select="count(.//error)" /> | ||
</xsl:attribute> | ||
<xsl:for-each select="//checkstyle"> | ||
<xsl:apply-templates /> | ||
</xsl:for-each> | ||
</testsuite> | ||
</testsuites> | ||
</xsl:template> | ||
|
||
<xsl:template match="error"> | ||
<testcase> | ||
<xsl:attribute name="name"> | ||
<xsl:value-of select="@source" /> | ||
</xsl:attribute> | ||
<xsl:attribute name="classname"> | ||
<xsl:value-of select="../@name" /> | ||
</xsl:attribute> | ||
<error> | ||
<xsl:attribute name="type"> | ||
<xsl:value-of select="@source" /> | ||
</xsl:attribute> | ||
<xsl:attribute name="message"> | ||
<xsl:text>Line </xsl:text> | ||
<xsl:value-of select="@line" /> | ||
<xsl:text>: </xsl:text> | ||
<xsl:value-of select="@message" /> | ||
</xsl:attribute> | ||
</error> | ||
</testcase> | ||
</xsl:template> | ||
|
||
</xsl:stylesheet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
load("@rules_java//java:defs.bzl", "java_binary", "java_library") | ||
|
||
java_library( | ||
name = "xml", | ||
srcs = ["XSLTTransformer.java"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
java_binary( | ||
name = "XSLTTransformer", | ||
main_class = "XSLTTransformer", | ||
visibility = ["//visibility:public"], | ||
runtime_deps = [":xml"], | ||
) |
35 changes: 35 additions & 0 deletions
35
java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.io.File; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
public class XSLTTransformer { | ||
public static void main(String[] args) throws Exception { | ||
if (args.length != 2) { | ||
System.err.println("Usage: java XSLTTransformer <input.xml> <transform.xslt>"); | ||
System.exit(1); | ||
} | ||
|
||
String inputXML = args[0]; | ||
String xsltFile = args[1]; | ||
|
||
// Create transformer factory | ||
TransformerFactory factory = TransformerFactory.newInstance(); | ||
|
||
// Load the XSLT file | ||
StreamSource xslt = new StreamSource(new File(xsltFile)); | ||
|
||
// Create a transformer | ||
Transformer transformer = factory.newTransformer(xslt); | ||
|
||
// Load the input XML file | ||
StreamSource xmlInput = new StreamSource(new File(inputXML)); | ||
|
||
// Set the output file | ||
StreamResult xmlOutput = new StreamResult(System.out); | ||
|
||
// Perform the transformation | ||
transformer.transform(xmlInput, xmlOutput); | ||
} | ||
} |