Skip to content

Commit

Permalink
[java] ExecutableFinder should keep order of PATH segments because an…
Browse files Browse the repository at this point in the history
… executable with a given name can appear multiple times in PATH
  • Loading branch information
barancev committed Apr 24, 2020
1 parent d6b82a6 commit 238ca35
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions java/client/src/org/openqa/selenium/os/ExecutableFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@

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
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down

0 comments on commit 238ca35

Please sign in to comment.