Skip to content

Commit

Permalink
fix: Updated the build to output failed go tests when being run in th…
Browse files Browse the repository at this point in the history
…e maven build.
  • Loading branch information
chrisdutz committed Dec 6, 2023
1 parent bf0066f commit 8fd198c
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 21 deletions.
107 changes: 86 additions & 21 deletions plc4go/pom.xml
Expand Up @@ -64,6 +64,28 @@
<build>
<sourceDirectory>pkg</sourceDirectory>
<plugins>
<!--
Create surefire folder for report, as this is required to exist.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<mkdir dir="target${file.separator}surefire-reports" />
</target>
</configuration>
</execution>
</executions>
</plugin>

<!--
Copy the test-resources in here
-->
Expand Down Expand Up @@ -282,6 +304,10 @@
</execution>

<!-- Processes code that uses old APIs and rewrites them use newer ones -->
<!--
REMARK: The default lifecycle defines this is run in the "validate" phase,
we redefine it to run in the process-sources so it also processes generated code.
-->
<execution>
<id>default-fix</id>
<phase>process-sources</phase>
Expand Down Expand Up @@ -326,25 +352,31 @@
<execution>
<id>default-test</id>
<phase>test</phase>
<!-- TODO: Which goal?!? -->
<goals>
<goal>test</goal>
</goals>
<configuration>
<buildFlags>
<flag>-v</flag>
<!--flag>-v</flag-->
<!-- TODO: race detection temporary disabled till root cause for the issue is found https://github.com/golang/go/issues/40343 -->
<!--flag>-race</flag-->
<flag>${go.cover.flag}</flag>
</buildFlags>
<outLogFile>test-out-verbose.log</outLogFile>
<!--
Don't fail the build if we have test failures as we'll have the results translated
to junit format and will have the failsafe-plugin then fail the build. This way Jenkins
can display the results better.
-->
<ignoreErrorExitCode>true</ignoreErrorExitCode>
<packages>
<package>./...</package>
</packages>
</configuration>
</execution>

<execution>
<!--execution>
<id>readable-test</id>
<!-- TODO: Possibly move to test or prepare-package -->
<phase>test</phase>
<goals>
<goal>custom</goal>
Expand All @@ -359,19 +391,23 @@
<package>./...</package>
</packages>
</configuration>
</execution>
</execution-->

<!-- Convert generated test report into go-junit-report format and save as file -->
<execution>
<id>makeJUnitReport</id>
<!--execution>
<id>make-junit-report</id>
<phase>test</phase>
<goals>
<goal>custom</goal>
</goals>
<configuration>
<skip>${skipTests}</skip>
<exec>go-junit-report</exec>
<!--customCommand>-set-exit-code</customCommand-->
<!- -
This is just stupid, I need to provide something here, or the build won't work ...
this should hopefully have very little side-effects
- ->
<customCommand>-no-xml-header</customCommand>
<buildFlags>
<flag>-in</flag>
<flag>${project.build.directory}${file.separator}reports${file.separator}test-out-verbose.log</flag>
Expand All @@ -386,7 +422,7 @@
<package>target${file.separator}surefire-reports${file.separator}go-junit-report.xml</package>
</packages>
</configuration>
</execution>
</execution-->

<execution>
<id>run-go-vet</id>
Expand All @@ -406,27 +442,56 @@
</configuration>
</plugin>

<!--
Create surefire folder for report
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<id>make-junit-report</id>
<!-- We need to ensure it's after test and before verify -->
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
<goal>exec</goal>
</goals>
<configuration>
<target>
<mkdir dir="target${file.separator}surefire-reports" />
</target>
<executable>${project.build.directory}/go-junit-report</executable>
<arguments>
<argument>-in</argument>
<argument>${project.build.directory}${file.separator}reports${file.separator}test-out-verbose.log</argument>
<argument>-out</argument>
<argument>target${file.separator}surefire-reports${file.separator}go-junit-report.xml</argument>
</arguments>
<successCodes>
<successCode>0</successCode>
</successCodes>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<executions>
<execution>
<id>output-failing-tests</id>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<inherited>false</inherited>
<configuration>
<source>${project.basedir}/script/validate-go-test-results.groovy</source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>4.0.16</version>
</dependency>
</dependencies>
</plugin>

<!-- Ensure we don't deploy anything -->
Expand Down
36 changes: 36 additions & 0 deletions plc4go/script/validate-go-test-results.groovy
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// Only if there's output, do we
def file = new File("target/surefire-reports/go-junit-report.xml")
if (file.exists()) {
// Read the go-junit-report.xml file.
def testResults = new XmlSlurper().parse(file)

// If there are errors, output the entries for these failed tests and fail the build after that.
if(testResults.@failures != "0" && testResults.@failures != "") {
def failedTestSuites = testResults.testsuite.find { node -> node.@failures != '0' || node.@errors != '0' }
for (failedTestSuite in failedTestSuites) {
print failedTestSuite
}
throw new RuntimeException("There were test failures in Go.")
}
}


0 comments on commit 8fd198c

Please sign in to comment.