Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class CredentialHelper {

private static final String USR_LOCAL_BIN = "/usr/local/bin/";

private static final String OPT_HOMEBREW_BIN = "/opt/homebrew/bin/";

private static final String[] MAC_OS_BIN_DIRECTORIES = { OPT_HOMEBREW_BIN, USR_LOCAL_BIN };

private static final Set<String> CREDENTIAL_NOT_FOUND_MESSAGES = Set.of("credentials not found in native keychain",
"no credentials server URL", "no credentials username");

Expand Down Expand Up @@ -92,16 +96,22 @@ private Process start(ProcessBuilder processBuilder) throws IOException {
if (!Platform.isMac()) {
throw ex;
}
try {
List<String> command = new ArrayList<>(processBuilder.command());
command.set(0, USR_LOCAL_BIN + command.get(0));
return processBuilder.command(command).start();
}
catch (Exception suppressed) {
// Suppresses the exception and rethrows the original exception
ex.addSuppressed(suppressed);
throw ex;
String executable = processBuilder.command().get(0);
for (String binDirectory : MAC_OS_BIN_DIRECTORIES) {
try {
List<String> command = new ArrayList<>(processBuilder.command());
if (executable.startsWith(binDirectory)) {
continue;
}
command.set(0, binDirectory + executable);
return processBuilder.command(command).start();
}
catch (Exception suppressed) {
// Suppresses the exception and rethrows the original exception
ex.addSuppressed(suppressed);
}
}
throw ex;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ void getWhenExecutableDoesNotExistErrorThrowsException() {
.satisfies((ex) -> {
if (Platform.isMac()) {
assertThat(ex.getMessage()).doesNotContain("/usr/local/bin/");
assertThat(ex.getSuppressed()).allSatisfy((suppressed) -> assertThat(suppressed)
assertThat(ex.getSuppressed()).anySatisfy((suppressed) -> assertThat(suppressed)
.hasMessageContaining("/opt/homebrew/bin/" + executable));
assertThat(ex.getSuppressed()).anySatisfy((suppressed) -> assertThat(suppressed)
.hasMessageContaining("/usr/local/bin/" + executable));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class ProcessRunner {

private static final String USR_LOCAL_BIN = "/usr/local/bin";

private static final String OPT_HOMEBREW_BIN = "/opt/homebrew/bin";

private static final String[] MAC_OS_BIN_DIRECTORIES = { OPT_HOMEBREW_BIN, USR_LOCAL_BIN };

private static final boolean MAC_OS = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac");

private static final Log logger = LogFactory.getLog(ProcessRunner.class);
Expand Down Expand Up @@ -108,11 +112,22 @@ private Process startProcess(String[] command) {
}
catch (IOException ex) {
String path = processBuilder.environment().get("PATH");
if (MAC_OS && path != null && !path.contains(USR_LOCAL_BIN)
&& !command[0].startsWith(USR_LOCAL_BIN + "/")) {
String[] localCommand = command.clone();
localCommand[0] = USR_LOCAL_BIN + "/" + localCommand[0];
return startProcess(localCommand);
if (MAC_OS && path != null) {
for (String binDirectory : MAC_OS_BIN_DIRECTORIES) {
if (path.contains(binDirectory) || command[0].startsWith(binDirectory + "/")) {
continue;
}
String[] localCommand = command.clone();
localCommand[0] = binDirectory + "/" + command[0];
ProcessBuilder localProcessBuilder = new ProcessBuilder(localCommand);
localProcessBuilder.directory(this.workingDirectory);
try {
return localProcessBuilder.start();
}
catch (IOException suppressed) {
ex.addSuppressed(suppressed);
}
}
}
throw new ProcessStartException(command, ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class DisabledIfProcessUnavailableCondition implements ExecutionCondition {

private static final String USR_LOCAL_BIN = "/usr/local/bin";

private static final String OPT_HOMEBREW_BIN = "/opt/homebrew/bin";

private static final String[] MAC_OS_BIN_DIRECTORIES = { OPT_HOMEBREW_BIN, USR_LOCAL_BIN };

private static final boolean MAC_OS = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac");

@Override
Expand All @@ -68,23 +72,35 @@ private Stream<String[]> getAnnotationValue(AnnotatedElement testElement) {
private void check(String[] command) {
ProcessBuilder processBuilder = new ProcessBuilder(command);
try {
Process process = processBuilder.start();
Assert.state(process.waitFor(30, TimeUnit.SECONDS), "Process did not exit within 30 seconds");
Assert.state(process.exitValue() == 0, () -> "Process exited with %d".formatted(process.exitValue()));
process.destroy();
check(processBuilder);
}
catch (Exception ex) {
String path = processBuilder.environment().get("PATH");
if (MAC_OS && path != null && !path.contains(USR_LOCAL_BIN)
&& !command[0].startsWith(USR_LOCAL_BIN + "/")) {
String[] localCommand = command.clone();
localCommand[0] = USR_LOCAL_BIN + "/" + localCommand[0];
check(localCommand);
return;
if (MAC_OS && path != null) {
for (String binDirectory : MAC_OS_BIN_DIRECTORIES) {
if (path.contains(binDirectory) || command[0].startsWith(binDirectory + "/")) {
continue;
}
String[] localCommand = command.clone();
localCommand[0] = binDirectory + "/" + command[0];
try {
check(new ProcessBuilder(localCommand));
return;
}
catch (Exception ignored) {
}
}
}
throw new RuntimeException(
"Unable to start process '%s'".formatted(StringUtils.arrayToDelimitedString(command, " ")));
}
}

private void check(ProcessBuilder processBuilder) throws Exception {
Process process = processBuilder.start();
Assert.state(process.waitFor(30, TimeUnit.SECONDS), "Process did not exit within 30 seconds");
Assert.state(process.exitValue() == 0, () -> "Process exited with %d".formatted(process.exitValue()));
process.destroy();
}

}
Loading