< prev index next >
test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java
Print this page
@@ -37,10 +37,15 @@
import java.util.stream.Stream;
import jdk.jpackage.test.Functional.ThrowingSupplier;
public final class Executor extends CommandArguments<Executor> {
+ public static Executor of(String... cmdline) {
+ return new Executor().setExecutable(cmdline[0]).addArguments(
+ Arrays.copyOfRange(cmdline, 1, cmdline.length));
+ }
+
public Executor() {
saveOutputType = new HashSet<>(Set.of(SaveOutputType.NONE));
}
public Executor setExecutable(String v) {
@@ -168,11 +173,11 @@
final int exitCode;
private List<String> output;
}
- public Result execute() {
+ public Result executeWithoutExitCodeCheck() {
if (toolProvider != null && directory != null) {
throw new IllegalArgumentException(
"Can't change directory when using tool provider");
}
@@ -187,25 +192,35 @@
throw new IllegalStateException("No command to execute");
}).get();
}
+ public Result execute(int expectedCode) {
+ return executeWithoutExitCodeCheck().assertExitCodeIs(expectedCode);
+ }
+
+ public Result execute() {
+ return execute(0);
+ }
+
public String executeAndGetFirstLineOfOutput() {
- return saveFirstLineOfOutput().execute().assertExitCodeIsZero().getFirstLineOfOutput();
+ return saveFirstLineOfOutput().execute().getFirstLineOfOutput();
}
public List<String> executeAndGetOutput() {
- return saveOutput().execute().assertExitCodeIsZero().getOutput();
+ return saveOutput().execute().getOutput();
}
private boolean withSavedOutput() {
return saveOutputType.contains(SaveOutputType.FULL) || saveOutputType.contains(
SaveOutputType.FIRST_LINE);
}
private Path executablePath() {
- if (directory == null || executable.isAbsolute()) {
+ if (directory == null
+ || executable.isAbsolute()
+ || !Set.of(".", "..").contains(executable.getName(0).toString())) {
return executable;
}
// If relative path to executable is used it seems to be broken when
// ProcessBuilder changes the directory. On Windows it changes the
@@ -235,11 +250,11 @@
if (directory != null) {
builder.directory(directory.toFile());
sb.append(String.format("; in directory [%s]", directory));
}
- TKit.trace("Execute " + sb.toString() + "...");
+ trace("Execute " + sb.toString() + "...");
Process process = builder.start();
List<String> outputLines = null;
if (withSavedOutput()) {
try (BufferedReader outReader = new BufferedReader(
@@ -264,23 +279,23 @@
}
}
}
Result reply = new Result(process.waitFor());
- TKit.trace("Done. Exit code: " + reply.exitCode);
+ trace("Done. Exit code: " + reply.exitCode);
if (outputLines != null) {
reply.output = Collections.unmodifiableList(outputLines);
}
return reply;
}
private Result runToolProvider(PrintStream out, PrintStream err) {
- TKit.trace("Execute " + getPrintableCommandLine() + "...");
+ trace("Execute " + getPrintableCommandLine() + "...");
Result reply = new Result(toolProvider.run(out, err, args.toArray(
String[]::new)));
- TKit.trace("Done. Exit code: " + reply.exitCode);
+ trace("Done. Exit code: " + reply.exitCode);
return reply;
}
private Result runToolProvider() throws IOException {
@@ -351,10 +366,14 @@
return Stream.concat(Stream.of(executable), args.stream()).map(
v -> (v.isEmpty() || regex.matcher(v).find()) ? "\"" + v + "\"" : v).collect(
Collectors.joining(" "));
}
+ private static void trace(String msg) {
+ TKit.trace(String.format("exec: %s", msg));
+ }
+
private ToolProvider toolProvider;
private Path executable;
private Set<SaveOutputType> saveOutputType;
private Path directory;
< prev index next >