Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This PR fixes the issue of flush() #1358

Merged
merged 9 commits into from
Jul 25, 2023
2 changes: 2 additions & 0 deletions src/org/mozilla/javascript/engine/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public static void print(Context cx, Scriptable thisObj, Object[] args, Function
self.stdout.write(ScriptRuntime.toString(arg));
}
self.stdout.write('\n');
// ref bug https://github.com/mozilla/rhino/issues/1356
self.stdout.flush();
}

private static Builtins getSelf(Scriptable scope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringWriter;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.*;
import org.mozilla.javascript.engine.RhinoScriptEngineFactory;

public class BuiltinsTest {
Expand All @@ -35,6 +35,27 @@ public void printStdout() throws ScriptException {
engine.eval("print('Hello, World!');");
}

@Test
public void printStdoutAndCheckItPrints() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream original = System.out;
PrintStream modified = new PrintStream(bos, false);
System.setOut(modified);
// Now Get A SimpleContext
ScriptContext sc = new SimpleScriptContext();
try {
// this was a broken test
engine.eval("print('Hello, World!');", sc);
// this has been hard work https://github.com/mozilla/rhino/issues/1356
Assert.assertEquals("Hello, World!\n", bos.toString());
} finally {
// revert the sys out
System.setOut(original);
modified.close();
bos.close();
}
}

@Test
public void printWriter() throws ScriptException {
StringWriter sw = new StringWriter();
Expand Down