[java] ExecutableFinder should keep order of PATH segments because an executable with a given name can appear multiple times in PATH
package org.openqa.selenium.os;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
import static org.openqa.selenium.Platform.WINDOWS;
-import com.google.common.collect.ImmutableSet;
-
import org.openqa.selenium.Platform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Map;
public class ExecutableFinder {
- private static final ImmutableSet<String> ENDINGS = Platform.getCurrent().is(WINDOWS) ?
- ImmutableSet.of("", ".cmd", ".exe", ".com", ".bat") : ImmutableSet.of("");
-
- private final ImmutableSet.Builder<String> pathSegmentBuilder =
- new ImmutableSet.Builder<>();
+ private static final List<String> ENDINGS = Platform.getCurrent().is(WINDOWS) ?
+ Arrays.asList("", ".cmd", ".exe", ".com", ".bat") : singletonList("");
/**
* Find the executable by scanning the file system and the PATH. In the case of Windows this
@@ -55,12 +55,12 @@ public String find(String named) {
}
}
- addPathFromEnvironment();
+ List<String> pathSegments = new ArrayList<>(fromEnvironment());
if (Platform.getCurrent().is(Platform.MAC)) {
- addMacSpecificPath();
+ pathSegments.addAll(macSpecificPathSegments());
}
- for (String pathSegment : pathSegmentBuilder.build()) {
+ for (String pathSegment : pathSegments) {
for (String ending : ENDINGS) {
file = new File(pathSegment, named + ending);
if (canExecute(file)) {
@@ -71,7 +71,7 @@ public String find(String named) {
return null;
}
- private void addPathFromEnvironment() {
+ private List<String> fromEnvironment() {
String pathName = "PATH";
Map<String, String> env = System.getenv();
if (!env.containsKey(pathName)) {
@@ -83,20 +83,19 @@ private void addPathFromEnvironment() {
}
}
String path = env.get(pathName);
- if (path != null) {
- pathSegmentBuilder.add(path.split(File.pathSeparator));
- }
+ return path != null ? Arrays.asList(path.split(File.pathSeparator)) : emptyList();
}
- private void addMacSpecificPath() {
+ private List<String> macSpecificPathSegments() {
File pathFile = new File("/etc/paths");
if (pathFile.exists()) {
try {
- pathSegmentBuilder.addAll(Files.readAllLines(pathFile.toPath()));
+ return Files.readAllLines(pathFile.toPath());
} catch (IOException e) {
// Guess we won't include those, then
}
}
+ return emptyList();
}
private static boolean canExecute(File file) {
