Skip to content

Commit 04d4227

Browse files
committed
Keep one environment variable per case-insensitive name on Windows
Inherited names arrive upper-cased, so an added Path used to leave PATH in the environment as well, and which of the two the process saw was not defined. Other platforms are unchanged.
1 parent 2f09f60 commit 04d4227

2 files changed

Lines changed: 134 additions & 5 deletions

File tree

‎src/main/java/org/codehaus/plexus/util/cli/Commandline.java‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.io.File;
6161
import java.io.IOException;
6262
import java.util.Collections;
63+
import java.util.Iterator;
6364
import java.util.LinkedHashMap;
6465
import java.util.Map;
6566
import java.util.Properties;
@@ -388,7 +389,18 @@ public void addArguments(String[] line) {
388389
*/
389390
public void addEnvironment(String name, String value) {
390391
// envVars.add( name + "=" + value );
391-
envVars.put(name, value);
392+
synchronized (envVars) {
393+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
394+
// Windows keeps one variable per case-insensitive name: the last spelling added wins
395+
for (Iterator<String> names = envVars.keySet().iterator(); names.hasNext(); ) {
396+
String existing = names.next();
397+
if (existing != null && !existing.equals(name) && existing.equalsIgnoreCase(name)) {
398+
names.remove();
399+
}
400+
}
401+
}
402+
envVars.put(name, value);
403+
}
392404
}
393405

394406
/**
@@ -398,12 +410,34 @@ public void addEnvironment(String name, String value) {
398410
public void addSystemEnvironment() throws Exception {
399411
Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
400412

401-
for (Object o : systemEnvVars.keySet()) {
402-
String key = (String) o;
403-
if (!envVars.containsKey(key)) {
404-
addEnvironment(key, systemEnvVars.getProperty(key));
413+
synchronized (envVars) {
414+
for (Object o : systemEnvVars.keySet()) {
415+
String key = (String) o;
416+
if (!hasEnvironment(key)) {
417+
addEnvironment(key, systemEnvVars.getProperty(key));
418+
}
419+
}
420+
}
421+
}
422+
423+
/**
424+
* Windows keeps one variable per case-insensitive name, so there an added <code>Path</code> stands for the
425+
* inherited <code>PATH</code> as well.
426+
*/
427+
private boolean hasEnvironment(String name) {
428+
if (envVars.containsKey(name)) {
429+
return true;
430+
}
431+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
432+
synchronized (envVars) {
433+
for (String own : envVars.keySet()) {
434+
if (name.equalsIgnoreCase(own)) {
435+
return true;
436+
}
437+
}
405438
}
406439
}
440+
return false;
407441
}
408442

409443
/**
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.codehaus.plexus.util.cli;
2+
3+
/*
4+
* Copyright The Codehaus Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.condition.DisabledOnOs;
26+
import org.junit.jupiter.api.condition.EnabledOnOs;
27+
import org.junit.jupiter.api.condition.OS;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
/**
33+
* The environment a {@link Commandline} hands to the process. Windows keeps one variable per case-insensitive name,
34+
* and the inherited names reach the command line upper-cased, see {@link CommandLineUtils#getSystemEnvVars()}.
35+
*/
36+
class CommandlineEnvironmentTest {
37+
38+
@Test
39+
@EnabledOnOs(OS.WINDOWS)
40+
void variableAddedInAnotherSpellingReplacesTheInheritedOne() throws Exception {
41+
Commandline cl = new Commandline();
42+
cl.addEnvironment("Path", "C:\\own");
43+
44+
assertEquals(Collections.singletonList("Path=C:\\own"), paths(cl));
45+
}
46+
47+
@Test
48+
@EnabledOnOs(OS.WINDOWS)
49+
void variableAddedAfterTheInheritedOnesReplacesTheInheritedOne() throws Exception {
50+
Commandline cl = new Commandline();
51+
// as a first execute() does
52+
cl.addSystemEnvironment();
53+
cl.addEnvironment("Path", "C:\\own");
54+
55+
assertEquals(Collections.singletonList("Path=C:\\own"), paths(cl));
56+
}
57+
58+
@Test
59+
@EnabledOnOs(OS.WINDOWS)
60+
void lastSpellingAddedWins() throws Exception {
61+
Commandline cl = new Commandline();
62+
cl.addEnvironment("Path", "C:\\first");
63+
cl.addEnvironment("PATH", "C:\\second");
64+
65+
assertEquals(Collections.singletonList("PATH=C:\\second"), paths(cl));
66+
}
67+
68+
@Test
69+
@DisabledOnOs(OS.WINDOWS)
70+
void spellingsAreDifferentVariablesElsewhere() throws Exception {
71+
Commandline cl = new Commandline();
72+
cl.addEnvironment("Path", "/own");
73+
74+
List<String> paths = paths(cl);
75+
assertEquals(2, paths.size(), paths.toString());
76+
}
77+
78+
@Test
79+
void nullNameIsStillAccepted() throws Exception {
80+
Commandline cl = new Commandline();
81+
cl.addEnvironment(null, "value");
82+
83+
assertTrue(Arrays.asList(cl.getEnvironmentVariables()).contains("null=value"));
84+
}
85+
86+
private static List<String> paths(Commandline cl) throws Exception {
87+
List<String> paths = new ArrayList<>();
88+
for (String variable : cl.getEnvironmentVariables()) {
89+
if (variable.regionMatches(true, 0, "PATH=", 0, 5)) {
90+
paths.add(variable);
91+
}
92+
}
93+
return paths;
94+
}
95+
}

0 commit comments

Comments
 (0)