From 6e8d3c2cd6283e98448c9e0290399be0baf9428e Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 21 Jun 2024 00:13:25 -0700 Subject: [PATCH] Added public methods to directly access configuration options --- .../rife/bld/extension/ExecOperation.java | 27 ++++++++++ .../rife/bld/extension/ExecOperationTest.java | 54 ++++++++++++------- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/main/java/rife/bld/extension/ExecOperation.java b/src/main/java/rife/bld/extension/ExecOperation.java index 3989ead..aa035c5 100644 --- a/src/main/java/rife/bld/extension/ExecOperation.java +++ b/src/main/java/rife/bld/extension/ExecOperation.java @@ -147,6 +147,15 @@ public ExecOperation fromProject(BaseProject project) { return this; } + /** + * Returns whether the operation should fail if the command exit value/status is not 0. + * + * @return {@code true} or {@code false} + */ + public boolean isFailOnExit() { + return failOnExit_; + } + /** * Configure the command timeout. * @@ -158,6 +167,15 @@ public ExecOperation timeout(int timeout) { return this; } + /** + * Returns the command timeout. + * + * @return the timeout + */ + public int timeout() { + return timeout_; + } + /** * Configures the working directory. * @@ -178,4 +196,13 @@ public ExecOperation workDir(File dir) { public ExecOperation workDir(String dir) { return workDir(new File(dir)); } + + /** + * Returns the working directory. + * + * @return the directory + */ + public File workDir() { + return workDir_; + } } diff --git a/src/test/java/rife/bld/extension/ExecOperationTest.java b/src/test/java/rife/bld/extension/ExecOperationTest.java index 5d819b3..ee1c06e 100644 --- a/src/test/java/rife/bld/extension/ExecOperationTest.java +++ b/src/test/java/rife/bld/extension/ExecOperationTest.java @@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test; import rife.bld.BaseProject; import rife.bld.Project; +import rife.bld.WebProject; import java.io.File; import java.util.List; @@ -57,32 +58,49 @@ void testExitValue() { @Test void testFailOnExit() { - assertThatCode(() -> - new ExecOperation() - .fromProject(new BaseProject()) - .command(List.of("cat", FOO)) - .failOnExit(false) - .execute()).doesNotThrowAnyException(); + var op = new ExecOperation() + .fromProject(new BaseProject()) + .command(List.of("cat", FOO)) + .failOnExit(false); + assertThat(op.isFailOnExit()).isFalse(); + assertThatCode(op::execute).doesNotThrowAnyException(); + + op.failOnExit(true); + assertThat(op.isFailOnExit()).isTrue(); } @Test void testTimeout() { - assertThatCode(() -> - new ExecOperation() - .fromProject(new BaseProject()) - .timeout(5) - .command(List.of("sleep", "10")) - .execute()).message().contains("timed out"); + var op = new ExecOperation() + .fromProject(new BaseProject()) + .timeout(5) + .command(List.of("sleep", "10")); + assertThat(op.timeout()).isEqualTo(5); + assertThatCode(op::execute).message().contains("timed out"); + } + + @Test + void testTouch() throws Exception { + var tmpFile = new File("hello.tmp"); + tmpFile.deleteOnExit(); + new ExecOperation() + .fromProject(new Project()) + .timeout(10) + .command("touch", tmpFile.getName()) + .execute(); + + assertThat(tmpFile).exists(); } @Test void testWorkDir() { - assertThatCode(() -> - new ExecOperation() - .fromProject(new BaseProject()) - .command("echo", FOO) - .workDir(new File(System.getProperty("java.io.tmpdir"))) - .execute()).doesNotThrowAnyException(); + var workDir = new File(System.getProperty("java.io.tmpdir")); + var op = new ExecOperation() + .fromProject(new BaseProject()) + .command("echo", FOO) + .workDir(workDir); + assertThat(op.workDir()).isEqualTo(workDir); + assertThatCode(op::execute).doesNotThrowAnyException(); } @Test