< prev index next >

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java

Print this page

        

*** 37,46 **** --- 37,51 ---- 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,178 **** final int exitCode; private List<String> output; } ! public Result execute() { if (toolProvider != null && directory != null) { throw new IllegalArgumentException( "Can't change directory when using tool provider"); } --- 173,183 ---- final int exitCode; private List<String> output; } ! public Result executeWithoutExitCodeCheck() { if (toolProvider != null && directory != null) { throw new IllegalArgumentException( "Can't change directory when using tool provider"); }
*** 187,211 **** throw new IllegalStateException("No command to execute"); }).get(); } public String executeAndGetFirstLineOfOutput() { ! return saveFirstLineOfOutput().execute().assertExitCodeIsZero().getFirstLineOfOutput(); } public List<String> executeAndGetOutput() { ! return saveOutput().execute().assertExitCodeIsZero().getOutput(); } private boolean withSavedOutput() { return saveOutputType.contains(SaveOutputType.FULL) || saveOutputType.contains( SaveOutputType.FIRST_LINE); } private Path executablePath() { ! if (directory == null || executable.isAbsolute()) { return executable; } // If relative path to executable is used it seems to be broken when // ProcessBuilder changes the directory. On Windows it changes the --- 192,226 ---- 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().getFirstLineOfOutput(); } public List<String> executeAndGetOutput() { ! 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() ! || !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,245 **** if (directory != null) { builder.directory(directory.toFile()); sb.append(String.format("; in directory [%s]", directory)); } ! TKit.trace("Execute " + sb.toString() + "..."); Process process = builder.start(); List<String> outputLines = null; if (withSavedOutput()) { try (BufferedReader outReader = new BufferedReader( --- 250,260 ---- if (directory != null) { builder.directory(directory.toFile()); sb.append(String.format("; in directory [%s]", directory)); } ! trace("Execute " + sb.toString() + "..."); Process process = builder.start(); List<String> outputLines = null; if (withSavedOutput()) { try (BufferedReader outReader = new BufferedReader(
*** 264,286 **** } } } Result reply = new Result(process.waitFor()); ! TKit.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() + "..."); Result reply = new Result(toolProvider.run(out, err, args.toArray( String[]::new))); ! TKit.trace("Done. Exit code: " + reply.exitCode); return reply; } private Result runToolProvider() throws IOException { --- 279,301 ---- } } } Result reply = new Result(process.waitFor()); ! trace("Done. Exit code: " + reply.exitCode); if (outputLines != null) { reply.output = Collections.unmodifiableList(outputLines); } return reply; } private Result runToolProvider(PrintStream out, PrintStream err) { ! trace("Execute " + getPrintableCommandLine() + "..."); Result reply = new Result(toolProvider.run(out, err, args.toArray( String[]::new))); ! trace("Done. Exit code: " + reply.exitCode); return reply; } private Result runToolProvider() throws IOException {
*** 351,360 **** --- 366,379 ---- 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 >