Skip to content

Commit 6822659

Browse files
committed
Incorporating copilot comments
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 59bb9a0 commit 6822659

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

impl/test/src/test/java/io/serverlessworkflow/impl/test/junit/BinaryCheckCondition.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,51 @@
1616
package io.serverlessworkflow.impl.test.junit;
1717

1818
import java.io.IOException;
19+
import java.util.concurrent.TimeUnit;
1920
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2021
import org.junit.jupiter.api.extension.ExecutionCondition;
2122
import org.junit.jupiter.api.extension.ExtensionContext;
23+
import org.junit.platform.commons.util.AnnotationUtils;
2224

2325
public class BinaryCheckCondition implements ExecutionCondition {
2426

2527
@Override
2628
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
27-
String[] command =
28-
context
29-
.getElement()
30-
.map(el -> el.getAnnotation(DisabledIfBinaryUnavailable.class).value())
31-
.orElse(null);
32-
if (command == null || command.length == 0) {
33-
throw new IllegalArgumentException("No @DisabledIfBinaryUnavailable annotation is present");
34-
}
35-
36-
if (isBinaryAvailable(command)) {
37-
return ConditionEvaluationResult.enabled(command[0] + " is available on this system.");
38-
} else {
39-
return ConditionEvaluationResult.disabled(
40-
"Test disabled: " + command[0] + " command not found.");
41-
}
29+
return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfBinaryUnavailable.class)
30+
.map(
31+
annotation -> {
32+
String[] binary = annotation.value();
33+
if (binary == null || binary.length == 0) {
34+
return ConditionEvaluationResult.enabled(
35+
"No command found in the annotation @DisabledIfBinaryUnavailable");
36+
}
37+
if (isBinaryAvailable(binary)) {
38+
return ConditionEvaluationResult.enabled(binary[0] + " is available.");
39+
} else {
40+
return ConditionEvaluationResult.disabled(
41+
"Test disabled: " + binary[0] + " not found.");
42+
}
43+
})
44+
.orElse(ConditionEvaluationResult.enabled("No @DisabledIfBinaryUnavailable found."));
4245
}
4346

4447
public boolean isBinaryAvailable(String... command) {
4548
try {
46-
Process process = new ProcessBuilder(command).start();
47-
return process.waitFor() == 0;
49+
Process process =
50+
new ProcessBuilder(command)
51+
.redirectErrorStream(true)
52+
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
53+
.start();
54+
boolean finished = process.waitFor(2, TimeUnit.SECONDS);
55+
if (finished) {
56+
return process.exitValue() == 0;
57+
}
58+
process.destroyForcibly();
59+
return false;
4860
} catch (IOException | InterruptedException e) {
61+
if (e instanceof InterruptedException) {
62+
Thread.currentThread().interrupt();
63+
}
4964
return false;
5065
}
5166
}

0 commit comments

Comments
 (0)