Skip to content

Commit

Permalink
A File can now also be used to specify the working dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Apr 5, 2024
1 parent e7d3060 commit 2a2c819
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
41 changes: 26 additions & 15 deletions src/main/java/rife/bld/extension/ExecOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -39,8 +40,8 @@ public class ExecOperation extends AbstractOperation<ExecOperation> {
private final List<String> args_ = new ArrayList<>();
private boolean failOnExit_ = true;
private BaseProject project_;
private int timeout = 30;
private String workDir_;
private int timeout_ = 30;
private File workDir_;

/**
* Configures the command and arguments to be executed.
Expand Down Expand Up @@ -81,11 +82,11 @@ public void execute() throws Exception {
LOGGER.severe("A project must be specified.");
}

final File workDir;
if (workDir_ == null || workDir_.isBlank()) {
workDir = new File(project_.workDirectory().getAbsolutePath());
} else {
workDir = new File(workDir_);
final File workDir = Objects.requireNonNullElseGet(workDir_,
() -> new File(project_.workDirectory().getAbsolutePath()));

if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Working directory: " + workDir.getAbsolutePath());
}

if (workDir.isDirectory()) {
Expand All @@ -99,21 +100,21 @@ public void execute() throws Exception {
}

var proc = pb.start();
var err = proc.waitFor(timeout, TimeUnit.SECONDS);
var err = proc.waitFor(timeout_, TimeUnit.SECONDS);

if (!err) {
proc.destroy();
throw new IOException("The command timed out.");
} else if (proc.exitValue() != 0 && failOnExit_) {
throw new IOException("The command exit status is: " + proc.exitValue());
throw new IOException("The command exit value/status is: " + proc.exitValue());
}
} else {
throw new IOException("Invalid work directory: " + workDir);
throw new IOException("Invalid working directory: " + workDir);
}
}

/**
* Configures whether the operation should fail if the command exit status is not 0.
* Configures whether the operation should fail if the command exit value/status is not 0.
* <p>
* Default is {@code TRUE}
*
Expand Down Expand Up @@ -143,18 +144,28 @@ public ExecOperation fromProject(BaseProject project) {
* @return this operation instance
*/
public ExecOperation timeout(int timeout) {
this.timeout = timeout;
timeout_ = timeout;
return this;
}

/**
* Configures the work directory.
* Configures the working directory.
*
* @param dir the directory path
* @param dir the directory
* @return this operation instance
*/
public ExecOperation workDir(String dir) {
public ExecOperation workDir(File dir) {
workDir_ = dir;
return this;
}

/**
* Configures the working directory.
*
* @param dir the directory path
* @return this operation instance
*/
public ExecOperation workDir(String dir) {
return workDir(new File(dir));
}
}
16 changes: 13 additions & 3 deletions src/test/java/rife/bld/extension/ExecOperationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ void testException() {
}

@Test
void testExitStatus() {
void testExitValue() {
assertThatCode(() ->
new ExecOperation()
.fromProject(new BaseProject())
.command(List.of("cat", FOO))
.execute()).message().contains("exit status");
.execute()).message().contains("exit value/status");
}

@Test
Expand All @@ -83,11 +83,21 @@ void testTimeout() {

@Test
void testWorkDir() {
assertThatCode(() ->
new ExecOperation()
.fromProject(new BaseProject())
.command("echo", FOO)
.workDir(new File(System.getProperty("java.io.tmpdir")))
.execute()).doesNotThrowAnyException();
}

@Test
void testWorkDirInvalid() {
assertThatCode(() ->
new ExecOperation()
.fromProject(new BaseProject())
.command("echo")
.workDir(FOO)
.execute()).message().startsWith("Invalid work directory: ").endsWith(FOO);
.execute()).message().startsWith("Invalid working directory: ").endsWith(FOO);
}
}

0 comments on commit 2a2c819

Please sign in to comment.