8249289: Exception thrown when --temp points to non-existant directory

Reviewed-by: asemenyuk, almatvee
This commit is contained in:
Andy Herrick 2020-07-17 09:18:51 -04:00
parent e13cb76baa
commit 2f6afe55d8
2 changed files with 43 additions and 24 deletions
src/jdk.incubator.jpackage/share/classes/jdk/incubator/jpackage/internal
test/jdk/tools/jpackage/share/jdk/jpackage/tests

@ -79,18 +79,20 @@ public class DeployParams {
List<Path> files = new LinkedList<>(); List<Path> files = new LinkedList<>();
if (!Files.isSymbolicLink(root)) { if (!Files.isSymbolicLink(root)) {
if (Files.isDirectory(root)) { if (Files.isDirectory(root)) {
List<Path> children = Files.list(root).collect(Collectors.toList()); try (Stream<Path> stream = Files.list(root)) {
if (children != null && children.size() > 0) { List<Path> children = stream.collect(Collectors.toList());
children.forEach(f -> { if (children != null && children.size() > 0) {
try { children.forEach(f -> {
files.addAll(expandFileset(f)); try {
} catch (IOException ex) { files.addAll(expandFileset(f));
throw new RuntimeException(ex); } catch (IOException ex) {
} throw new RuntimeException(ex);
}); }
} else { });
// Include empty folders } else {
files.add(root); // Include empty folders
files.add(root);
}
} }
} else { } else {
files.add(root); files.add(root);
@ -214,13 +216,13 @@ public class DeployParams {
// Validate temp dir // Validate temp dir
String root = (String)bundlerArguments.get( String root = (String)bundlerArguments.get(
Arguments.CLIOptions.TEMP_ROOT.getId()); Arguments.CLIOptions.TEMP_ROOT.getId());
if (root != null) { if (root != null && Files.exists(Path.of(root))) {
try { try (Stream<Path> stream = Files.walk(Path.of(root), 1)) {
String [] contents = Files.list(Path.of(root)) Path [] contents = stream.toArray(Path[]::new);
.toArray(String[]::new); // contents.length > 1 because Files.walk(path) includes path
if (contents != null && contents.length > 1) {
if (contents != null && contents.length > 0) { throw new PackagerException(
throw new PackagerException("ERR_BuildRootInvalid", root); "ERR_BuildRootInvalid", root);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
throw new PackagerException(ioe); throw new PackagerException(ioe);

@ -245,11 +245,24 @@ public final class BasicTest {
* @throws IOException * @throws IOException
*/ */
@Test @Test
public void testTemp() throws IOException { @Parameter("true")
final Path tempRoot = TKit.createTempDirectory("temp-root"); @Parameter("false")
public void testTemp(boolean withExistingTempDir) throws IOException {
final Path tempRoot = TKit.createTempDirectory("tmp");
// This Test has problems on windows where path in the temp dir are too long
// for the wix tools. We can't use a tempDir outside the TKit's WorkDir, so
// we minimize both the tempRoot directory name (above) and the tempDir name
// (below) to the extension part (which is necessary to differenciate between
// the multiple PackageTypes that will be run for one JPackageCommand).
// It might be beter if the whole work dir name was shortened from:
// jtreg_open_test_jdk_tools_jpackage_share_jdk_jpackage_tests_BasicTest_java.
Function<JPackageCommand, Path> getTempDir = cmd -> { Function<JPackageCommand, Path> getTempDir = cmd -> {
return tempRoot.resolve(cmd.outputBundle().getFileName()); String ext = cmd.outputBundle().getFileName().toString();
int i = ext.lastIndexOf(".");
if (i > 0 && i < (ext.length() - 1)) {
ext = ext.substring(i+1);
}
return tempRoot.resolve(ext);
}; };
Supplier<PackageTest> createTest = () -> { Supplier<PackageTest> createTest = () -> {
@ -259,7 +272,11 @@ public final class BasicTest {
.addInitializer(JPackageCommand::setDefaultInputOutput) .addInitializer(JPackageCommand::setDefaultInputOutput)
.addInitializer(cmd -> { .addInitializer(cmd -> {
Path tempDir = getTempDir.apply(cmd); Path tempDir = getTempDir.apply(cmd);
Files.createDirectories(tempDir); if (withExistingTempDir) {
Files.createDirectories(tempDir);
} else {
Files.createDirectories(tempDir.getParent());
}
cmd.addArguments("--temp", tempDir); cmd.addArguments("--temp", tempDir);
}); });
}; };