diff --git a/MODULE.bazel b/MODULE.bazel index 463e7aa82..c1421ceca 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -35,7 +35,7 @@ maven.install( # kubernetes:client-java also depends on Protobuf (+gRPC?), version may need to be upgraded together "io.kubernetes:client-java:18.0.0", # Other - "ch.vorburger.exec:exec:3.1.4", + "ch.vorburger.exec:exec:3.1.5", "com.github.java-json-tools:uri-template:0.10", "com.zaxxer:nuprocess:2.0.6", "info.picocli:picocli:4.7.1", diff --git a/common/markdown/src/main/java/dev/enola/common/markdown/exec/ExecMD.java b/common/markdown/src/main/java/dev/enola/common/markdown/exec/ExecMD.java index ef53f0a44..468175373 100644 --- a/common/markdown/src/main/java/dev/enola/common/markdown/exec/ExecMD.java +++ b/common/markdown/src/main/java/dev/enola/common/markdown/exec/ExecMD.java @@ -144,20 +144,10 @@ void exec(Path dir, String preamble, String command, Appendable script, Appendab Duration timeout = Duration.ofSeconds(7); try { - var exitCode = runner.bash(dir, fullCommand, md, timeout); - if (exitCode != 0 && !expectFailure) { - throw new MarkdownProcessingException( - exitCode - + " exit code (use ```bash $? marker if that's expected): " - + fullCommand); - } - if (exitCode == 0 && expectFailure) { - throw new MarkdownProcessingException( - "exit code 0, but was expected to fail: " + fullCommand); - } - + var exitCode = runner.bash(expectFailure, dir, fullCommand, md, timeout); } catch (Exception e) { - throw new MarkdownProcessingException("exec failed: " + fullCommand, e); + throw new MarkdownProcessingException( + "exec failed (use ```bash $? marker if that's expected): " + fullCommand, e); } } diff --git a/common/markdown/src/main/java/dev/enola/common/markdown/exec/NuProcessRunner.java b/common/markdown/src/main/java/dev/enola/common/markdown/exec/NuProcessRunner.java index 79364611d..b533766f6 100644 --- a/common/markdown/src/main/java/dev/enola/common/markdown/exec/NuProcessRunner.java +++ b/common/markdown/src/main/java/dev/enola/common/markdown/exec/NuProcessRunner.java @@ -32,7 +32,12 @@ class NuProcessRunner implements Runner { @Override - public int exec(Path dir, List command, Appendable output, Duration timeout) + public int exec( + boolean expectNonZeroExitCode, + Path dir, + List command, + Appendable output, + Duration timeout) throws Exception { AppendingHandler handler = new AppendingHandler(output); NuProcessBuilder pb = new NuProcessBuilder(handler, command); diff --git a/common/markdown/src/main/java/dev/enola/common/markdown/exec/Runner.java b/common/markdown/src/main/java/dev/enola/common/markdown/exec/Runner.java index aabd23fd0..a1ef6ce84 100644 --- a/common/markdown/src/main/java/dev/enola/common/markdown/exec/Runner.java +++ b/common/markdown/src/main/java/dev/enola/common/markdown/exec/Runner.java @@ -29,10 +29,26 @@ * href="https://github.com/zeroturnaround/zt-exec">zt-exec. */ interface Runner { - int exec(Path dir, List command, Appendable output, Duration timeout) throws Exception; + int exec( + boolean expectNonZeroExitCode, + Path dir, + List command, + Appendable output, + Duration timeout) + throws Exception; - default int bash(Path dir, String command, Appendable output, Duration timeout) + default int bash( + boolean expectNonZeroExitCode, + Path dir, + String command, + Appendable output, + Duration timeout) throws Exception { - return exec(dir, List.of("/usr/bin/env", "bash", "-c", command), output, timeout); + return exec( + expectNonZeroExitCode, + dir, + List.of("/usr/bin/env", "bash", "-c", command), + output, + timeout); } } diff --git a/common/markdown/src/main/java/dev/enola/common/markdown/exec/VorburgerExecRunner.java b/common/markdown/src/main/java/dev/enola/common/markdown/exec/VorburgerExecRunner.java index bd6401870..0b9a6a366 100644 --- a/common/markdown/src/main/java/dev/enola/common/markdown/exec/VorburgerExecRunner.java +++ b/common/markdown/src/main/java/dev/enola/common/markdown/exec/VorburgerExecRunner.java @@ -21,9 +21,6 @@ import ch.vorburger.exec.ManagedProcess; import ch.vorburger.exec.ManagedProcessBuilder; -import ch.vorburger.exec.ManagedProcessException; - -import org.apache.commons.exec.Executor; import java.io.ByteArrayOutputStream; import java.nio.file.Path; @@ -32,7 +29,12 @@ public class VorburgerExecRunner implements Runner { @Override - public int exec(Path dir, List command, Appendable output, Duration timeout) + public int exec( + boolean expectNonZeroExitCode, + Path dir, + List command, + Appendable output, + Duration timeout) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -46,24 +48,15 @@ public int exec(Path dir, List command, Appendable output, Duration time for (var arg : command.subList(1, command.size())) { pb.addArgument(arg, false); } + if (expectNonZeroExitCode) { + pb.setIsSuccessExitValueChecker(exitValue -> exitValue != 0); + } ManagedProcess p = pb.build(); - int exitCode; try { - exitCode = p.start().waitForExitMaxMs(timeout.toMillis()); - } catch (ManagedProcessException e) { - // TODO FIXME This is very ugly!! Needs - // https://github.com/vorburger/ch.vorburger.exec/issues/45 - if (e.getMessage().contains("failed, exitValue=") - && p.exitValue() != Executor.INVALID_EXITVALUE) { - exitCode = p.exitValue(); - } else { - throw e; - } + return p.start().waitForExitMaxMs(timeout.toMillis()); } finally { output.append(baos.toString(UTF_8)); } - - return exitCode; } } diff --git a/common/markdown/src/test/java/dev/enola/common/markdown/exec/RunnerTest.java b/common/markdown/src/test/java/dev/enola/common/markdown/exec/RunnerTest.java index cdcae5567..ce3eb8e22 100644 --- a/common/markdown/src/test/java/dev/enola/common/markdown/exec/RunnerTest.java +++ b/common/markdown/src/test/java/dev/enola/common/markdown/exec/RunnerTest.java @@ -19,40 +19,40 @@ import static org.junit.Assert.assertEquals; -import org.junit.Test; +import static java.nio.file.Path.of; +import static java.time.Duration.ofSeconds; -import java.nio.file.Path; -import java.time.Duration; +import org.junit.Test; public class RunnerTest { Runner runner = new VorburgerExecRunner(); // NuProcessRunner(); - void check(String command, int expectedExitCode, String expectedOutput) throws Exception { + void check(String command, boolean expectNonZeroExitCode, String expectedOutput) + throws Exception { var sb = new StringBuffer(); - var actualExitCode = runner.bash(Path.of("."), command, sb, Duration.ofSeconds(3)); + var actualExitCode = runner.bash(expectNonZeroExitCode, of("."), command, sb, ofSeconds(3)); assertEquals(expectedOutput, sb.toString()); - assertEquals(expectedExitCode, actualExitCode); + assertEquals(expectNonZeroExitCode, actualExitCode != 0); } @Test public void testTrue() throws Exception { - check("true", 0, ""); + check("true", false, ""); } @Test public void testEcho() throws Exception { - check("echo hi", 0, "hi\n"); + check("echo hi", false, "hi\n"); } @Test public void testFalse() throws Exception { - check("false", 1, ""); + check("false", true, ""); } @Test public void testInexistantCommand() throws Exception { - int exitValue = 127; // or Integer.MIN_VALUE for NuProcess - check("does-not-exist", exitValue, "bash: line 1: does-not-exist: command not found\n"); + check("does-not-exist", true, "bash: line 1: does-not-exist: command not found\n"); } } diff --git a/maven_install.json b/maven_install.json index 5d300f16f..b92deb55d 100644 --- a/maven_install.json +++ b/maven_install.json @@ -1,7 +1,7 @@ { "__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", - "__INPUT_ARTIFACTS_HASH": -709240374, - "__RESOLVED_ARTIFACTS_HASH": -1427845027, + "__INPUT_ARTIFACTS_HASH": -1178826615, + "__RESOLVED_ARTIFACTS_HASH": -324205966, "artifacts": { "aopalliance:aopalliance": { "shasums": { @@ -19,10 +19,10 @@ }, "ch.vorburger.exec:exec": { "shasums": { - "jar": "8a46a96d66e8d69f6cfcb85f09f414350fb562912bb9aba9c862c42f71ba50cf", - "sources": "ed6de568fab512685e71f4708e0f5c34f17de0432144d83e9615096f0af096b7" + "jar": "1aaf7c21efe48a9883d0049c7343cb1522b71bc2ef164aeb4024b608d19920a2", + "sources": "0c5735244e8c928d281bc7f1f01fca45b61a342dfa0c24d2de3ebe548395b7c8" }, - "version": "3.1.4" + "version": "3.1.5" }, "com.github.java-json-tools:btf": { "shasums": {