|
| 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