-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor redirect streams to take OutputStreams
- Loading branch information
Showing
5 changed files
with
113 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package jep.test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import jep.Interpreter; | ||
import jep.JepConfig; | ||
import jep.SubInterpreter; | ||
|
||
/** | ||
* Created: July 2021 | ||
* | ||
* @author Nate Jensen | ||
* @since 4.0 | ||
*/ | ||
public class TestRedirectStreams { | ||
|
||
public static void main(String[] args) throws Exception { | ||
ByteArrayOutputStream stdout = new ByteArrayOutputStream(); | ||
ByteArrayOutputStream stderr = new ByteArrayOutputStream(); | ||
|
||
try (Interpreter interp = new SubInterpreter(new JepConfig() | ||
.redirectStdout(stdout).redirectStdErr(stderr))) { | ||
String testString = "This string came from Python's print function."; | ||
interp.set("testString", testString); | ||
interp.exec("print(testString)"); | ||
String stdoutOutput = new String(stdout.toByteArray(), | ||
StandardCharsets.UTF_8); | ||
// Python print function will add a \n on the end | ||
testString += "\n"; | ||
if (!testString.equals(stdoutOutput)) { | ||
throw new IllegalStateException( | ||
"testString did not match stdoutOutput! testString=" | ||
+ testString + " stdoutOutput=" + stdoutOutput); | ||
} | ||
|
||
String line1 = "stderrLine1"; | ||
String line2 = "stderrLine2"; | ||
interp.set("line1", line1); | ||
interp.set("line2", line2); | ||
interp.exec("import sys"); | ||
interp.exec("print(line1, file=sys.stderr)"); | ||
interp.exec("sys.stderr.write(line2)"); | ||
String stderrOutput = new String(stderr.toByteArray(), | ||
StandardCharsets.UTF_8); | ||
String combined = line1 + "\n" + line2; | ||
if (!combined.equals(stderrOutput)) { | ||
throw new IllegalStateException( | ||
"testString did not match stderrOutput! testString=" | ||
+ combined + " stderrOutput=" + stderrOutput); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import unittest | ||
|
||
from jep_pipe import jep_pipe | ||
from jep_pipe import build_java_process_cmd | ||
|
||
class TestRedirectStreams(unittest.TestCase): | ||
|
||
def test_compiledScript(self): | ||
jep_pipe(build_java_process_cmd('jep.test.TestRedirectStreams')) | ||
|