olamy commented on code in PR #3575:
URL: https://github.com/apache/maven-surefire/pull/3575#discussion_r4672073896
##########
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/Commandline.java:
##########
@@ -43,29 +38,13 @@ public Commandline() {
public Commandline(String[] excludedEnvironmentVariables) {
this.excludedEnvironmentVariables = new ConcurrentLinkedDeque<>();
- this.addedEnvironmentVariables = new HashSet<>();
Collections.addAll(this.excludedEnvironmentVariables,
excludedEnvironmentVariables);
}
@Override
- public void addEnvironment(String name, String value) {
- super.addEnvironment(name, value);
- addedEnvironmentVariables.add(name);
- }
-
- @Override
- public String[] getEnvironmentVariables() {
- String[] envs = super.getEnvironmentVariables();
- Set<String> result = new HashSet<>(Arrays.asList(envs));
- result.addAll(addedEnvironmentVariables);
- return excludedEnvironmentVariables.isEmpty()
- ? result.toArray(new String[0])
- : result.stream()
- .filter(env -> {
- String varName =
- env != null && env.contains("=") ?
env.substring(0, env.indexOf('=')) : env;
- return
!excludedEnvironmentVariables.contains(varName);
- })
- .toArray(String[]::new);
+ protected Map<String, String> environment() {
+ Map<String, String> env = super.environment();
+ env.keySet().removeAll(excludedEnvironmentVariables);
Review Comment:
Same problem with exclusions. `removeAll` is case-sensitive, so on Windows
`excludedEnvironmentVariables=PATH` doesn't remove `Path` anymore, and that's
the name you get when Maven runs from cmd, PowerShell or an IDE. It used to
work because the names were already upper-cased.
With the case-insensitive map from the parent, #3634 removes the names one
by one:
```java
for (String name : excludedEnvironmentVariables) {
env.remove(name);
}
```
`keySet().removeAll` won't do it even then, because sometimes it calls
`contains` on our list instead, and that's case-sensitive.
Also, the `doesNotContain("PATH=")` checks in ForkConfigurationTest can't
fail, because they compare whole `NAME=value` entries. #3634 checks the names
instead.
##########
surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.surefire.extensions.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+/**
+ * The command line of a process Surefire starts: an executable, its
arguments, the environment and the working
+ * directory, started with {@link ProcessBuilder}. The process is a direct
child; there is no shell in between.
+ */
+public class Commandline {
+ private static final boolean WINDOWS =
+ System.getProperty("os.name",
"").toLowerCase(java.util.Locale.ROOT).startsWith("windows");
+
+ private static final char[] CMD_SPECIAL_CHARS = {' ', '\t', '&', '<', '>',
'(', ')', '@', '^', '|'};
+
+ private String executable;
+ private final List<String> arguments = new ArrayList<>();
+ private final Map<String, String> addedEnvironment = new LinkedHashMap<>();
+ private File workingDirectory;
+
+ public Commandline() {}
+
+ /**
+ * @param commandLine the executable followed by its arguments, split like
a shell would, see
+ * {@link #translateCommandline(String)}
+ */
+ public Commandline(String commandLine) {
+ String[] parts = translateCommandline(commandLine);
+ if (parts.length > 0) {
+ executable = parts[0];
+ arguments.addAll(java.util.Arrays.asList(parts).subList(1,
parts.length));
Review Comment:
why using fqcn?
##########
surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.surefire.extensions.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+/**
+ * The command line of a process Surefire starts: an executable, its
arguments, the environment and the working
+ * directory, started with {@link ProcessBuilder}. The process is a direct
child; there is no shell in between.
+ */
+public class Commandline {
+ private static final boolean WINDOWS =
+ System.getProperty("os.name",
"").toLowerCase(java.util.Locale.ROOT).startsWith("windows");
+
+ private static final char[] CMD_SPECIAL_CHARS = {' ', '\t', '&', '<', '>',
'(', ')', '@', '^', '|'};
+
+ private String executable;
+ private final List<String> arguments = new ArrayList<>();
+ private final Map<String, String> addedEnvironment = new LinkedHashMap<>();
+ private File workingDirectory;
+
+ public Commandline() {}
+
+ /**
+ * @param commandLine the executable followed by its arguments, split like
a shell would, see
+ * {@link #translateCommandline(String)}
+ */
+ public Commandline(String commandLine) {
+ String[] parts = translateCommandline(commandLine);
+ if (parts.length > 0) {
+ executable = parts[0];
+ arguments.addAll(java.util.Arrays.asList(parts).subList(1,
parts.length));
+ }
+ }
+
+ public void setExecutable(String executable) {
+ this.executable = executable;
+ }
+
+ public String getExecutable() {
+ return executable;
+ }
+
+ public Argument createArg() {
+ return new Argument();
+ }
+
+ public void addArgument(String argument) {
+ arguments.add(argument);
+ }
+
+ public void addArguments(String... args) {
+ Collections.addAll(arguments, args);
+ }
+
+ /**
+ * @return the arguments, without the executable
+ */
+ public String[] getArguments() {
+ return arguments.toArray(new String[0]);
+ }
+
+ /**
+ * @return the executable followed by the arguments
+ */
+ public String[] getCommandline() {
+ List<String> all = new ArrayList<>(arguments.size() + 1);
+ all.add(executable);
+ all.addAll(arguments);
+ return all.toArray(new String[0]);
+ }
+
+ public void setWorkingDirectory(String path) {
+ this.workingDirectory = path == null ? null : new File(path);
+ }
+
+ public void setWorkingDirectory(File workingDirectory) {
+ this.workingDirectory = workingDirectory;
+ }
+
+ public File getWorkingDirectory() {
+ return workingDirectory;
+ }
+
+ public void addEnvironment(String name, String value) {
+ addedEnvironment.put(name, value);
+ }
+
+ /**
+ * The environment the process gets: the environment of this JVM, with
{@link #addEnvironment added} variables
+ * overriding it. Subclasses may remove variables.
+ *
+ * @return a mutable copy
+ */
+ protected Map<String, String> environment() {
+ Map<String, String> env = new LinkedHashMap<>(System.getenv());
Review Comment:
I think we lost something on Windows here. maven-shared-utils used to
upper-case the inherited variable names on Windows, so a `<PATH>` in
`environmentVariables` replaced the existing `Path`. Now we copy
`System.getenv()` as is into a case-sensitive map, so the fork gets both `Path`
and `PATH`, and `System.getenv("PATH")` in the fork can return the old value.
Windows doesn't care about case anyway, so #3634 uses a case-insensitive map
there:
```java
Map<String, String> env = WINDOWS ? new
TreeMap<>(String.CASE_INSENSITIVE_ORDER) : new LinkedHashMap<>();
env.putAll(System.getenv());
env.putAll(addedEnvironment);
```
I haven't tried it on a real Windows box, this is from reading the code.
##########
surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.surefire.extensions.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+/**
+ * The command line of a process Surefire starts: an executable, its
arguments, the environment and the working
+ * directory, started with {@link ProcessBuilder}. The process is a direct
child; there is no shell in between.
+ */
+public class Commandline {
+ private static final boolean WINDOWS =
+ System.getProperty("os.name",
"").toLowerCase(java.util.Locale.ROOT).startsWith("windows");
+
+ private static final char[] CMD_SPECIAL_CHARS = {' ', '\t', '&', '<', '>',
'(', ')', '@', '^', '|'};
+
+ private String executable;
+ private final List<String> arguments = new ArrayList<>();
+ private final Map<String, String> addedEnvironment = new LinkedHashMap<>();
+ private File workingDirectory;
+
+ public Commandline() {}
+
+ /**
+ * @param commandLine the executable followed by its arguments, split like
a shell would, see
+ * {@link #translateCommandline(String)}
+ */
+ public Commandline(String commandLine) {
+ String[] parts = translateCommandline(commandLine);
+ if (parts.length > 0) {
+ executable = parts[0];
+ arguments.addAll(java.util.Arrays.asList(parts).subList(1,
parts.length));
+ }
+ }
+
+ public void setExecutable(String executable) {
+ this.executable = executable;
+ }
+
+ public String getExecutable() {
+ return executable;
+ }
+
+ public Argument createArg() {
+ return new Argument();
+ }
+
+ public void addArgument(String argument) {
+ arguments.add(argument);
+ }
+
+ public void addArguments(String... args) {
+ Collections.addAll(arguments, args);
+ }
+
+ /**
+ * @return the arguments, without the executable
+ */
+ public String[] getArguments() {
+ return arguments.toArray(new String[0]);
+ }
+
+ /**
+ * @return the executable followed by the arguments
+ */
+ public String[] getCommandline() {
+ List<String> all = new ArrayList<>(arguments.size() + 1);
+ all.add(executable);
+ all.addAll(arguments);
+ return all.toArray(new String[0]);
+ }
+
+ public void setWorkingDirectory(String path) {
+ this.workingDirectory = path == null ? null : new File(path);
+ }
+
+ public void setWorkingDirectory(File workingDirectory) {
+ this.workingDirectory = workingDirectory;
+ }
+
+ public File getWorkingDirectory() {
+ return workingDirectory;
+ }
+
+ public void addEnvironment(String name, String value) {
+ addedEnvironment.put(name, value);
+ }
+
+ /**
+ * The environment the process gets: the environment of this JVM, with
{@link #addEnvironment added} variables
+ * overriding it. Subclasses may remove variables.
+ *
+ * @return a mutable copy
+ */
+ protected Map<String, String> environment() {
+ Map<String, String> env = new LinkedHashMap<>(System.getenv());
+ env.putAll(addedEnvironment);
+ return env;
+ }
+
+ /**
+ * @return the environment of the process as {@code NAME=value} entries
+ */
+ public String[] getEnvironmentVariables() {
+ Map<String, String> env = environment();
+ String[] result = new String[env.size()];
+ int i = 0;
+ for (Map.Entry<String, String> e : env.entrySet()) {
+ result[i++] = e.getKey() + "=" + e.getValue();
+ }
+ return result;
+ }
+
+ public ProcessBuilder toProcessBuilder() {
+ ProcessBuilder builder = new ProcessBuilder(getCommandline());
+ builder.directory(workingDirectory);
Review Comment:
One more, on Linux and macOS. With the old `sh -c "cd <dir> && java ..."`
wrapper, `PWD` was the fork's working directory. With `builder.directory()` the
fork just inherits Maven's `PWD`. So in a multi-module build, a test that
builds paths from `System.getenv("PWD")` now points at the root instead of the
module.
#3634 sets it again when there's a working directory. The configured
variables go in after, so a `PWD` set by the user still wins:
```java
if (!WINDOWS && workingDirectory != null) {
env.put("PWD",
workingDirectory.toPath().toAbsolutePath().normalize().toString());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]