This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git


The following commit(s) were added to refs/heads/master by this push:
     new d40f0b67 Add DefaultExecutor.Builder.setWorkingDirectory(Path)
d40f0b67 is described below

commit d40f0b67f78dbbd05b33d5f58f2e4421eff6764f
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Feb 19 09:24:25 2025 -0500

    Add DefaultExecutor.Builder.setWorkingDirectory(Path)
---
 src/changes/changes.xml                              |  1 +
 .../org/apache/commons/exec/DefaultExecutor.java     | 13 +++++++++++++
 src/main/java/org/apache/commons/exec/Executor.java  |  1 +
 .../java/org/apache/commons/exec/StandAloneTest.java | 20 +++++++++++++++++++-
 4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bab9a573..e7542378 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -29,6 +29,7 @@
             <action type="add" dev="ggregory" due-to="Gary Gregory">Add Maven 
property project.build.outputTimestamp for build reproducibility.</action>
             <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
CommandLine.CommandLine(Path).</action>
             <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
Executor.getWorkingDirectoryPath().</action>
+            <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
DefaultExecutor.Builder.setWorkingDirectory(Path).</action>
             <!-- FIX -->
             <action type="fix" dev="ggregory" issue="EXEC-122" 
due-to="Marcono1234">Document PumpStreamHandler stream thread-safety 
requirements.</action>
             <action type="fix" dev="ggregory" due-to="Marcono1234">Fix CI only 
running on Ubuntu and improve OS-specific tests #143.</action>
diff --git a/src/main/java/org/apache/commons/exec/DefaultExecutor.java 
b/src/main/java/org/apache/commons/exec/DefaultExecutor.java
index 19a8759a..ac8a3126 100644
--- a/src/main/java/org/apache/commons/exec/DefaultExecutor.java
+++ b/src/main/java/org/apache/commons/exec/DefaultExecutor.java
@@ -19,6 +19,7 @@ package org.apache.commons.exec;
 import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.Map;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadFactory;
@@ -138,6 +139,18 @@ public class DefaultExecutor implements Executor {
             return asThis();
         }
 
+        /**
+         * Sets the working directory.
+         *
+         * @param workingDirectory the working directory., null resets to the 
default.
+         * @return {@code this} instance.
+         * @since 1.5.0
+         */
+        public T setWorkingDirectory(final Path workingDirectory) {
+            this.workingDirectory = workingDirectory != null ? 
workingDirectory.toFile() : null;
+            return asThis();
+        }
+
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/exec/Executor.java 
b/src/main/java/org/apache/commons/exec/Executor.java
index 313524b2..9cce5fe0 100644
--- a/src/main/java/org/apache/commons/exec/Executor.java
+++ b/src/main/java/org/apache/commons/exec/Executor.java
@@ -190,4 +190,5 @@ public interface Executor {
      * @param dir the working directory.
      */
     void setWorkingDirectory(File dir);
+
 }
diff --git a/src/test/java/org/apache/commons/exec/StandAloneTest.java 
b/src/test/java/org/apache/commons/exec/StandAloneTest.java
index b9fa8e71..2eb29aa0 100644
--- a/src/test/java/org/apache/commons/exec/StandAloneTest.java
+++ b/src/test/java/org/apache/commons/exec/StandAloneTest.java
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.concurrent.Executors;
 
 import org.junit.jupiter.api.Test;
@@ -36,7 +37,7 @@ public class StandAloneTest {
 
     @Test
     @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
-    public void testDefaultExecutorBuilder() throws Exception {
+    public void testDefaultExecutorBuilderFromFile() throws Exception {
         final Path testScript = 
TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
         // @formatter:off
         final Executor exec = DefaultExecutor.builder()
@@ -51,6 +52,23 @@ public class StandAloneTest {
         assertTrue(new File("./target/mybackup.gz").exists());
     }
 
+    @Test
+    @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
+    public void testDefaultExecutorBuilderFromPath() throws Exception {
+        final Path testScript = 
TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
+        // @formatter:off
+        final Executor exec = DefaultExecutor.builder()
+                .setThreadFactory(Executors.defaultThreadFactory())
+                .setExecuteStreamHandler(new PumpStreamHandler())
+                .setWorkingDirectory(Paths.get("."))
+                .get();
+        // @formatter:on
+        exec.setStreamHandler(new PumpStreamHandler());
+        final CommandLine cl = new CommandLine(testScript);
+        exec.execute(cl);
+        assertTrue(new File("./target/mybackup.gz").exists());
+    }
+
     @Test
     @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
     public void testDefaultExecutorDefaultBuilder() throws Exception {

Reply via email to