Skip to content

Commit 41ab11c

Browse files
authored
Merge pull request #430 from dmlloyd/oops
Fix accidental loss of setting the CWD of the subprocess
2 parents 08ffd87 + d9af001 commit 41ab11c

5 files changed

Lines changed: 40 additions & 0 deletions

File tree

process/src/main/java/io/smallrye/common/process/PipelineRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ int createThreads(final ThreadFactory tf, final ProcessRunner<O> runner, final P
598598
throws IOException {
599599
pb = processBuilder.pb;
600600
pb.command(Stream.concat(Stream.of(processBuilder.command.toString()), processBuilder.arguments.stream()).toList());
601+
pb.directory(processBuilder.directory);
601602
int cnt = createInputThread(tf, runner)
602603
+ createErrorThreads(tf, runner)
603604
+ createOutputThreads(tf, runner, nextRunner)

process/src/test/java/io/smallrye/common/process/TestBasicExecution.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.net.URI;
88
import java.net.URISyntaxException;
99
import java.net.URL;
10+
import java.nio.file.Path;
1011
import java.util.ArrayDeque;
1112
import java.util.List;
1213
import java.util.concurrent.CompletableFuture;
@@ -16,6 +17,7 @@
1617
import org.junit.jupiter.api.Test;
1718

1819
import io.smallrye.common.process.helpers.Cat;
20+
import io.smallrye.common.process.helpers.Cwd;
1921
import io.smallrye.common.process.helpers.Errorifier;
2022
import io.smallrye.common.process.helpers.ErrorifierWithOutput;
2123

@@ -216,6 +218,31 @@ public void testSplitPipeline() throws Exception {
216218
assertTrue(q.isEmpty());
217219
}
218220

221+
@Test
222+
public void testWorkingDirectory() throws Exception {
223+
assertEquals("test-a", ProcessBuilder.newBuilder(ProcessUtil.pathOfJava())
224+
.arguments(findHelper(Cwd.class))
225+
.directory(findResource("test-a/empty.txt").getParent())
226+
.output().toSingleString(1000)
227+
.run());
228+
assertEquals("test-b", ProcessBuilder.newBuilder(ProcessUtil.pathOfJava())
229+
.arguments(findHelper(Cwd.class))
230+
.directory(findResource("test-b/empty.txt").getParent())
231+
.output().toSingleString(1000)
232+
.run());
233+
}
234+
235+
static Path findResource(String name) throws URISyntaxException {
236+
URL url = TestBasicExecution.class.getClassLoader().getResource(name);
237+
if (url == null) {
238+
throw new IllegalStateException("Unexpected missing file for " + name);
239+
}
240+
if (!url.getProtocol().equals("file")) {
241+
throw new IllegalStateException("Unexpected wrong protocol for " + url);
242+
}
243+
return Path.of(url.toURI());
244+
}
245+
219246
/**
220247
* Build a command line for the {@code java} command to execute a class which is on our class path.
221248
* This allows us to test process execution on any platform.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.smallrye.common.process.helpers;
2+
3+
import java.nio.file.Path;
4+
5+
/**
6+
* Get and return the current working directory file name.
7+
*/
8+
public final class Cwd {
9+
public static void main(String[] args) {
10+
System.out.print(Path.of("").toAbsolutePath().getFileName());
11+
}
12+
}

process/src/test/resources/test-a/empty.txt

Whitespace-only changes.

process/src/test/resources/test-b/empty.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)