Skip to content

Commit

Permalink
Return error output when maven execution fails in JUnitMavenRunnerExt…
Browse files Browse the repository at this point in the history
…ension (#264)

* Fix Maven version configuration: it was found that since MAVEN_HOME was not set, JUnitMavenRunnerExtension used the first in PATH (being 3.8.8).
* Capture error and output to file to then be added to the exception cause that breaks tests.
* Also fix some http:// references.
  • Loading branch information
abelsromero authored Apr 8, 2024
1 parent 302ba8a commit 574b3c1
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ jobs:
run: mvn -version
- name: Clean all modules
run: mvn -B clean
- name: Test
run: mvn -B test -pl tests
- name: Set MAVEN_HOME and Test (Windows)
if: matrix.os == 'windows-latest'
run: |
set MAVEN_HOME="$(which mvn)"
echo "MAVEN_HOME: $MAVEN_HOME"
mvn -B test -pl tests
- name: Set MAVEN_HOME and Test
if: matrix.os != 'windows-latest'
run: |
export MAVEN_HOME="$(which mvn)"
echo "MAVEN_HOME: $MAVEN_HOME"
mvn -B test -pl tests
- name: Upload test output
uses: actions/upload-artifact@v4
env:
Expand Down
2 changes: 1 addition & 1 deletion asciidoc-to-html-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
-->
<!-- Attributes common to all output formats -->
<attributes>
<endpoint-url>http://example.org</endpoint-url>
<endpoint-url>https://example.org</endpoint-url>
<sourcedir>${project.build.sourceDirectory}</sourcedir>
<project-version>${project.version}</project-version>
</attributes>
Expand Down
2 changes: 1 addition & 1 deletion docbook-pipeline-docbkx-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<baseDir>${project.basedir}</baseDir>
-->
<attributes>
<endpoint-url>http://example.org</endpoint-url>
<endpoint-url>https://example.org</endpoint-url>
<sourcedir>${project.build.sourceDirectory}</sourcedir>
</attributes>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
-->
<!-- Attributes common to all output formats -->
<attributes>
<endpoint-url>http://example.org</endpoint-url>
<endpoint-url>https://example.org</endpoint-url>
<sourcedir>${project.build.sourceDirectory}</sourcedir>
<project-version>${project.version}</project-version>
</attributes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public int run() {
throw new RuntimeException("repository does not exists: " + repository);
}
return new ProcessRunner(path.getAbsoluteFile(), buildCommand())
.run();
.run()
.getStatus();
}

private List<String> buildCommand() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.asciidoctor.maven.examples.tests;

class ProcessResult {

private final int status;
private final String output;

ProcessResult(int status, String output) {
this.status = status;
this.output = output;
}

public int getStatus() {
return status;
}

public String getOutput() {
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class ProcessRunner {
Expand All @@ -14,19 +16,39 @@ public ProcessRunner(File workingDir, List<String> command) {
this.command = command;
}

public int run() {
public ProcessResult run() {
final Path output = createTempFile("mvn-error-", ".err");
final File file = output.toFile();

final ProcessBuilder processBuilder = new ProcessBuilder()
.directory(workingDir)
.command(command)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.redirectOutput(ProcessBuilder.Redirect.INHERIT);
.redirectError(file)
.redirectOutput(file);

try {
return processBuilder
.start()
.waitFor();
int status = processBuilder.start().waitFor();
throwIfError(status, output);
return new ProcessResult(status, Files.readString(file.toPath()).trim());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

private void throwIfError(int value, Path errFile) throws IOException {
if (value != 0) {
String joinedCommand = String.join(" ", command);
final String errorOutput = Files.readString(errFile);
throw new RuntimeException(String.format("Command: [%s];\n%s", joinedCommand, errorOutput));
}
errFile.toFile().deleteOnExit();
}

private Path createTempFile(String prefix, String suffix) {
try {
return Files.createTempFile(prefix, suffix);
} catch (IOException e) {
throw new RuntimeException("Could not create temp file", e);
}
}
}

0 comments on commit 574b3c1

Please sign in to comment.