Run commands via cmd on Windows

This commit is contained in:
Erik C. Thauvin 2025-03-25 13:00:40 -07:00
parent 62b1eeb595
commit b9a694f99a
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -31,11 +31,10 @@ import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatCode;
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
class ExecOperationTest { class ExecOperationTest {
private static final String FOO = "foo"; private static final String FOO = "foo";
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase(Locale.US).contains("win"); private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase(Locale.US).contains("win");
private static final String CAT = IS_WINDOWS ? "type" : "cat";
@Test @Test
void testCommand() { void testCommand() {
@ -55,18 +54,30 @@ class ExecOperationTest {
@Test @Test
void testExitValue() { void testExitValue() {
List<String> cat;
if (IS_WINDOWS) {
cat = List.of("cmd", "/c", "type", FOO);
} else {
cat = List.of("cat", FOO);
}
assertThatCode(() -> assertThatCode(() ->
new ExecOperation() new ExecOperation()
.fromProject(new BaseProject()) .fromProject(new BaseProject())
.command(List.of(CAT, FOO)) .command(cat)
.execute()).isInstanceOf(ExitStatusException.class); .execute()).isInstanceOf(ExitStatusException.class);
} }
@Test @Test
void testFailOnExit() { void testFailOnExit() {
List<String> cat;
if (IS_WINDOWS) {
cat = List.of("cmd", "/c", "type", FOO);
} else {
cat = List.of("cat", FOO);
}
var op = new ExecOperation() var op = new ExecOperation()
.fromProject(new BaseProject()) .fromProject(new BaseProject())
.command(List.of(CAT, FOO)) .command(cat)
.failOnExit(false); .failOnExit(false);
assertThat(op.isFailOnExit()).isFalse(); assertThat(op.isFailOnExit()).isFalse();
assertThatCode(op::execute).doesNotThrowAnyException(); assertThatCode(op::execute).doesNotThrowAnyException();
@ -79,7 +90,7 @@ class ExecOperationTest {
void testTimeout() { void testTimeout() {
List<String> sleep; List<String> sleep;
if (IS_WINDOWS) { if (IS_WINDOWS) {
sleep = List.of("timeout", "/t", "10"); sleep = List.of("cmd", "/c", "timeout", "/t", "10");
} else { } else {
sleep = List.of("sleep", "10"); sleep = List.of("sleep", "10");
} }
@ -107,10 +118,16 @@ class ExecOperationTest {
@Test @Test
void testWorkDir() { void testWorkDir() {
List<String> echo;
if (IS_WINDOWS) {
echo = List.of("cmd", "/c", "echo", FOO);
} else {
echo = List.of("echo", FOO);
}
var workDir = new File(System.getProperty("java.io.tmpdir")); var workDir = new File(System.getProperty("java.io.tmpdir"));
var op = new ExecOperation() var op = new ExecOperation()
.fromProject(new BaseProject()) .fromProject(new BaseProject())
.command("echo", FOO) .command(echo)
.workDir(workDir); .workDir(workDir);
assertThat(op.workDir()).as("as file").isEqualTo(workDir); assertThat(op.workDir()).as("as file").isEqualTo(workDir);
assertThatCode(op::execute).doesNotThrowAnyException(); assertThatCode(op::execute).doesNotThrowAnyException();