From edc7a8a801c6e305f430202d53cd0e7e0cd54a58 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 11 Aug 2024 09:55:58 +0200 Subject: [PATCH] Environment variable Path should be used as case-insensitive fix #438 --- pom.xml | 2 ++ .../java/org/codehaus/mojo/exec/ExecMojo.java | 18 ++++++++++++------ .../org/codehaus/mojo/exec/ExecMojoTest.java | 15 ++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 4f8cf34c..737b3997 100644 --- a/pom.xml +++ b/pom.xml @@ -137,6 +137,8 @@ 9.7 1C 2024-08-06T21:32:24Z + + false diff --git a/src/main/java/org/codehaus/mojo/exec/ExecMojo.java b/src/main/java/org/codehaus/mojo/exec/ExecMojo.java index 8d2f9bb5..e2c437a3 100644 --- a/src/main/java/org/codehaus/mojo/exec/ExecMojo.java +++ b/src/main/java/org/codehaus/mojo/exec/ExecMojo.java @@ -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; @@ -488,8 +489,7 @@ private Map handleSystemEnvVariables() throws MojoExecutionExcep } if (this.getLog().isDebugEnabled()) { - Set keys = new TreeSet<>(); - keys.addAll(enviro.keySet()); + Set keys = new TreeSet<>(enviro.keySet()); for (String key : keys) { this.getLog().debug("env: " + key + "=" + enviro.get(key)); } @@ -758,10 +758,13 @@ CommandLine getExecutablePath(Map enviro, File dir) { exec = tc.findTool(executable); } else { if (OS.isFamilyWindows()) { + System.out.println("enviro: " + enviro); List paths = this.getExecutablePaths(enviro); paths.add(0, dir.getAbsolutePath()); + System.out.println("win paths: " + paths); exec = findExecutable(executable, paths); + System.out.println("win executable: " + executable + " found exec: " + exec); } } } @@ -825,10 +828,13 @@ private List getExecutablePaths(Map enviro) { List 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; } diff --git a/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java b/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java index 3a0773a2..431b3e73 100644 --- a/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java +++ b/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java @@ -144,17 +144,18 @@ public void testGetExecutablePath() throws IOException { final String comSpec = System.getenv("ComSpec"); + // for windows scripts cmd should be used 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); + realMojo.setExecutable("javax"); cmd = realMojo.getExecutablePath(enviro, workdir); - assertTrue( - "is bat file on windows' PATH, execute using ComSpec.", - cmd.getExecutable().equals(comSpec)); + assertEquals("full path is discovered.", path + File.separator + "javax", cmd.getExecutable()); + f.delete(); assertFalse("file deleted...", f.exists()); }