11package io .github .easy4j .opencli .core ;
22
33import io .github .easy4j .opencli .OpenCliProperties ;
4+ import io .github .easy4j .opencli .contract .LifecycleProbe ;
45import io .github .easy4j .opencli .exception .OpenCliException ;
56import io .github .easy4j .opencli .exception .OpenCliNonZeroExitException ;
67import io .github .easy4j .opencli .exception .OpenCliTimeoutException ;
78import java .io .ByteArrayOutputStream ;
9+ import java .io .File ;
810import java .io .PrintStream ;
911import java .nio .charset .StandardCharsets ;
1012import java .nio .file .Path ;
13+ import java .util .ArrayList ;
14+ import java .util .Arrays ;
1115import java .util .UUID ;
1216import org .junit .jupiter .api .Test ;
13- import org .junit .jupiter .api .condition .EnabledOnOs ;
14- import org .junit .jupiter .api .condition .OS ;
1517import org .junit .jupiter .api .function .Executable ;
1618import org .junit .jupiter .api .io .TempDir ;
1719
1820import static org .junit .jupiter .api .Assertions .assertEquals ;
1921import static org .junit .jupiter .api .Assertions .assertFalse ;
2022import static org .junit .jupiter .api .Assertions .assertThrows ;
23+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2124
2225class OpenCliProcessPrivacyTest {
2326
@@ -33,34 +36,54 @@ void keepsMissingExecutablePathOutOfLogsAndStackTrace(@TempDir Path directory) t
3336 }
3437
3538 @ Test
36- @ EnabledOnOs ({OS .LINUX , OS .MAC })
3739 void preservesStderrOnNonzeroExitWithoutLeakingArgsOrStderr () throws Exception {
3840 String canary = "stderr-" + UUID .randomUUID ();
39- OpenCliProperties properties = new OpenCliProperties ();
40- properties .setExecutable ("/bin/sh" );
41-
42- try (OpenCliExecutor executor = new OpenCliExecutor (properties )) {
41+ try (OpenCliExecutor executor = new OpenCliExecutor (childProperties ())) {
4342 OpenCliNonZeroExitException failure = assertPrivateFailure (OpenCliNonZeroExitException .class , canary ,
44- () -> executor .invoke ("-c " , "printf '%s' \" $1 \" >&2; exit 7" , "fixture " , canary ));
43+ () -> executor .invoke ("privacy " , "nonzero " , canary ));
4544 assertEquals (canary , failure .getPartialResult ().getStderr ());
4645 assertEquals (Integer .valueOf (7 ), failure .getPartialResult ().getExitCode ());
4746 }
4847 }
4948
5049 @ Test
51- @ EnabledOnOs ({OS .LINUX , OS .MAC })
52- void keepsArgumentsOutOfTimeoutDiagnostics () throws Exception {
50+ void keepsArgumentsAndPartialStderrOutOfTimeoutDiagnostics () throws Exception {
5351 String canary = "timeout-" + UUID .randomUUID ();
54- OpenCliProperties properties = new OpenCliProperties ();
55- properties .setExecutable ("/bin/sh" );
56- properties .setCommandTimeoutMillis (200 );
52+ OpenCliProperties properties = childProperties ();
53+ properties .setCommandTimeoutMillis (2500 );
54+
55+ try (OpenCliExecutor executor = new OpenCliExecutor (properties )) {
56+ OpenCliTimeoutException failure = assertPrivateFailure (OpenCliTimeoutException .class , canary ,
57+ () -> executor .invoke ("privacy" , "timeout" , canary ));
58+ assertEquals (canary , failure .getPartialResult ().getStderr ());
59+ }
60+ }
61+
62+ @ Test
63+ void keepsPartialStdoutOutOfOutputLimitDiagnostics () throws Exception {
64+ String canary = "overflow-" + UUID .randomUUID ();
65+ OpenCliProperties properties = childProperties ();
66+ properties .setMaxStdoutBytes (128 );
5767
5868 try (OpenCliExecutor executor = new OpenCliExecutor (properties )) {
59- assertPrivateFailure (OpenCliTimeoutException .class , canary ,
60- () -> executor .invoke ("-c" , "exec sleep 5" , "fixture" , canary ));
69+ OpenCliException failure = assertPrivateFailure (OpenCliException .class , canary ,
70+ () -> executor .invoke ("privacy" , "overflow" , canary ));
71+ assertEquals ("OUTPUT_LIMIT" , failure .getPartialResult ().getExecutionDetails ().getTerminationReason ().name ());
72+ assertEquals (128 , failure .getPartialResult ().getStdout ().length ());
73+ assertTrue (failure .getPartialResult ().getStdout ().startsWith (canary ));
6174 }
6275 }
6376
77+ private static OpenCliProperties childProperties () {
78+ OpenCliProperties properties = new OpenCliProperties ();
79+ String executable = System .getProperty ("os.name" ).startsWith ("Windows" ) ? "java.exe" : "java" ;
80+ properties .setExecutable (new File (new File (System .getProperty ("java.home" ), "bin" ), executable ).getAbsolutePath ());
81+ properties .setLeadingArguments (new ArrayList <>(Arrays .asList ("-cp" ,
82+ System .getProperty ("surefire.test.class.path" , System .getProperty ("java.class.path" )),
83+ LifecycleProbe .class .getName ())));
84+ return properties ;
85+ }
86+
6487 private static <T extends OpenCliException > T assertPrivateFailure (
6588 Class <T > type , String canary , Executable invocation ) throws Exception {
6689 ByteArrayOutputStream output = new ByteArrayOutputStream ();
0 commit comments