Skip to content

Commit

Permalink
7903515: Some default env vars on Windows not set
Browse files Browse the repository at this point in the history
Reviewed-by: jjg
  • Loading branch information
sormuras committed Aug 15, 2023
1 parent b9c6dcd commit 22f7332
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased](https://git.openjdk.org/jtreg/compare/jtreg-7.3+1...master)

_Nothing noteworthy, yet_
* Fixed setting default environment variables on Windows
* [CODETOOLS-7903515](https://bugs.openjdk.org/browse/CODETOOLS-7903515)

## [7.3](https://git.openjdk.org/jtreg/compare/jtreg-7.2+1...jtreg-7.3+1)

Expand Down
40 changes: 23 additions & 17 deletions src/share/classes/com/sun/javatest/regtest/tool/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2149,33 +2149,32 @@ private void startHttpServer() {
}

private Map<String, String> getEnvVars() {
Map<String, String> sysEnv = System.getenv();
Map<String, String> envVars = new TreeMap<>();
OS os = OS.current();
if (os.family.equals("windows")) {
addEnvVars(envVars, sysEnv, DEFAULT_WINDOWS_ENV_VARS);
addEnvVars(envVars, DEFAULT_WINDOWS_ENV_VARS);
// TODO PATH? MKS? Cygwin?
addEnvVars(envVars, sysEnv, "PATH"); // accept user's path, for now
addEnvVars(envVars, "PATH"); // accept user's path, for now
} else {
addEnvVars(envVars, sysEnv, DEFAULT_UNIX_ENV_VARS);
addEnvVars(envVars, sysEnv, e -> e.getKey().startsWith("XDG_"));
addEnvVars(envVars, sysEnv, "PATH=/bin:/usr/bin:/usr/sbin");
addEnvVars(envVars, DEFAULT_UNIX_ENV_VARS);
addEnvVarsByName(envVars, name -> name.startsWith("XDG_"));
addEnvVars(envVars, "PATH=/bin:/usr/bin:/usr/sbin");
}
addEnvVars(envVars, sysEnv, envVarArgs);
addEnvVars(envVars, sysEnv, e -> e.getKey().startsWith("JTREG_"));
addEnvVars(envVars, envVarArgs);
addEnvVarsByName(envVars, name -> name.startsWith("JTREG_"));

return envVars;
}

private void addEnvVars(Map<String, String> table, Map<String, String> sysEnv, String list) {
addEnvVars(table, sysEnv, list.split(","));
private void addEnvVars(Map<String, String> table, String list) {
addEnvVars(table, list.split(","));
}

private void addEnvVars(Map<String, String> table, Map<String, String> sysEnv, String[] list) {
addEnvVars(table, sysEnv, List.of(list));
private void addEnvVars(Map<String, String> table, String[] list) {
addEnvVars(table, List.of(list));
}

private void addEnvVars(Map<String, String> table, Map<String, String> sysEnv, List<String> list) {
private void addEnvVars(Map<String, String> table, List<String> list) {
if (list == null)
return;

Expand All @@ -2185,7 +2184,7 @@ private void addEnvVars(Map<String, String> table, Map<String, String> sysEnv, L
continue;
int eq = s.indexOf("=");
if (eq == -1) {
String value = sysEnv.get(s);
String value = getEnvironmentVariableOrNull(s);
if (value != null)
table.put(s, value);
} else if (eq > 0) {
Expand All @@ -2196,12 +2195,19 @@ private void addEnvVars(Map<String, String> table, Map<String, String> sysEnv, L
}
}

private void addEnvVars(Map<String, String> table, Map<String, String> sysEnv, Predicate<Map.Entry<String, String>> filter) {
sysEnv.entrySet().stream()
.filter(filter)
// This method streams over a case-sensitive set of entries, returned by System.getenv();
// Custom name filters can be implemented in a case-insensitive manner.
private void addEnvVarsByName(Map<String, String> table, Predicate<String> nameFilter) {
System.getenv().entrySet().stream()
.filter(e -> nameFilter.test(e.getKey()))
.forEach(e -> table.put(e.getKey(), e.getValue()));
}

// 7903515: Use case-insensitive System.getenv(String) here.
private static String getEnvironmentVariableOrNull(String name) {
return System.getenv(name);
}

private static String combineKeywords(String kw1, String kw2) {
return (kw1 == null ? kw2 : kw1 + " & " + kw2);
}
Expand Down

0 comments on commit 22f7332

Please sign in to comment.