Skip to content

Commit

Permalink
Bump ch.vorburger.exec to 3.1.5 (fixes #163 - hopefully!)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed May 13, 2023
1 parent 31c0f0d commit 62374dd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 51 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@

class NuProcessRunner implements Runner {
@Override
public int exec(Path dir, List<String> command, Appendable output, Duration timeout)
public int exec(
boolean expectNonZeroExitCode,
Path dir,
List<String> command,
Appendable output,
Duration timeout)
throws Exception {
AppendingHandler handler = new AppendingHandler(output);
NuProcessBuilder pb = new NuProcessBuilder(handler, command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@
* href="https://github.com/zeroturnaround/zt-exec">zt-exec</a>.
*/
interface Runner {
int exec(Path dir, List<String> command, Appendable output, Duration timeout) throws Exception;
int exec(
boolean expectNonZeroExitCode,
Path dir,
List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +29,12 @@

public class VorburgerExecRunner implements Runner {
@Override
public int exec(Path dir, List<String> command, Appendable output, Duration timeout)
public int exec(
boolean expectNonZeroExitCode,
Path dir,
List<String> command,
Appendable output,
Duration timeout)
throws Exception {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand All @@ -46,24 +48,15 @@ public int exec(Path dir, List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
10 changes: 5 additions & 5 deletions maven_install.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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": {
Expand Down

0 comments on commit 62374dd

Please sign in to comment.