author | herrick |
Tue, 24 Sep 2019 13:41:16 -0400 | |
branch | JDK-8200758-branch |
changeset 58301 | e0efb29609bd |
parent 58115 | 4a27283b542d |
child 58416 | f09bf58c1f17 |
permissions | -rw-r--r-- |
58113 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
package jdk.jpackage.test; |
|
24 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
25 |
import java.io.IOException; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
26 |
import java.nio.file.Files; |
58113 | 27 |
import java.nio.file.Path; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
28 |
import java.util.Arrays; |
58113 | 29 |
import java.util.HashMap; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
30 |
import java.util.List; |
58113 | 31 |
import java.util.Map; |
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
32 |
import java.util.Set; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
33 |
import java.util.function.Function; |
58113 | 34 |
import java.util.stream.Stream; |
35 |
||
36 |
public class LinuxHelper { |
|
37 |
private static String getRelease(JPackageCommand cmd) { |
|
38 |
return cmd.getArgumentValue("--linux-app-release", () -> "1"); |
|
39 |
} |
|
40 |
||
41 |
public static String getPackageName(JPackageCommand cmd) { |
|
42 |
cmd.verifyIsOfType(PackageType.LINUX); |
|
58115 | 43 |
return cmd.getArgumentValue("--linux-package-name", |
58113 | 44 |
() -> cmd.name().toLowerCase()); |
45 |
} |
|
46 |
||
47 |
static String getBundleName(JPackageCommand cmd) { |
|
48 |
cmd.verifyIsOfType(PackageType.LINUX); |
|
49 |
||
50 |
final PackageType packageType = cmd.packageType(); |
|
51 |
String format = null; |
|
52 |
switch (packageType) { |
|
53 |
case LINUX_DEB: |
|
54 |
format = "%s_%s-%s_%s"; |
|
55 |
break; |
|
56 |
||
57 |
case LINUX_RPM: |
|
58 |
format = "%s-%s-%s.%s"; |
|
59 |
break; |
|
60 |
} |
|
61 |
||
62 |
final String release = getRelease(cmd); |
|
63 |
final String version = cmd.version(); |
|
64 |
||
65 |
return String.format(format, |
|
66 |
getPackageName(cmd), version, release, getPackageArch(packageType)) |
|
67 |
+ packageType.getSuffix(); |
|
68 |
} |
|
69 |
||
70 |
public static Stream<Path> getPackageFiles(JPackageCommand cmd) { |
|
71 |
cmd.verifyIsOfType(PackageType.LINUX); |
|
72 |
||
73 |
final PackageType packageType = cmd.packageType(); |
|
74 |
final Path packageFile = cmd.outputBundle(); |
|
75 |
||
76 |
Executor exec = new Executor(); |
|
77 |
switch (packageType) { |
|
78 |
case LINUX_DEB: |
|
79 |
exec.setExecutable("dpkg") |
|
80 |
.addArgument("--contents") |
|
81 |
.addArgument(packageFile); |
|
82 |
break; |
|
83 |
||
84 |
case LINUX_RPM: |
|
85 |
exec.setExecutable("rpm") |
|
86 |
.addArgument("-qpl") |
|
87 |
.addArgument(packageFile); |
|
88 |
break; |
|
89 |
} |
|
90 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
91 |
Stream<String> lines = exec.executeAndGetOutput().stream(); |
58113 | 92 |
if (packageType == PackageType.LINUX_DEB) { |
93 |
// Typical text lines produced by dpkg look like: |
|
94 |
// drwxr-xr-x root/root 0 2019-08-30 05:30 ./opt/appcategorytest/runtime/lib/ |
|
95 |
// -rw-r--r-- root/root 574912 2019-08-30 05:30 ./opt/appcategorytest/runtime/lib/libmlib_image.so |
|
96 |
// Need to skip all fields but absolute path to file. |
|
97 |
lines = lines.map(line -> line.substring(line.indexOf(" ./") + 2)); |
|
98 |
} |
|
99 |
return lines.map(Path::of); |
|
100 |
} |
|
101 |
||
102 |
static Path getLauncherPath(JPackageCommand cmd) { |
|
103 |
cmd.verifyIsOfType(PackageType.LINUX); |
|
104 |
||
105 |
final String launcherName = cmd.name(); |
|
106 |
final String launcherRelativePath = Path.of("/bin", launcherName).toString(); |
|
107 |
||
108 |
return getPackageFiles(cmd).filter(path -> path.toString().endsWith( |
|
109 |
launcherRelativePath)).findFirst().or(() -> { |
|
110 |
Test.assertUnexpected(String.format( |
|
111 |
"Failed to find %s in %s package", launcherName, |
|
112 |
getPackageName(cmd))); |
|
113 |
return null; |
|
114 |
}).get(); |
|
115 |
} |
|
116 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
117 |
static long getInstalledPackageSizeKB(JPackageCommand cmd) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
118 |
cmd.verifyIsOfType(PackageType.LINUX); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
119 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
120 |
final Path packageFile = cmd.outputBundle(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
121 |
switch (cmd.packageType()) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
122 |
case LINUX_DEB: |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
123 |
return Long.parseLong(getDebBundleProperty(packageFile, |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
124 |
"Installed-Size")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
125 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
126 |
case LINUX_RPM: |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
127 |
return Long.parseLong(getRpmBundleProperty(packageFile, "Size")) >> 10; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
128 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
129 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
130 |
return 0; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
131 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
132 |
|
58113 | 133 |
static String getDebBundleProperty(Path bundle, String fieldName) { |
134 |
return new Executor() |
|
135 |
.setExecutable("dpkg-deb") |
|
136 |
.addArguments("-f", bundle.toString(), fieldName) |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
137 |
.executeAndGetFirstLineOfOutput(); |
58113 | 138 |
} |
139 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
140 |
static String getRpmBundleProperty(Path bundle, String fieldName) { |
58113 | 141 |
return new Executor() |
142 |
.setExecutable("rpm") |
|
143 |
.addArguments( |
|
144 |
"-qp", |
|
145 |
"--queryformat", |
|
146 |
String.format("%%{%s}", fieldName), |
|
147 |
bundle.toString()) |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
148 |
.executeAndGetFirstLineOfOutput(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
149 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
150 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
151 |
static void addDebBundleDesktopIntegrationVerifier(PackageTest test, |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
152 |
boolean integrated) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
153 |
Function<List<String>, String> verifier = (lines) -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
154 |
// Lookup for xdg commands |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
155 |
return lines.stream().filter(line -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
156 |
Set<String> words = Set.of(line.split("\\s+")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
157 |
return words.contains("xdg-desktop-menu") || words.contains( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
158 |
"xdg-mime") || words.contains("xdg-icon-resource"); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
159 |
}).findFirst().orElse(null); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
160 |
}; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
161 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
162 |
test.addBundleVerifier(cmd -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
163 |
Test.withTempDirectory(tempDir -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
164 |
try { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
165 |
// Extract control Debian package files into temporary directory |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
166 |
new Executor() |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
167 |
.setExecutable("dpkg") |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
168 |
.addArguments( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
169 |
"-e", |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
170 |
cmd.outputBundle().toString(), |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
171 |
tempDir.toString() |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
172 |
).execute().assertExitCodeIsZero(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
173 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
174 |
Path controlFile = Path.of("postinst"); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
175 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
176 |
// Lookup for xdg commands in postinstall script |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
177 |
String lineWithXsdCommand = verifier.apply( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
178 |
Files.readAllLines(tempDir.resolve(controlFile))); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
179 |
String assertMsg = String.format( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
180 |
"Check if %s@%s control file uses xdg commands", |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
181 |
cmd.outputBundle(), controlFile); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
182 |
if (integrated) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
183 |
Test.assertNotNull(lineWithXsdCommand, assertMsg); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
184 |
} else { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
185 |
Test.assertNull(lineWithXsdCommand, assertMsg); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
186 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
187 |
} catch (IOException ex) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
188 |
throw new RuntimeException(ex); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
189 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
190 |
}); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
191 |
}); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
192 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
193 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
194 |
static void initFileAssociationsTestFile(Path testFile) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
195 |
try { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
196 |
// Write something in test file. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
197 |
// On Ubuntu and Oracle Linux empty files are considered |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
198 |
// plain text. Seems like a system bug. |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
199 |
// |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
200 |
// $ >foo.jptest1 |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
201 |
// $ xdg-mime query filetype foo.jptest1 |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
202 |
// text/plain |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
203 |
// $ echo > foo.jptest1 |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
204 |
// $ xdg-mime query filetype foo.jptest1 |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
205 |
// application/x-jpackage-jptest1 |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
206 |
// |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
207 |
Files.write(testFile, Arrays.asList("")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
208 |
} catch (IOException ex) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
209 |
throw new RuntimeException(ex); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
210 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
211 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
212 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
213 |
private static Path getSystemDesktopFilesFolder() { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
214 |
return Stream.of("/usr/share/applications", |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
215 |
"/usr/local/share/applications").map(Path::of).filter(dir -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
216 |
return Files.exists(dir.resolve("defaults.list")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
217 |
}).findFirst().orElseThrow(() -> new RuntimeException( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
218 |
"Failed to locate system .desktop files folder")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
219 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
220 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
221 |
static void addFileAssociationsVerifier(PackageTest test, FileAssociations fa) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
222 |
test.addInstallVerifier(cmd -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
223 |
Test.withTempFile(fa.getSuffix(), testFile -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
224 |
initFileAssociationsTestFile(testFile); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
225 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
226 |
String mimeType = queryFileMimeType(testFile); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
227 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
228 |
Test.assertEquals(fa.getMime(), mimeType, String.format( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
229 |
"Check mime type of [%s] file", testFile)); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
230 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
231 |
String desktopFileName = queryMimeTypeDefaultHandler(mimeType); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
232 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
233 |
Path desktopFile = getSystemDesktopFilesFolder().resolve( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
234 |
desktopFileName); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
235 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
236 |
Test.assertFileExists(desktopFile, true); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
237 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
238 |
Test.trace(String.format("Reading [%s] file...", desktopFile)); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
239 |
String mimeHandler = null; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
240 |
try { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
241 |
mimeHandler = Files.readAllLines(desktopFile).stream().peek( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
242 |
v -> Test.trace(v)).filter( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
243 |
v -> v.startsWith("Exec=")).map( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
244 |
v -> v.split("=", 2)[1]).findFirst().orElseThrow(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
245 |
} catch (IOException ex) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
246 |
throw new RuntimeException(ex); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
247 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
248 |
Test.trace(String.format("Done")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
249 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
250 |
Test.assertEquals(cmd.launcherInstallationPath().toString(), |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
251 |
mimeHandler, String.format( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
252 |
"Check mime type handler is the main application launcher")); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
253 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
254 |
}); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
255 |
}); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
256 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
257 |
test.addUninstallVerifier(cmd -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
258 |
Test.withTempFile(fa.getSuffix(), testFile -> { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
259 |
initFileAssociationsTestFile(testFile); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
260 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
261 |
String mimeType = queryFileMimeType(testFile); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
262 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
263 |
Test.assertNotEquals(fa.getMime(), mimeType, String.format( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
264 |
"Check mime type of [%s] file", testFile)); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
265 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
266 |
String desktopFileName = queryMimeTypeDefaultHandler(fa.getMime()); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
267 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
268 |
Test.assertNull(desktopFileName, String.format( |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
269 |
"Check there is no default handler for [%s] mime type", |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
270 |
fa.getMime())); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
271 |
}); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
272 |
}); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
273 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
274 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
275 |
private static String queryFileMimeType(Path file) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
276 |
return new Executor() |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
277 |
.setExecutable("xdg-mime") |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
278 |
.addArguments("query", "filetype", file.toString()) |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
279 |
.executeAndGetFirstLineOfOutput(); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
280 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
281 |
|
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
282 |
private static String queryMimeTypeDefaultHandler(String mimeType) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
283 |
return new Executor() |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
284 |
.setExecutable("xdg-mime") |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
285 |
.addArguments("query", "default", mimeType) |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
286 |
.executeAndGetFirstLineOfOutput(); |
58113 | 287 |
} |
288 |
||
289 |
private static String getPackageArch(PackageType type) { |
|
290 |
if (archs == null) { |
|
291 |
archs = new HashMap<>(); |
|
292 |
} |
|
293 |
||
294 |
String arch = archs.get(type); |
|
295 |
if (arch == null) { |
|
296 |
Executor exec = new Executor(); |
|
297 |
switch (type) { |
|
298 |
case LINUX_DEB: |
|
299 |
exec.setExecutable("dpkg").addArgument( |
|
300 |
"--print-architecture"); |
|
301 |
break; |
|
302 |
||
303 |
case LINUX_RPM: |
|
304 |
exec.setExecutable("rpmbuild").addArgument( |
|
305 |
"--eval=%{_target_cpu}"); |
|
306 |
break; |
|
307 |
} |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
308 |
arch = exec.executeAndGetFirstLineOfOutput(); |
58113 | 309 |
archs.put(type, arch); |
310 |
} |
|
311 |
return arch; |
|
312 |
} |
|
313 |
||
314 |
static private Map<PackageType, String> archs; |
|
315 |
} |