8350102: Decouple jpackage test-lib Executor.Result and Executor classes

Reviewed-by: almatvee
This commit is contained in:
Alexey Semenyuk 2025-02-19 17:08:01 +00:00
parent 70a6c0b7ac
commit 3487f8cbd5
3 changed files with 41 additions and 33 deletions

View File

@ -36,6 +36,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Pattern;
@ -76,6 +77,10 @@ public final class Executor extends CommandArguments<Executor> {
return setToolProvider(v.asToolProvider());
}
public Optional<Path> getExecutable() {
return Optional.ofNullable(executable);
}
public Executor setDirectory(Path v) {
directory = v;
return this;
@ -173,10 +178,10 @@ public final class Executor extends CommandArguments<Executor> {
return this;
}
public class Result {
public record Result(int exitCode, List<String> output, Supplier<String> cmdline) {
Result(int exitCode) {
this.exitCode = exitCode;
public Result {
Objects.requireNonNull(cmdline);
}
public String getFirstLineOfOutput() {
@ -187,14 +192,10 @@ public final class Executor extends CommandArguments<Executor> {
return output;
}
public String getPrintableCommandLine() {
return Executor.this.getPrintableCommandLine();
}
public Result assertExitCodeIs(int expectedExitCode) {
TKit.assertEquals(expectedExitCode, exitCode, String.format(
"Check command %s exited with %d code",
getPrintableCommandLine(), expectedExitCode));
cmdline.get(), expectedExitCode));
return this;
}
@ -205,9 +206,6 @@ public final class Executor extends CommandArguments<Executor> {
public int getExitCode() {
return exitCode;
}
final int exitCode;
private List<String> output;
}
public Result executeWithoutExitCodeCheck() {
@ -408,28 +406,34 @@ public final class Executor extends CommandArguments<Executor> {
}
}
Result reply = new Result(process.waitFor());
trace("Done. Exit code: " + reply.exitCode);
final int exitCode = process.waitFor();
trace("Done. Exit code: " + exitCode);
final List<String> output;
if (outputLines != null) {
reply.output = Collections.unmodifiableList(outputLines);
output = Collections.unmodifiableList(outputLines);
} else {
output = null;
}
return reply;
return createResult(exitCode, output);
}
private Result runToolProvider(PrintStream out, PrintStream err) {
private int 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;
final int exitCode = toolProvider.run(out, err, args.toArray(
String[]::new));
trace("Done. Exit code: " + exitCode);
return exitCode;
}
private Result createResult(int exitCode, List<String> output) {
return new Result(exitCode, output, this::getPrintableCommandLine);
}
private Result runToolProvider() throws IOException {
if (!withSavedOutput()) {
if (saveOutputType.contains(SaveOutputType.DUMP)) {
return runToolProvider(System.out, System.err);
return createResult(runToolProvider(System.out, System.err), null);
}
PrintStream nullPrintStream = new PrintStream(new OutputStream() {
@ -438,36 +442,40 @@ public final class Executor extends CommandArguments<Executor> {
// Nop
}
});
return runToolProvider(nullPrintStream, nullPrintStream);
return createResult(runToolProvider(nullPrintStream, nullPrintStream), null);
}
try (ByteArrayOutputStream buf = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(buf)) {
Result reply = runToolProvider(ps, ps);
final var exitCode = runToolProvider(ps, ps);
ps.flush();
final List<String> output;
try (BufferedReader bufReader = new BufferedReader(new StringReader(
buf.toString()))) {
if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) {
String firstLine = bufReader.lines().findFirst().orElse(null);
if (firstLine != null) {
reply.output = List.of(firstLine);
output = List.of(firstLine);
} else {
output = null;
}
} else if (saveOutputType.contains(SaveOutputType.FULL)) {
reply.output = bufReader.lines().collect(
Collectors.toUnmodifiableList());
output = bufReader.lines().collect(Collectors.toUnmodifiableList());
} else {
output = null;
}
if (saveOutputType.contains(SaveOutputType.DUMP)) {
Stream<String> lines;
if (saveOutputType.contains(SaveOutputType.FULL)) {
lines = reply.output.stream();
lines = output.stream();
} else {
lines = bufReader.lines();
}
lines.forEach(System.out::println);
}
}
return reply;
return createResult(exitCode, output);
}
}

View File

@ -56,7 +56,7 @@ import jdk.jpackage.internal.util.function.ThrowingSupplier;
* anything. The simplest is to compile test application and pack in a jar for
* use on jpackage command line.
*/
public final class JPackageCommand extends CommandArguments<JPackageCommand> {
public class JPackageCommand extends CommandArguments<JPackageCommand> {
public JPackageCommand() {
prerequisiteActions = new Actions();
@ -799,7 +799,7 @@ public final class JPackageCommand extends CommandArguments<JPackageCommand> {
outputValidator.accept(result.getOutput().stream());
}
if (result.exitCode == 0) {
if (result.exitCode() == 0) {
executeVerifyActions();
}

View File

@ -72,7 +72,7 @@ public class WindowsHelper {
for (int attempt = 0; attempt < 8; ++attempt) {
result = misexec.executeWithoutExitCodeCheck();
if (result.exitCode == 1605) {
if (result.exitCode() == 1605) {
// ERROR_UNKNOWN_PRODUCT, attempt to uninstall not installed
// package
return;
@ -81,7 +81,7 @@ public class WindowsHelper {
// The given Executor may either be of an msiexec command or an
// unpack.bat script containing the msiexec command. In the later
// case, when misexec returns 1618, the unpack.bat may return 1603
if ((result.exitCode == 1618) || (result.exitCode == 1603)) {
if ((result.exitCode() == 1618) || (result.exitCode() == 1603)) {
// Another installation is already in progress.
// Wait a little and try again.
Long timeout = 1000L * (attempt + 3); // from 3 to 10 seconds
@ -523,7 +523,7 @@ public class WindowsHelper {
var status = Executor.of("reg", "query", keyPath, "/v", valueName)
.saveOutput()
.executeWithoutExitCodeCheck();
if (status.exitCode == 1) {
if (status.exitCode() == 1) {
// Should be the case of no such registry value or key
String lookupString = "ERROR: The system was unable to find the specified registry key or value.";
TKit.assertTextStream(lookupString)