Added public methods to directly access configuration options

This commit is contained in:
Erik C. Thauvin 2024-06-21 00:13:25 -07:00
parent 35287d7d6d
commit 6e8d3c2cd6
Signed by: erik
GPG key ID: 776702A6A2DA330E
2 changed files with 63 additions and 18 deletions

View file

@ -147,6 +147,15 @@ public class ExecOperation extends AbstractOperation<ExecOperation> {
return this; 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. * Configure the command timeout.
* *
@ -158,6 +167,15 @@ public class ExecOperation extends AbstractOperation<ExecOperation> {
return this; return this;
} }
/**
* Returns the command timeout.
*
* @return the timeout
*/
public int timeout() {
return timeout_;
}
/** /**
* Configures the working directory. * Configures the working directory.
* *
@ -178,4 +196,13 @@ public class ExecOperation extends AbstractOperation<ExecOperation> {
public ExecOperation workDir(String dir) { public ExecOperation workDir(String dir) {
return workDir(new File(dir)); return workDir(new File(dir));
} }
/**
* Returns the working directory.
*
* @return the directory
*/
public File workDir() {
return workDir_;
}
} }

View file

@ -19,6 +19,7 @@ package rife.bld.extension;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.WebProject;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
@ -57,32 +58,49 @@ class ExecOperationTest {
@Test @Test
void testFailOnExit() { void testFailOnExit() {
assertThatCode(() -> var op = new ExecOperation()
new ExecOperation() .fromProject(new BaseProject())
.fromProject(new BaseProject()) .command(List.of("cat", FOO))
.command(List.of("cat", FOO)) .failOnExit(false);
.failOnExit(false) assertThat(op.isFailOnExit()).isFalse();
.execute()).doesNotThrowAnyException(); assertThatCode(op::execute).doesNotThrowAnyException();
op.failOnExit(true);
assertThat(op.isFailOnExit()).isTrue();
} }
@Test @Test
void testTimeout() { void testTimeout() {
assertThatCode(() -> var op = new ExecOperation()
new ExecOperation() .fromProject(new BaseProject())
.fromProject(new BaseProject()) .timeout(5)
.timeout(5) .command(List.of("sleep", "10"));
.command(List.of("sleep", "10")) assertThat(op.timeout()).isEqualTo(5);
.execute()).message().contains("timed out"); 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 @Test
void testWorkDir() { void testWorkDir() {
assertThatCode(() -> var workDir = new File(System.getProperty("java.io.tmpdir"));
new ExecOperation() var op = new ExecOperation()
.fromProject(new BaseProject()) .fromProject(new BaseProject())
.command("echo", FOO) .command("echo", FOO)
.workDir(new File(System.getProperty("java.io.tmpdir"))) .workDir(workDir);
.execute()).doesNotThrowAnyException(); assertThat(op.workDir()).isEqualTo(workDir);
assertThatCode(op::execute).doesNotThrowAnyException();
} }
@Test @Test