Skip to content

Commit

Permalink
Environment variable Path should be used as case-insensitive
Browse files Browse the repository at this point in the history
fix #438
  • Loading branch information
slawekjaranowski committed Aug 11, 2024
1 parent a8c4f94 commit af12344
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
<asm.version>9.7</asm.version>
<invoker.parallelThreads>1C</invoker.parallelThreads>
<project.build.outputTimestamp>2024-08-06T21:32:24Z</project.build.outputTimestamp>

<surefire.redirectTestOutputToFile>false</surefire.redirectTestOutputToFile>
</properties>

<dependencies>
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/codehaus/mojo/exec/ExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;
Expand Down Expand Up @@ -488,8 +489,7 @@ private Map<String, String> handleSystemEnvVariables() throws MojoExecutionExcep
}

if (this.getLog().isDebugEnabled()) {
Set<String> keys = new TreeSet<>();
keys.addAll(enviro.keySet());
Set<String> keys = new TreeSet<>(enviro.keySet());
for (String key : keys) {
this.getLog().debug("env: " + key + "=" + enviro.get(key));
}
Expand Down Expand Up @@ -759,9 +759,11 @@ CommandLine getExecutablePath(Map<String, String> enviro, File dir) {
} else {
if (OS.isFamilyWindows()) {
List<String> paths = this.getExecutablePaths(enviro);
System.out.println("win paths: " + paths);
paths.add(0, dir.getAbsolutePath());

exec = findExecutable(executable, paths);
System.out.println("win exec: " + exec);
}
}
}
Expand Down Expand Up @@ -825,10 +827,13 @@ private List<String> getExecutablePaths(Map<String, String> enviro) {
List<String> paths = new ArrayList<>();
paths.add("");

String path = enviro.get("PATH");
if (path != null) {
paths.addAll(Arrays.asList(StringUtils.split(path, File.pathSeparator)));
}
enviro.entrySet().stream()
.filter(entry -> "PATH".equalsIgnoreCase(entry.getKey()))
.map(Map.Entry::getValue)
.filter(Objects::nonNull)
.map(path -> path.split(File.pathSeparator))
.map(Arrays::asList)
.forEach(paths::addAll);

return paths;
}
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,16 @@ public void testGetExecutablePath() throws IOException {

realMojo.setExecutable("javax.bat");
cmd = realMojo.getExecutablePath(enviro, workdir);
assertTrue(
"is bat file on windows, execute using ComSpec.",
cmd.getExecutable().equals(comSpec));
assertEquals("is bat file on windows, execute using ComSpec.", comSpec, cmd.getExecutable());
assertEquals("is /c argument pass using ComSpec.", "/c", cmd.getArguments()[0]);

enviro.put("PATH", workdir.getAbsolutePath() + File.separator + "target");
String path = workdir.getAbsolutePath() + File.separator + "target";
enviro.put("Path", path);
cmd = realMojo.getExecutablePath(enviro, workdir);
assertTrue(
"is bat file on windows' PATH, execute using ComSpec.",
cmd.getExecutable().equals(comSpec));
assertEquals("is bat file on windows' PATH, execute using ComSpec.", comSpec, cmd.getExecutable());
assertEquals("is /c argument pass using ComSpec.", "/c", cmd.getArguments()[0]);
assertEquals("full path is discovered.", path + File.separator + "javax.bat", cmd.getArguments()[1]);

f.delete();
assertFalse("file deleted...", f.exists());
}
Expand Down

0 comments on commit af12344

Please sign in to comment.