Skip to content

Commit 763ad57

Browse files
ankhuntiPaul Hohensee
authored andcommitted
8347300: Don't exclude the "PATH" var from the environment when running app launchers in jpackage tests
Backport-of: d69463e4bcbddd346b9486059c5ad3a1cb555632
1 parent 56a6901 commit 763ad57

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -53,7 +53,6 @@ public static Executor of(String... cmdline) {
5353

5454
public Executor() {
5555
saveOutputType = new HashSet<>(Set.of(SaveOutputType.NONE));
56-
removePathEnvVar = false;
5756
winEnglishOutput = false;
5857
}
5958

@@ -86,8 +85,8 @@ public Executor setExecutable(JavaTool v) {
8685
return setExecutable(v.getPath());
8786
}
8887

89-
public Executor setRemovePathEnvVar(boolean value) {
90-
removePathEnvVar = value;
88+
public Executor removeEnvVar(String envVarName) {
89+
removeEnvVars.add(Objects.requireNonNull(envVarName));
9190
return this;
9291
}
9392

@@ -372,10 +371,12 @@ private Result runExecutable() throws IOException, InterruptedException {
372371
builder.directory(directory.toFile());
373372
sb.append(String.format("; in directory [%s]", directory));
374373
}
375-
if (removePathEnvVar) {
376-
// run this with cleared Path in Environment
377-
TKit.trace("Clearing PATH in environment");
378-
builder.environment().remove("PATH");
374+
if (!removeEnvVars.isEmpty()) {
375+
final var envComm = Comm.compare(builder.environment().keySet(), removeEnvVars);
376+
builder.environment().keySet().removeAll(envComm.common());
377+
envComm.common().forEach(envVar -> {
378+
TKit.trace(String.format("Clearing %s in environment", envVar));
379+
});
379380
}
380381

381382
trace("Execute " + sb.toString() + "...");
@@ -504,7 +505,7 @@ private static void trace(String msg) {
504505
private Path executable;
505506
private Set<SaveOutputType> saveOutputType;
506507
private Path directory;
507-
private boolean removePathEnvVar;
508+
private Set<String> removeEnvVars = new HashSet<>();
508509
private boolean winEnglishOutput;
509510
private String winTmpDir = null;
510511

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -472,14 +472,14 @@ private Executor getExecutor(String...args) {
472472
}
473473
}
474474

475-
final List<String> launcherArgs = List.of(args);
476-
return new Executor()
475+
final var executor = new Executor()
477476
.setDirectory(outputFile.getParent())
478477
.saveOutput(saveOutput)
479478
.dumpOutput()
480-
.setRemovePathEnvVar(removePathEnvVar)
481479
.setExecutable(executablePath)
482-
.addArguments(launcherArgs);
480+
.addArguments(List.of(args));
481+
482+
return configureEnvironment(executor);
483483
}
484484

485485
private boolean launcherNoExit;
@@ -496,6 +496,14 @@ public static AppOutputVerifier assertApp(Path helloAppLauncher) {
496496
return new AppOutputVerifier(helloAppLauncher);
497497
}
498498

499+
public static Executor configureEnvironment(Executor executor) {
500+
if (CLEAR_JAVA_ENV_VARS) {
501+
executor.removeEnvVar("JAVA_TOOL_OPTIONS");
502+
executor.removeEnvVar("_JAVA_OPTIONS");
503+
}
504+
return executor;
505+
}
506+
499507
static final String OUTPUT_FILENAME = "appOutput.txt";
500508

501509
private final JavaAppDesc appDesc;
@@ -505,4 +513,7 @@ public static AppOutputVerifier assertApp(Path helloAppLauncher) {
505513

506514
private static final String CLASS_NAME = HELLO_JAVA.getFileName().toString().split(
507515
"\\.", 2)[0];
516+
517+
private static final boolean CLEAR_JAVA_ENV_VARS = Optional.ofNullable(
518+
TKit.getConfigProperty("clear-app-launcher-java-env-vars")).map(Boolean::parseBoolean).orElse(false);
508519
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -30,6 +30,7 @@
3030
import jdk.jpackage.test.JPackageCommand;
3131
import jdk.jpackage.test.Annotations.Test;
3232
import jdk.jpackage.test.Executor;
33+
import static jdk.jpackage.test.HelloApp.configureEnvironment;
3334
import jdk.jpackage.test.TKit;
3435

3536
/**
@@ -53,6 +54,7 @@ public static void test() throws Exception {
5354

5455
JPackageCommand cmd = JPackageCommand
5556
.helloAppImage(TEST_APP_JAVA + "*Hello")
57+
.ignoreFakeRuntime()
5658
.addArguments("--java-options", "-D" + testAddDirProp
5759
+ "=$APPDIR");
5860

@@ -62,7 +64,7 @@ public static void test() throws Exception {
6264

6365
final int attempts = 3;
6466
final int waitBetweenAttemptsSeconds = 5;
65-
List<String> output = new Executor()
67+
List<String> output = configureEnvironment(new Executor())
6668
.saveOutput()
6769
.setExecutable(cmd.appLauncherPath().toAbsolutePath())
6870
.addArguments("--print-env-var=" + envVarName)

test/jdk/tools/jpackage/windows/WinChildProcessTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 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
@@ -40,6 +40,7 @@
4040
import java.nio.file.Path;
4141

4242
import jdk.jpackage.test.JPackageCommand;
43+
import static jdk.jpackage.test.HelloApp.configureEnvironment;
4344
import jdk.jpackage.test.Annotations.Test;
4445
import jdk.jpackage.test.Executor;
4546
import jdk.jpackage.test.TKit;
@@ -54,14 +55,15 @@ public static void test() {
5455
long childPid = 0;
5556
try {
5657
JPackageCommand cmd = JPackageCommand
57-
.helloAppImage(TEST_APP_JAVA + "*Hello");
58+
.helloAppImage(TEST_APP_JAVA + "*Hello")
59+
.ignoreFakeRuntime();
5860

5961
// Create the image of the third party application launcher
6062
cmd.executeAndAssertImageCreated();
6163

6264
// Start the third party application launcher and dump and save the
6365
// output of the application
64-
List<String> output = new Executor().saveOutput().dumpOutput()
66+
List<String> output = configureEnvironment(new Executor()).saveOutput().dumpOutput()
6567
.setExecutable(cmd.appLauncherPath().toAbsolutePath())
6668
.execute(0).getOutput();
6769
String pidStr = output.get(0);

0 commit comments

Comments
 (0)