88
99public class ProcessRunnerTest {
1010
11- private static boolean isWindows () {
12- return System .getProperty ("os.name" ).toLowerCase ().contains ("win" );
11+ private static String [] shellCommand (String script ) {
12+ if (System .getProperty ("os.name" ).toLowerCase ().contains ("win" )) {
13+ return new String [] {"cmd.exe" , "/c" , script };
14+ } else {
15+ return new String [] {"sh" , "-c" , script };
16+ }
1317 }
1418
1519 @ Test
1620 void runWithOutput_returnsCommandOutput () {
17- String output ;
18- if (isWindows ()) {
19- output = ProcessRunner .runWithOutput ("cmd.exe" , "/c" , "echo hello" );
20- } else {
21- output = ProcessRunner .runWithOutput ("sh" , "-c" , "echo hello" );
22- }
21+ String output = ProcessRunner .runWithOutput (shellCommand ("echo hello" ));
2322
2423 assertNotNull (output , "Output should not be null" );
2524 assertFalse (output .isEmpty (), "Output should not be empty" );
@@ -28,25 +27,16 @@ void runWithOutput_returnsCommandOutput() {
2827
2928 @ Test
3029 void runWithOutput_nonZeroExitDoesNotThrow () {
31- String output ;
32- if (isWindows ()) {
33- output = assertDoesNotThrow (() -> ProcessRunner .runWithOutput ("cmd.exe" , "/c" , "echo output && exit /b 1" ));
34- } else {
35- output = assertDoesNotThrow (() -> ProcessRunner .runWithOutput ("sh" , "-c" , "echo output; exit 1" ));
36- }
30+ String [] command = shellCommand ("echo output; exit 1" );
31+ String output = assertDoesNotThrow (() -> ProcessRunner .runWithOutput (command ));
3732
3833 assertNotNull (output , "Output should not be null" );
3934 assertTrue (output .contains ("output" ), "Output should contain 'output'" );
4035 }
4136
4237 @ Test
4338 void runWithOutput_capturesStderrViaMergedStream () {
44- String output ;
45- if (isWindows ()) {
46- output = ProcessRunner .runWithOutput ("cmd.exe" , "/c" , "echo stdout && echo stderr 1>&2" );
47- } else {
48- output = ProcessRunner .runWithOutput ("sh" , "-c" , "echo stdout && echo stderr 1>&2" );
49- }
39+ String output = ProcessRunner .runWithOutput (shellCommand ("echo stdout && echo stderr 1>&2" ));
5040
5141 assertNotNull (output , "Output should not be null" );
5242 assertFalse (output .isEmpty (), "Output should not be empty" );
0 commit comments