20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.test;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.nio.file.Files;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Formatter;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 import org.graalvm.compiler.serviceprovider.GraalServices;
41 import org.graalvm.util.CollectionsUtil;
42 import org.junit.Assume;
43
44 /**
45 * Utility methods for spawning a VM in a subprocess during unit tests.
46 */
47 public final class SubprocessUtil {
48
49 private SubprocessUtil() {
50 }
51
52 /**
53 * Gets the command line for the current process.
54 *
55 * @return the command line arguments for the current process or {@code null} if they are not
56 * available
57 */
58 public static List<String> getProcessCommandLine() {
59 String processArgsFile = System.getenv().get("MX_SUBPROCESS_COMMAND_FILE");
60 if (processArgsFile != null) {
232 */
233 private static Subprocess javaHelper(List<String> vmArgs, Map<String, String> env, List<String> mainClassAndArgs) throws IOException, InterruptedException {
234 List<String> command = new ArrayList<>(vmArgs);
235 command.addAll(mainClassAndArgs);
236 ProcessBuilder processBuilder = new ProcessBuilder(command);
237 if (env != null) {
238 Map<String, String> processBuilderEnv = processBuilder.environment();
239 processBuilderEnv.putAll(env);
240 }
241 processBuilder.redirectErrorStream(true);
242 Process process = processBuilder.start();
243 BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
244 String line;
245 List<String> output = new ArrayList<>();
246 while ((line = stdout.readLine()) != null) {
247 output.add(line);
248 }
249 return new Subprocess(command, process.waitFor(), output);
250 }
251
252 private static final boolean isJava8OrEarlier = GraalServices.Java8OrEarlier;
253
254 private static boolean hasArg(String optionName) {
255 if (optionName.equals("-cp") || optionName.equals("-classpath")) {
256 return true;
257 }
258 if (!isJava8OrEarlier) {
259 if (optionName.equals("--version") ||
260 optionName.equals("--show-version") ||
261 optionName.equals("--dry-run") ||
262 optionName.equals("--disable-@files") ||
263 optionName.equals("--dry-run") ||
264 optionName.equals("--help") ||
265 optionName.equals("--help-extra")) {
266 return false;
267 }
268 if (optionName.startsWith("--")) {
269 return optionName.indexOf('=') == -1;
270 }
271 }
272 return false;
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.test;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.nio.file.Files;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Formatter;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
41 import org.graalvm.util.CollectionsUtil;
42 import org.junit.Assume;
43
44 /**
45 * Utility methods for spawning a VM in a subprocess during unit tests.
46 */
47 public final class SubprocessUtil {
48
49 private SubprocessUtil() {
50 }
51
52 /**
53 * Gets the command line for the current process.
54 *
55 * @return the command line arguments for the current process or {@code null} if they are not
56 * available
57 */
58 public static List<String> getProcessCommandLine() {
59 String processArgsFile = System.getenv().get("MX_SUBPROCESS_COMMAND_FILE");
60 if (processArgsFile != null) {
232 */
233 private static Subprocess javaHelper(List<String> vmArgs, Map<String, String> env, List<String> mainClassAndArgs) throws IOException, InterruptedException {
234 List<String> command = new ArrayList<>(vmArgs);
235 command.addAll(mainClassAndArgs);
236 ProcessBuilder processBuilder = new ProcessBuilder(command);
237 if (env != null) {
238 Map<String, String> processBuilderEnv = processBuilder.environment();
239 processBuilderEnv.putAll(env);
240 }
241 processBuilder.redirectErrorStream(true);
242 Process process = processBuilder.start();
243 BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
244 String line;
245 List<String> output = new ArrayList<>();
246 while ((line = stdout.readLine()) != null) {
247 output.add(line);
248 }
249 return new Subprocess(command, process.waitFor(), output);
250 }
251
252 private static final boolean isJava8OrEarlier = JavaVersionUtil.Java8OrEarlier;
253
254 private static boolean hasArg(String optionName) {
255 if (optionName.equals("-cp") || optionName.equals("-classpath")) {
256 return true;
257 }
258 if (!isJava8OrEarlier) {
259 if (optionName.equals("--version") ||
260 optionName.equals("--show-version") ||
261 optionName.equals("--dry-run") ||
262 optionName.equals("--disable-@files") ||
263 optionName.equals("--dry-run") ||
264 optionName.equals("--help") ||
265 optionName.equals("--help-extra")) {
266 return false;
267 }
268 if (optionName.startsWith("--")) {
269 return optionName.indexOf('=') == -1;
270 }
271 }
272 return false;
|