8227529: With malformed --app-image the error messages are awful

Reviewed-by: almatvee
This commit is contained in:
Alexey Semenyuk 2023-12-01 01:53:20 +00:00
parent 76fea80707
commit 56d4c33f40
3 changed files with 71 additions and 7 deletions

View File

@ -423,6 +423,14 @@ public final class JPackageCommand extends CommandArguments<JPackageCommand> {
return outputDir().resolve(bundleName);
}
Optional<Path> nullableOutputBundle() {
try {
return Optional.ofNullable(outputBundle());
} catch (Exception ex) {
return Optional.empty();
}
}
/**
* Returns application layout.
*
@ -749,11 +757,15 @@ public final class JPackageCommand extends CommandArguments<JPackageCommand> {
if (hasArgument("--dest")) {
if (isImagePackageType()) {
TKit.deleteDirectoryContentsRecursive(outputDir());
} else if (ThrowingSupplier.toSupplier(() -> TKit.deleteIfExists(
outputBundle())).get()) {
TKit.trace(
String.format("Deleted [%s] file before running jpackage",
outputBundle()));
} else {
nullableOutputBundle().ifPresent(path -> {
if (ThrowingSupplier.toSupplier(() -> TKit.deleteIfExists(
path)).get()) {
TKit.trace(String.format(
"Deleted [%s] file before running jpackage",
path));
}
});
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -582,7 +582,9 @@ public final class PackageTest extends RunnablePackageTest {
if (expectedJPackageExitCode == 0) {
TKit.assertFileExists(cmd.outputBundle());
} else {
TKit.assertPathExists(cmd.outputBundle(), false);
cmd.nullableOutputBundle().ifPresent(outputBundle -> {
TKit.assertPathExists(outputBundle, false);
});
}
verifyPackageBundle(cmd, result);
break;

View File

@ -25,6 +25,7 @@ import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
import java.util.List;
import jdk.jpackage.internal.AppImageFile;
import jdk.jpackage.test.Annotations.Parameter;
import jdk.jpackage.test.TKit;
import jdk.jpackage.test.JPackageCommand;
@ -102,6 +103,55 @@ public class AppImagePackageTest {
// default: {CREATE, UNPACK, VERIFY}, but we can't verify foreign image
}
@Test
public static void testBadAppImage() throws IOException {
Path appImageDir = TKit.createTempDirectory("appimage");
Files.createFile(appImageDir.resolve("foo"));
configureAppImageWithoutJPackageXMLFile(appImageDir).addInitializer(
cmd -> {
cmd.removeArgumentWithValue("--name");
}).run(Action.CREATE);
}
@Test
public static void testBadAppImage2() throws IOException {
Path appImageDir = TKit.createTempDirectory("appimage");
Files.createFile(appImageDir.resolve("foo"));
configureAppImageWithoutJPackageXMLFile(appImageDir).run(Action.CREATE);
}
@Test
public static void testBadAppImage3() throws IOException {
Path appImageDir = TKit.createTempDirectory("appimage");
JPackageCommand appImageCmd = JPackageCommand.helloAppImage().
setFakeRuntime().setArgumentValue("--dest", appImageDir);
configureAppImageWithoutJPackageXMLFile(appImageCmd.outputBundle()).
addRunOnceInitializer(() -> {
appImageCmd.execute();
Files.delete(AppImageFile.getPathInAppImage(appImageCmd.
outputBundle()));
}).run(Action.CREATE);
}
private static PackageTest configureAppImageWithoutJPackageXMLFile(
Path appImageDir) {
return new PackageTest()
.addInitializer(cmd -> {
cmd.saveConsoleOutput(true);
cmd.addArguments("--app-image", appImageDir);
cmd.removeArgumentWithValue("--input");
cmd.ignoreDefaultVerbose(true); // no "--verbose" option
})
.addBundleVerifier((cmd, result) -> {
TKit.assertTextStream(
"Error: Missing .jpackage.xml file in app-image dir").apply(
result.getOutput().stream());
})
.setExpectedExitCode(1);
}
private static Path iconPath(String name) {
return TKit.TEST_SRC_ROOT.resolve(Path.of("resources", name
+ TKit.ICON_SUFFIX));