Skip to content

Commit 470cd83

Browse files
authored
Consistent argsfile parsing (#1385)
* Worker --persistent_worker argsfile functionality + test
1 parent de3d3a7 commit 470cd83

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

src/java/io/bazel/rulesscala/worker/Worker.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public void checkPermission(Permission permission) {
8181
int code = 0;
8282

8383
try {
84-
workerInterface.work(stringListToArray(request.getArgumentsList()));
84+
String[] workerArgs = stringListToArray(request.getArgumentsList());
85+
String[] args = expandArgsIfArgsfile(workerArgs);
86+
workerInterface.work(args);
8587
} catch (ExitTrapped e) {
8688
code = e.code;
8789
} catch (Exception e) {
@@ -115,15 +117,21 @@ public void checkPermission(Permission permission) {
115117
/** The single pass runner for ephemeral (non-persistent) worker processes */
116118
private static void ephemeralWorkerMain(String workerArgs[], Interface workerInterface)
117119
throws Exception {
118-
String[] args;
119-
if (workerArgs.length == 1 && workerArgs[0].startsWith("@")) {
120-
args =
121-
stringListToArray(
122-
Files.readAllLines(Paths.get(workerArgs[0].substring(1)), StandardCharsets.UTF_8));
120+
String[] args = expandArgsIfArgsfile(workerArgs);
121+
workerInterface.work(args);
122+
}
123+
124+
private static String[] expandArgsIfArgsfile(String[] allArgs) throws IOException {
125+
if (allArgs.length == 1 && allArgs[0].startsWith("@")) {
126+
return stringListToArray(
127+
Files.readAllLines(
128+
Paths.get(allArgs[0].substring(1)),
129+
StandardCharsets.UTF_8)
130+
);
131+
123132
} else {
124-
args = workerArgs;
133+
return allArgs;
125134
}
126-
workerInterface.work(args);
127135
}
128136

129137
/**

src/java/io/bazel/rulesscala/worker/WorkerTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
import java.io.PipedInputStream;
88
import java.io.PipedOutputStream;
99
import java.io.PrintStream;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
1013
import java.util.concurrent.atomic.AtomicInteger;
1114
import org.junit.AfterClass;
15+
import org.junit.Assert;
1216
import org.junit.Test;
1317
import org.junit.runner.RunWith;
1418
import org.junit.runners.JUnit4;
1519

20+
import static org.junit.Assert.assertEquals;
21+
1622
@RunWith(JUnit4.class)
1723
public class WorkerTest {
1824

@@ -89,6 +95,42 @@ public void work(String[] args) throws Exception {
8995
}
9096
}
9197

98+
@Test
99+
public void testPersistentWorkerArgsfile() throws Exception {
100+
Path tmpFile = Files.createTempFile("testPersistentWorkerArgsfiles-args", ".txt");
101+
102+
try (PersistentWorkerHelper helper = new PersistentWorkerHelper()) {
103+
Worker.Interface worker =
104+
new Worker.Interface() {
105+
@Override
106+
public void work(String[] args) {
107+
for (String arg : args) {
108+
System.out.println(arg);
109+
}
110+
}
111+
};
112+
113+
String contents = "line 1\n--flag_1\nsome arg\n";
114+
115+
Files.write(tmpFile, contents.getBytes(StandardCharsets.UTF_8));
116+
117+
WorkerProtocol.WorkRequest.newBuilder()
118+
.addArguments("@" + tmpFile)
119+
.build()
120+
.writeDelimitedTo(helper.requestOut);
121+
122+
helper.runWorker(worker);
123+
124+
WorkerProtocol.WorkResponse response =
125+
WorkerProtocol.WorkResponse.parseDelimitedFrom(helper.responseIn);
126+
127+
assertEquals(response.getExitCode(), 0);
128+
assertEquals(response.getOutput(), contents);
129+
} finally {
130+
Files.deleteIfExists(tmpFile);
131+
}
132+
}
133+
92134
/** A helper to manage IO when testing a persistent worker. */
93135
private final class PersistentWorkerHelper implements AutoCloseable {
94136

0 commit comments

Comments
 (0)