Skip to content

Commit 6a31aae

Browse files
author
Alexey Semenyuk
committed
8350594: Misleading warning about install dir for DMG packaging
Reviewed-by: almatvee
1 parent 216f113 commit 6a31aae

File tree

5 files changed

+167
-32
lines changed

5 files changed

+167
-32
lines changed

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacBaseInstallerBundler.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -102,16 +102,20 @@ public abstract class MacBaseInstallerBundler extends AbstractBundler {
102102
static String getInstallDir(
103103
Map<String, ? super Object> params, boolean defaultOnly) {
104104
String returnValue = INSTALL_DIR.fetchFrom(params);
105-
if (defaultOnly && returnValue != null) {
106-
Log.info(I18N.getString("message.install-dir-ignored"));
105+
106+
final String defaultInstallDir;
107+
if (StandardBundlerParam.isRuntimeInstaller(params)) {
108+
defaultInstallDir = "/Library/Java/JavaVirtualMachines";
109+
} else {
110+
defaultInstallDir = "/Applications";
111+
}
112+
113+
if (defaultOnly && returnValue != null && !Path.of(returnValue).equals(Path.of(defaultInstallDir))) {
114+
Log.info(MessageFormat.format(I18N.getString("message.install-dir-ignored"), defaultInstallDir));
107115
returnValue = null;
108116
}
109117
if (returnValue == null) {
110-
if (StandardBundlerParam.isRuntimeInstaller(params)) {
111-
returnValue = "/Library/Java/JavaVirtualMachines";
112-
} else {
113-
returnValue = "/Applications";
114-
}
118+
returnValue = defaultInstallDir;
115119
}
116120
return returnValue;
117121
}

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# This code is free software; you can redistribute it and/or modify it
@@ -91,7 +91,7 @@ message.preparing-scripts=Preparing package scripts.
9191
message.preparing-distribution-dist=Preparing distribution.dist: {0}.
9292
message.signing.pkg=Warning: For signing PKG, you might need to set "Always Trust" for your certificate using "Keychain Access" tool.
9393
message.setfile.dmg=Setting custom icon on DMG file skipped because 'SetFile' utility was not found. Installing Xcode with Command Line Tools should resolve this issue.
94-
message.install-dir-ignored=Warning: "--install-dir" is not supported by DMG and will be default to /Applications.
94+
message.install-dir-ignored=Warning: "--install-dir" option is ignored for DMG packaging. The installation directory will default to {0}.
9595
message.codesign.failed.reason.app.content="codesign" failed and additional application content was supplied via the "--app-content" parameter. Probably the additional content broke the integrity of the application bundle and caused the failure. Ensure content supplied via the "--app-content" parameter does not break the integrity of the application bundle, or add it in the post-processing step.
9696
message.codesign.failed.reason.xcode.tools=Possible reason for "codesign" failure is missing Xcode with command line developer tools. Install Xcode with command line developer tools to see if it resolves the problem.
9797
warning.unsigned.app.image=Warning: Using unsigned app-image to build signed {0}.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public JPackageCommand(JPackageCommand cmd) {
7171
ignoreDefaultRuntime = cmd.ignoreDefaultRuntime;
7272
ignoreDefaultVerbose = cmd.ignoreDefaultVerbose;
7373
immutable = cmd.immutable;
74+
dmgInstallDir = cmd.dmgInstallDir;
7475
prerequisiteActions = new Actions(cmd.prerequisiteActions);
7576
verifyActions = new Actions(cmd.verifyActions);
7677
appLayoutAsserts = cmd.appLayoutAsserts;
@@ -497,7 +498,11 @@ public Path appInstallationDirectory() {
497498
}
498499

499500
if (TKit.isOSX()) {
500-
return MacHelper.getInstallationDirectory(this);
501+
if (packageType() == PackageType.MAC_DMG && dmgInstallDir != null) {
502+
return dmgInstallDir;
503+
} else {
504+
return MacHelper.getInstallationDirectory(this);
505+
}
501506
}
502507

503508
throw TKit.throwUnknownPlatformError();
@@ -868,6 +873,7 @@ private static JPackageCommand convertFromRuntime(JPackageCommand cmd) {
868873
var copy = new JPackageCommand(cmd);
869874
copy.immutable = false;
870875
copy.removeArgumentWithValue("--runtime-image");
876+
copy.dmgInstallDir = cmd.appInstallationDirectory();
871877
return copy;
872878
}
873879

@@ -1165,6 +1171,7 @@ public void run() {
11651171
private boolean ignoreDefaultRuntime;
11661172
private boolean ignoreDefaultVerbose;
11671173
private boolean immutable;
1174+
private Path dmgInstallDir;
11681175
private final Actions prerequisiteActions;
11691176
private final Actions verifyActions;
11701177
private Path executeInDirectory;

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,35 @@
2222
*/
2323
package jdk.jpackage.test;
2424

25+
import static java.util.stream.Collectors.toSet;
26+
2527
import java.io.ByteArrayInputStream;
2628
import java.io.IOException;
2729
import java.lang.reflect.InvocationTargetException;
2830
import java.lang.reflect.Method;
2931
import java.nio.charset.StandardCharsets;
3032
import java.nio.file.Files;
3133
import java.nio.file.Path;
32-
import java.util.List;
3334
import java.util.ArrayList;
35+
import java.util.List;
3436
import java.util.Optional;
3537
import java.util.Set;
3638
import java.util.regex.Pattern;
3739
import java.util.stream.Collectors;
38-
import static java.util.stream.Collectors.toSet;
3940
import java.util.stream.Stream;
4041
import javax.xml.parsers.DocumentBuilder;
4142
import javax.xml.parsers.DocumentBuilderFactory;
4243
import javax.xml.parsers.ParserConfigurationException;
4344
import javax.xml.xpath.XPath;
4445
import javax.xml.xpath.XPathConstants;
4546
import javax.xml.xpath.XPathFactory;
47+
import jdk.jpackage.internal.RetryExecutor;
48+
import jdk.jpackage.internal.util.PathUtils;
4649
import jdk.jpackage.internal.util.function.ThrowingConsumer;
4750
import jdk.jpackage.internal.util.function.ThrowingSupplier;
4851
import jdk.jpackage.test.PackageTest.PackageHandlers;
49-
import jdk.jpackage.internal.RetryExecutor;
50-
import jdk.jpackage.internal.util.PathUtils;
51-
import org.xml.sax.SAXException;
5252
import org.w3c.dom.NodeList;
53+
import org.xml.sax.SAXException;
5354

5455
public final class MacHelper {
5556

@@ -298,9 +299,18 @@ static String getBundleName(JPackageCommand cmd) {
298299

299300
static Path getInstallationDirectory(JPackageCommand cmd) {
300301
cmd.verifyIsOfType(PackageType.MAC);
301-
return Path.of(cmd.getArgumentValue("--install-dir",
302-
() -> cmd.isRuntime() ? "/Library/Java/JavaVirtualMachines" : "/Applications")).resolve(
303-
cmd.name() + (cmd.isRuntime() ? "" : ".app"));
302+
303+
final var defaultInstallLocation = Path.of(
304+
cmd.isRuntime() ? "/Library/Java/JavaVirtualMachines" : "/Applications");
305+
306+
final Path installLocation;
307+
if (cmd.packageType() == PackageType.MAC_DMG) {
308+
installLocation = defaultInstallLocation;
309+
} else {
310+
installLocation = cmd.getArgumentValue("--install-dir", () -> defaultInstallLocation, Path::of);
311+
}
312+
313+
return installLocation.resolve(cmd.name() + (cmd.isRuntime() ? "" : ".app"));
304314
}
305315

306316
static Path getUninstallCommand(JPackageCommand cmd) {

test/jdk/tools/jpackage/share/InstallDirTest.java

Lines changed: 127 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@
2222
*/
2323

2424
import java.nio.file.Path;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.stream.Stream;
2528
import jdk.internal.util.OperatingSystem;
26-
import jdk.jpackage.test.TKit;
27-
import jdk.jpackage.test.PackageTest;
28-
import jdk.jpackage.test.PackageType;
2929
import jdk.jpackage.test.Annotations.Parameter;
30+
import jdk.jpackage.test.Annotations.ParameterSupplier;
3031
import jdk.jpackage.test.Annotations.Test;
32+
import jdk.jpackage.test.JPackageCommand;
33+
import jdk.jpackage.test.JPackageStringBundle;
34+
import jdk.jpackage.test.PackageTest;
35+
import jdk.jpackage.test.PackageType;
36+
import jdk.jpackage.test.RunnablePackageTest.Action;
37+
import jdk.jpackage.test.TKit;
38+
import jdk.jpackage.test.TKit.TextStreamVerifier;
3139

3240
/**
3341
* Test --install-dir parameter. Output of the test should be
@@ -57,6 +65,7 @@
5765
* @key jpackagePlatformPackage
5866
* @build jdk.jpackage.test.*
5967
* @compile -Xlint:all -Werror InstallDirTest.java
68+
* @requires (jpackage.test.SQETest != null)
6069
* @run main/othervm/timeout=540 -Xmx512m jdk.jpackage.test.Main
6170
* --jpt-run=InstallDirTest.testCommon
6271
*/
@@ -68,10 +77,9 @@
6877
* @key jpackagePlatformPackage
6978
* @build jdk.jpackage.test.*
7079
* @compile -Xlint:all -Werror InstallDirTest.java
71-
* @requires (os.family == "linux")
7280
* @requires (jpackage.test.SQETest == null)
73-
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
74-
* --jpt-run=InstallDirTest.testLinuxInvalid
81+
* @run main/othervm/timeout=720 -Xmx512m jdk.jpackage.test.Main
82+
* --jpt-run=InstallDirTest
7583
*/
7684
public class InstallDirTest {
7785

@@ -92,11 +100,6 @@ public static void testCommon(Path installDir) {
92100
@Parameter("foo")
93101
@Parameter("/opt/foo/.././.")
94102
public static void testLinuxInvalid(String installDir) {
95-
testLinuxBad(installDir, "Invalid installation directory");
96-
}
97-
98-
private static void testLinuxBad(String installDir,
99-
String errorMessageSubstring) {
100103
new PackageTest().configureHelloApp()
101104
.setExpectedExitCode(1)
102105
.forTypes(PackageType.LINUX)
@@ -105,9 +108,120 @@ private static void testLinuxBad(String installDir,
105108
cmd.saveConsoleOutput(true);
106109
})
107110
.addBundleVerifier((cmd, result) -> {
108-
TKit.assertTextStream(errorMessageSubstring).apply(
109-
result.getOutput().stream());
111+
cmd.validateOutput(JPackageStringBundle.MAIN.cannedFormattedString("error.invalid-install-dir"));
110112
})
111113
.run();
112114
}
115+
116+
record DmgTestSpec(Path installDir, boolean installDirIgnored, boolean runtimeInstaller) {
117+
118+
DmgTestSpec {
119+
Objects.requireNonNull(installDir);
120+
}
121+
122+
static Builder build() {
123+
return new Builder();
124+
}
125+
126+
static final class Builder {
127+
128+
Builder ignoredInstallDir(String v) {
129+
installDir = Path.of(v);
130+
installDirIgnored = true;
131+
return this;
132+
}
133+
134+
Builder acceptedInstallDir(String v) {
135+
installDir = Path.of(v);
136+
installDirIgnored = false;
137+
return this;
138+
}
139+
140+
Builder runtimeInstaller() {
141+
runtimeInstaller = true;
142+
return this;
143+
}
144+
145+
DmgTestSpec create() {
146+
return new DmgTestSpec(installDir, installDirIgnored, runtimeInstaller);
147+
}
148+
149+
private Path installDir;
150+
private boolean installDirIgnored;
151+
private boolean runtimeInstaller;
152+
}
153+
154+
@Override
155+
public String toString() {
156+
final var sb = new StringBuilder();
157+
sb.append(installDir);
158+
if (installDirIgnored) {
159+
sb.append(", ignore");
160+
}
161+
if (runtimeInstaller) {
162+
sb.append(", runtime");
163+
}
164+
return sb.toString();
165+
}
166+
167+
void run() {
168+
final var test = new PackageTest().forTypes(PackageType.MAC_DMG).ignoreBundleOutputDir();
169+
if (runtimeInstaller) {
170+
test.addInitializer(cmd -> {
171+
cmd.removeArgumentWithValue("--input");
172+
});
173+
} else {
174+
test.configureHelloApp();
175+
}
176+
177+
test.addInitializer(JPackageCommand::setFakeRuntime).addInitializer(cmd -> {
178+
cmd.addArguments("--install-dir", installDir);
179+
cmd.validateOutput(createInstallDirWarningVerifier());
180+
}).run(Action.CREATE_AND_UNPACK);
181+
}
182+
183+
private TextStreamVerifier createInstallDirWarningVerifier() {
184+
final var verifier = TKit.assertTextStream(
185+
JPackageStringBundle.MAIN.cannedFormattedString("message.install-dir-ignored", defaultDmgInstallDir()).getValue());
186+
if (installDirIgnored) {
187+
return verifier;
188+
} else {
189+
return verifier.negate();
190+
}
191+
}
192+
193+
private String defaultDmgInstallDir() {
194+
if (runtimeInstaller) {
195+
return "/Library/Java/JavaVirtualMachines";
196+
} else {
197+
return "/Applications";
198+
}
199+
}
200+
}
201+
202+
@Test(ifOS = OperatingSystem.MACOS)
203+
@ParameterSupplier
204+
public static void testDmg(DmgTestSpec testSpec) {
205+
testSpec.run();
206+
}
207+
208+
public static List<Object[]> testDmg() {
209+
return Stream.of(
210+
DmgTestSpec.build().ignoredInstallDir("/foo"),
211+
DmgTestSpec.build().ignoredInstallDir("/foo/bar"),
212+
DmgTestSpec.build().ignoredInstallDir("/foo").runtimeInstaller(),
213+
DmgTestSpec.build().ignoredInstallDir("/foo/bar").runtimeInstaller(),
214+
215+
DmgTestSpec.build().ignoredInstallDir("/Library/Java/JavaVirtualMachines"),
216+
DmgTestSpec.build().ignoredInstallDir("/Applications").runtimeInstaller(),
217+
218+
DmgTestSpec.build().acceptedInstallDir("/Applications"),
219+
DmgTestSpec.build().ignoredInstallDir("/Applications/foo/bar/buz"),
220+
221+
DmgTestSpec.build().runtimeInstaller().acceptedInstallDir("/Library/Java/JavaVirtualMachines"),
222+
DmgTestSpec.build().runtimeInstaller().ignoredInstallDir("/Library/Java/JavaVirtualMachines/foo/bar/buz")
223+
).map(DmgTestSpec.Builder::create).map(testSpec -> {
224+
return new Object[] { testSpec };
225+
}).toList();
226+
}
113227
}

0 commit comments

Comments
 (0)