You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/02/19 14:10:26 UTC
(commons-exec) branch master updated: Add CommandLine.CommandLine(Path)
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 75ea3003 Add CommandLine.CommandLine(Path)
75ea3003 is described below
commit 75ea3003dfffe810df5252cd64da9838eaa3d4e0
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Wed Feb 19 09:10:20 2025 -0500
Add CommandLine.CommandLine(Path)
---
pom.xml | 6 ++--
src/changes/changes.xml | 3 +-
.../java/org/apache/commons/exec/CommandLine.java | 16 +++++++--
.../org/apache/commons/exec/AbstractExecTest.java | 4 +--
.../apache/commons/exec/DefaultExecutorTest.java | 16 ++++-----
.../apache/commons/exec/LogOutputStreamTest.java | 5 +--
.../org/apache/commons/exec/StandAloneTest.java | 38 ++++++++++++++--------
.../java/org/apache/commons/exec/TestUtil.java | 14 +++++---
.../java/org/apache/commons/exec/TutorialTest.java | 3 +-
.../org/apache/commons/exec/issues/Exec33Test.java | 2 +-
.../org/apache/commons/exec/issues/Exec34Test.java | 2 +-
.../org/apache/commons/exec/issues/Exec36Test.java | 2 +-
.../org/apache/commons/exec/issues/Exec41Test.java | 2 +-
.../org/apache/commons/exec/issues/Exec44Test.java | 2 +-
.../org/apache/commons/exec/issues/Exec62Test.java | 2 +-
15 files changed, 75 insertions(+), 42 deletions(-)
diff --git a/pom.xml b/pom.xml
index 1e3a9b4a..b029d965 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,7 +25,7 @@ limitations under the License.
<modelVersion>4.0.0</modelVersion>
<name>Apache Commons Exec</name>
<artifactId>commons-exec</artifactId>
- <version>1.4.1-SNAPSHOT</version>
+ <version>1.5.0-SNAPSHOT</version>
<inceptionYear>2005</inceptionYear>
<description>Apache Commons Exec is a library to reliably execute external processes from within the JVM.</description>
@@ -56,8 +56,8 @@ limitations under the License.
<maven.compiler.target>1.8</maven.compiler.target>
<commons.rc.version>RC1</commons.rc.version>
<commons.bc.version>1.3</commons.bc.version>
- <commons.release.version>1.4.0</commons.release.version>
- <commons.release.next>1.4.1</commons.release.next>
+ <commons.release.version>1.5.0</commons.release.version>
+ <commons.release.next>1.5.1</commons.release.next>
<commons.release.isDistModule>true</commons.release.isDistModule>
<commons.componentid>exec</commons.componentid>
<commons.module.name>org.apache.commons.exec</commons.module.name>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7ae6f1a9..83bd6da8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,9 +24,10 @@
<title>Apache Commons Exec Release Notes</title>
</properties>
<body>
- <release version="1.4.1" date="YYYY-MM-DD" description="Maintenance and feature Release (Java 8 or above)">
+ <release version="1.5.0" date="YYYY-MM-DD" description="Maintenance and feature Release (Java 8 or above)">
<!-- ADD -->
<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>
<!-- 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/CommandLine.java b/src/main/java/org/apache/commons/exec/CommandLine.java
index ab1ab264..967dbcdc 100644
--- a/src/main/java/org/apache/commons/exec/CommandLine.java
+++ b/src/main/java/org/apache/commons/exec/CommandLine.java
@@ -18,6 +18,7 @@
package org.apache.commons.exec;
import java.io.File;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -205,7 +206,7 @@ public class CommandLine {
}
/**
- * Create a command line without any arguments.
+ * Constructs a command line without any arguments.
*
* @param executable the executable file.
*/
@@ -215,7 +216,18 @@ public class CommandLine {
}
/**
- * Create a command line without any arguments.
+ * Constructs a command line without any arguments.
+ *
+ * @param executable the executable file.
+ * @since 1.5.0
+ */
+ public CommandLine(final Path executable) {
+ this.isFile = true;
+ this.executable = toCleanExecutable(executable.toAbsolutePath().toString());
+ }
+
+ /**
+ * Constructs a command line without any arguments.
*
* @param executable the executable.
* @throws NullPointerException on null input.
diff --git a/src/test/java/org/apache/commons/exec/AbstractExecTest.java b/src/test/java/org/apache/commons/exec/AbstractExecTest.java
index 95e4d626..d503abbc 100644
--- a/src/test/java/org/apache/commons/exec/AbstractExecTest.java
+++ b/src/test/java/org/apache/commons/exec/AbstractExecTest.java
@@ -30,7 +30,7 @@ public abstract class AbstractExecTest {
* Resolve the OS-specific test file to execute.
*/
protected File resolveTestScript(final String baseName) {
- final File result = TestUtil.resolveScriptForOS(testDir + "/" + baseName);
+ final File result = TestUtil.resolveScriptFileForOS(testDir + "/" + baseName);
if (!result.exists()) {
throw new IllegalArgumentException("Unable to find the following file: " + result.getAbsolutePath());
}
@@ -41,7 +41,7 @@ public abstract class AbstractExecTest {
* Resolve the OS-specific test file to execute.
*/
protected File resolveTestScript(final String directoryName, final String baseName) {
- final File result = TestUtil.resolveScriptForOS(testDir + "/" + directoryName + "/" + baseName);
+ final File result = TestUtil.resolveScriptFileForOS(testDir + "/" + directoryName + "/" + baseName);
if (!result.exists()) {
throw new IllegalArgumentException("Unable to find the following file: " + result.getAbsolutePath());
}
diff --git a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
index 49d8ac4c..9d764e89 100644
--- a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
+++ b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
@@ -75,17 +75,17 @@ public class DefaultExecutorTest {
private final File testDir = new File("src/test/scripts");
private final File foreverOutputFile = new File("./target/forever.txt");
private ByteArrayOutputStream baos;
- private final File testScript = TestUtil.resolveScriptForOS(testDir + "/test");
- private final File errorTestScript = TestUtil.resolveScriptForOS(testDir + "/error");
- private final File foreverTestScript = TestUtil.resolveScriptForOS(testDir + "/forever");
- private final File nonExistingTestScript = TestUtil.resolveScriptForOS(testDir + "/grmpffffff");
- private final File redirectScript = TestUtil.resolveScriptForOS(testDir + "/redirect");
+ private final Path testScript = TestUtil.resolveScriptPathForOS(testDir + "/test");
+ private final Path errorTestScript = TestUtil.resolveScriptPathForOS(testDir + "/error");
+ private final Path foreverTestScript = TestUtil.resolveScriptPathForOS(testDir + "/forever");
+ private final Path nonExistingTestScript = TestUtil.resolveScriptPathForOS(testDir + "/grmpffffff");
+ private final Path redirectScript = TestUtil.resolveScriptPathForOS(testDir + "/redirect");
- private final File printArgsScript = TestUtil.resolveScriptForOS(testDir + "/printargs");
+ private final Path printArgsScript = TestUtil.resolveScriptPathForOS(testDir + "/printargs");
// private final File acroRd32Script = TestUtil.resolveScriptForOS(testDir + "/acrord32");
- private final File stdinSript = TestUtil.resolveScriptForOS(testDir + "/stdin");
+ private final Path stdinSript = TestUtil.resolveScriptPathForOS(testDir + "/stdin");
- private final File environmentSript = TestUtil.resolveScriptForOS(testDir + "/environment");
+ private final Path environmentSript = TestUtil.resolveScriptPathForOS(testDir + "/environment");
// private final File wrapperScript = TestUtil.resolveScriptForOS(testDir + "/wrapper");
/**
diff --git a/src/test/java/org/apache/commons/exec/LogOutputStreamTest.java b/src/test/java/org/apache/commons/exec/LogOutputStreamTest.java
index 5eecb201..bcbea236 100644
--- a/src/test/java/org/apache/commons/exec/LogOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/exec/LogOutputStreamTest.java
@@ -23,6 +23,7 @@ import java.io.File;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
@@ -64,9 +65,9 @@ public class LogOutputStreamTest {
private final File testDir = new File("src/test/scripts");
private OutputStream systemOut;
- private final File environmentScript = TestUtil.resolveScriptForOS(testDir + "/environment");
+ private final Path environmentScript = TestUtil.resolveScriptPathForOS(testDir + "/environment");
- private final File utf8CharacterScript = TestUtil.resolveScriptForOS(testDir + "/utf8Characters");
+ private final Path utf8CharacterScript = TestUtil.resolveScriptPathForOS(testDir + "/utf8Characters");
@AfterEach
public void tearDown() throws Exception {
diff --git a/src/test/java/org/apache/commons/exec/StandAloneTest.java b/src/test/java/org/apache/commons/exec/StandAloneTest.java
index 84cef702..b9fa8e71 100644
--- a/src/test/java/org/apache/commons/exec/StandAloneTest.java
+++ b/src/test/java/org/apache/commons/exec/StandAloneTest.java
@@ -20,6 +20,7 @@ package org.apache.commons.exec;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
+import java.nio.file.Path;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.Test;
@@ -33,21 +34,10 @@ import org.junitpioneer.jupiter.SetSystemProperty;
@SetSystemProperty(key = "org.apache.commons.exec.debug", value = "true")
public class StandAloneTest {
- @Test
- @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
- public void testDefaultExecutor() throws Exception {
- final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/standalone");
- final Executor exec = new DefaultExecutor();
- 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 testDefaultExecutorBuilder() throws Exception {
- final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/standalone");
+ final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
// @formatter:off
final Executor exec = DefaultExecutor.builder()
.setThreadFactory(Executors.defaultThreadFactory())
@@ -64,11 +54,33 @@ public class StandAloneTest {
@Test
@DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
public void testDefaultExecutorDefaultBuilder() throws Exception {
- final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/standalone");
+ final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
final Executor exec = DefaultExecutor.builder().get();
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 testDefaultExecutorFromFile() throws Exception {
+ final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
+ final Executor exec = new DefaultExecutor();
+ 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 testDefaultExecutorFromPath() throws Exception {
+ final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
+ final Executor exec = new DefaultExecutor();
+ exec.setStreamHandler(new PumpStreamHandler());
+ final CommandLine cl = new CommandLine(testScript);
+ exec.execute(cl);
+ assertTrue(new File("./target/mybackup.gz").exists());
+ }
}
diff --git a/src/test/java/org/apache/commons/exec/TestUtil.java b/src/test/java/org/apache/commons/exec/TestUtil.java
index 20d125d1..a4835adb 100644
--- a/src/test/java/org/apache/commons/exec/TestUtil.java
+++ b/src/test/java/org/apache/commons/exec/TestUtil.java
@@ -20,6 +20,8 @@ package org.apache.commons.exec;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
/**
*/
@@ -44,15 +46,19 @@ public final class TestUtil {
return null; // unreachable.
}
- public static File resolveScriptForOS(final String script) {
+ public static File resolveScriptFileForOS(final String script) {
+ return resolveScriptPathForOS(script).toFile();
+ }
+
+ public static Path resolveScriptPathForOS(final String script) {
if (OS.isFamilyWindows()) {
- return new File(script + ".bat");
+ return Paths.get(script + ".bat");
}
if (OS.isFamilyUnix()) {
- return new File(script + ".sh");
+ return Paths.get(script + ".sh");
}
if (OS.isFamilyOpenVms()) {
- return new File(script + ".dcl");
+ return Paths.get(script + ".dcl");
}
fail("Test not supported for this OS");
return null; // unreachable.
diff --git a/src/test/java/org/apache/commons/exec/TutorialTest.java b/src/test/java/org/apache/commons/exec/TutorialTest.java
index 87a49123..2978d659 100644
--- a/src/test/java/org/apache/commons/exec/TutorialTest.java
+++ b/src/test/java/org/apache/commons/exec/TutorialTest.java
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@@ -70,7 +71,7 @@ public class TutorialTest {
private final File testDir = new File("src/test/scripts");
/** Simulates a PDF print job */
- private final File acroRd32Script = TestUtil.resolveScriptForOS(testDir + "/acrord32");
+ private final Path acroRd32Script = TestUtil.resolveScriptPathForOS(testDir + "/acrord32");
/**
* Simulate printing a PDF document.
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec33Test.java b/src/test/java/org/apache/commons/exec/issues/Exec33Test.java
index af4f6da3..cc46541c 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec33Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec33Test.java
@@ -37,7 +37,7 @@ public class Exec33Test {
private final Executor exec = DefaultExecutor.builder().get();
private final File testDir = new File("src/test/scripts");
- private final File testScript = TestUtil.resolveScriptForOS(testDir + "/test");
+ private final File testScript = TestUtil.resolveScriptFileForOS(testDir + "/test");
@Test
public void testExec33() throws Exception {
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec34Test.java b/src/test/java/org/apache/commons/exec/issues/Exec34Test.java
index 2f7e8302..11ddb4bb 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec34Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec34Test.java
@@ -38,7 +38,7 @@ public class Exec34Test {
private final Executor exec = DefaultExecutor.builder().get();
private final File testDir = new File("src/test/scripts");
- private final File pingScript = TestUtil.resolveScriptForOS(testDir + "/ping");
+ private final File pingScript = TestUtil.resolveScriptFileForOS(testDir + "/ping");
/**
*
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec36Test.java b/src/test/java/org/apache/commons/exec/issues/Exec36Test.java
index 8d07d90b..3e343b53 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec36Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec36Test.java
@@ -44,7 +44,7 @@ public class Exec36Test {
private final Executor exec = DefaultExecutor.builder().get();
private final File testDir = new File("src/test/scripts");
- private final File printArgsScript = TestUtil.resolveScriptForOS(testDir + "/printargs");
+ private final File printArgsScript = TestUtil.resolveScriptFileForOS(testDir + "/printargs");
private ByteArrayOutputStream baos;
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec41Test.java b/src/test/java/org/apache/commons/exec/issues/Exec41Test.java
index 1fa4ab78..477ea5aa 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec41Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec41Test.java
@@ -36,7 +36,7 @@ import org.junit.jupiter.api.Test;
public class Exec41Test {
private final File testDir = new File("src/test/scripts");
- private final File pingScript = TestUtil.resolveScriptForOS(testDir + "/ping");
+ private final File pingScript = TestUtil.resolveScriptFileForOS(testDir + "/ping");
/**
* Test EXEC-41 with a disabled PumpStreamHandler to check if we could return immediately after killing the process (no streams implies no blocking stream
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec44Test.java b/src/test/java/org/apache/commons/exec/issues/Exec44Test.java
index d4c19a24..c4d444ac 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec44Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec44Test.java
@@ -37,7 +37,7 @@ public class Exec44Test {
private final Executor exec = DefaultExecutor.builder().get();
private final File testDir = new File("src/test/scripts");
- private final File foreverTestScript = TestUtil.resolveScriptForOS(testDir + "/forever");
+ private final File foreverTestScript = TestUtil.resolveScriptFileForOS(testDir + "/forever");
/**
*
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec62Test.java b/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
index c22ca596..a9e75ff6 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
@@ -45,7 +45,7 @@ public class Exec62Test {
private void execute(final String scriptName) throws Exception {
final ExecuteWatchdog watchdog = new ExecuteWatchdog(4000);
final CommandLine commandLine = new CommandLine("/bin/sh");
- final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/issues/" + scriptName);
+ final File testScript = TestUtil.resolveScriptFileForOS("./src/test/scripts/issues/" + scriptName);
commandLine.addArgument(testScript.getAbsolutePath());