From 109ab9b6324c2acb8b8d94437061e01cd94db325 Mon Sep 17 00:00:00 2001 From: noga Date: Sat, 22 Jul 2023 12:30:34 +0530 Subject: [PATCH 1/8] Breaking the build by adding proper test for the print() statement. --- .../tests/scriptengine/BuiltinsTest.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index 5b60701c8f..2840b67e94 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -2,12 +2,18 @@ import static org.junit.Assert.*; +import java.io.PrintStream; import java.io.StringWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; + +import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -31,8 +37,24 @@ public void setup() { } @Test - public void printStdout() throws ScriptException { - engine.eval("print('Hello, World!');"); + public void printStdout() throws Exception { + // only way to test these are redirections and then prayers + // this was not true earlier + final String content = "Hello, World!"; + final String tmpFile = "out.txt" ; + final Path filePath = Paths.get(tmpFile); + if ( Files.exists( filePath )){ + Files.delete(filePath); + } + PrintStream o = new PrintStream(tmpFile); + PrintStream console = System.out; + System.setOut(o); + engine.eval("print('" + content + "');"); + System.setOut(console); + Assert.assertTrue( Files.exists(filePath)); + // now read the content of the file ? + String writtenContent = new String(Files. readAllBytes(filePath)); + Assert.assertEquals( content + "\n", writtenContent); } @Test From 894268cfc17cfd4328846c6aed4a51e5ff68c74c Mon Sep 17 00:00:00 2001 From: noga Date: Sat, 22 Jul 2023 15:05:05 +0530 Subject: [PATCH 2/8] Revert "Breaking the build by adding proper test for the print() statement." This reverts commit 109ab9b6324c2acb8b8d94437061e01cd94db325. --- .../tests/scriptengine/BuiltinsTest.java | 26 ++----------------- 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index 2840b67e94..5b60701c8f 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -2,18 +2,12 @@ import static org.junit.Assert.*; -import java.io.PrintStream; import java.io.StringWriter; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; - -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -37,24 +31,8 @@ public void setup() { } @Test - public void printStdout() throws Exception { - // only way to test these are redirections and then prayers - // this was not true earlier - final String content = "Hello, World!"; - final String tmpFile = "out.txt" ; - final Path filePath = Paths.get(tmpFile); - if ( Files.exists( filePath )){ - Files.delete(filePath); - } - PrintStream o = new PrintStream(tmpFile); - PrintStream console = System.out; - System.setOut(o); - engine.eval("print('" + content + "');"); - System.setOut(console); - Assert.assertTrue( Files.exists(filePath)); - // now read the content of the file ? - String writtenContent = new String(Files. readAllBytes(filePath)); - Assert.assertEquals( content + "\n", writtenContent); + public void printStdout() throws ScriptException { + engine.eval("print('Hello, World!');"); } @Test From c41c329eef7d76036f58fc949bb565f8dfe2fba1 Mon Sep 17 00:00:00 2001 From: noga Date: Sat, 22 Jul 2023 19:21:43 +0530 Subject: [PATCH 3/8] Breaking the build by adding proper test for the print() statement. --- .../tests/scriptengine/BuiltinsTest.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index 5b60701c8f..d01ddf7a6b 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -2,19 +2,28 @@ 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 { + static ByteArrayOutputStream bos = new ByteArrayOutputStream(); + static PrintStream original = System.out; + static PrintStream modified = new PrintStream(bos,false); + static { + // because script engine gets loaded before... hence + System.setOut(modified); + } + private static ScriptEngineManager manager; private ScriptEngine engine; @@ -25,6 +34,13 @@ public static void init() { manager.registerEngineName("rhino", new RhinoScriptEngineFactory()); } + @AfterClass + public static void revert() throws Exception{ + System.setOut(original); + modified.close(); + bos.close(); + } + @Before public void setup() { engine = manager.getEngineByName("rhino"); @@ -33,6 +49,7 @@ public void setup() { @Test public void printStdout() throws ScriptException { engine.eval("print('Hello, World!');"); + Assert.assertEquals( "Hello, World!\n", bos.toString() ); } @Test From 05018cdf8bf2dfbc8e08f0eef042887a212c82fc Mon Sep 17 00:00:00 2001 From: noga Date: Sat, 22 Jul 2023 19:24:22 +0530 Subject: [PATCH 4/8] Fixing the issue https://github.com/mozilla/rhino/issues/1356 --- src/org/mozilla/javascript/engine/Builtins.java | 2 ++ .../org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/org/mozilla/javascript/engine/Builtins.java b/src/org/mozilla/javascript/engine/Builtins.java index f29e0e6aa6..b3726dd4ec 100644 --- a/src/org/mozilla/javascript/engine/Builtins.java +++ b/src/org/mozilla/javascript/engine/Builtins.java @@ -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) { diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index d01ddf7a6b..62b01830f3 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -48,7 +48,9 @@ public void setup() { @Test public void printStdout() throws ScriptException { + // this was a broken test engine.eval("print('Hello, World!');"); + // this has been hard work https://github.com/mozilla/rhino/issues/1356 Assert.assertEquals( "Hello, World!\n", bos.toString() ); } From 249160041daa9c2c46802913b68b0df4d35a2213 Mon Sep 17 00:00:00 2001 From: noga Date: Sun, 23 Jul 2023 18:37:05 +0530 Subject: [PATCH 5/8] Much localized fix. --- .../mozilla/javascript/engine/Builtins.java | 2 +- .../tests/scriptengine/BuiltinsTest.java | 38 +++++++++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/org/mozilla/javascript/engine/Builtins.java b/src/org/mozilla/javascript/engine/Builtins.java index b3726dd4ec..707fac20e1 100644 --- a/src/org/mozilla/javascript/engine/Builtins.java +++ b/src/org/mozilla/javascript/engine/Builtins.java @@ -48,7 +48,7 @@ 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 + //ref bug https://github.com/mozilla/rhino/issues/1356 self.stdout.flush(); } diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index 62b01830f3..ee5fc4b6f9 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -16,14 +16,6 @@ public class BuiltinsTest { - static ByteArrayOutputStream bos = new ByteArrayOutputStream(); - static PrintStream original = System.out; - static PrintStream modified = new PrintStream(bos,false); - static { - // because script engine gets loaded before... hence - System.setOut(modified); - } - private static ScriptEngineManager manager; private ScriptEngine engine; @@ -34,24 +26,30 @@ public static void init() { manager.registerEngineName("rhino", new RhinoScriptEngineFactory()); } - @AfterClass - public static void revert() throws Exception{ - System.setOut(original); - modified.close(); - bos.close(); - } - @Before public void setup() { engine = manager.getEngineByName("rhino"); } @Test - public void printStdout() throws ScriptException { - // this was a broken test - engine.eval("print('Hello, World!');"); - // this has been hard work https://github.com/mozilla/rhino/issues/1356 - Assert.assertEquals( "Hello, World!\n", bos.toString() ); + public void printStdout() 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 From b2b830731ec089d6979468b6dd140ef41371c342 Mon Sep 17 00:00:00 2001 From: noga Date: Sun, 23 Jul 2023 18:37:05 +0530 Subject: [PATCH 6/8] Much localized fix. --- .../mozilla/javascript/engine/Builtins.java | 2 ++ .../tests/scriptengine/BuiltinsTest.java | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/org/mozilla/javascript/engine/Builtins.java b/src/org/mozilla/javascript/engine/Builtins.java index f29e0e6aa6..707fac20e1 100644 --- a/src/org/mozilla/javascript/engine/Builtins.java +++ b/src/org/mozilla/javascript/engine/Builtins.java @@ -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) { diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index 5b60701c8f..0314419e8f 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -31,8 +31,24 @@ public void setup() { } @Test - public void printStdout() throws ScriptException { - engine.eval("print('Hello, World!');"); + public void printStdout() 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 From 968fdbbdd4ac094040b5ac37abdeb04732e6ad83 Mon Sep 17 00:00:00 2001 From: noga Date: Sun, 23 Jul 2023 19:08:24 +0530 Subject: [PATCH 7/8] Fixing spotless issues. --- src/org/mozilla/javascript/engine/Builtins.java | 2 +- .../mozilla/javascript/tests/scriptengine/BuiltinsTest.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/org/mozilla/javascript/engine/Builtins.java b/src/org/mozilla/javascript/engine/Builtins.java index 707fac20e1..b3726dd4ec 100644 --- a/src/org/mozilla/javascript/engine/Builtins.java +++ b/src/org/mozilla/javascript/engine/Builtins.java @@ -48,7 +48,7 @@ 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 + // ref bug https://github.com/mozilla/rhino/issues/1356 self.stdout.flush(); } diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index ee5fc4b6f9..e7b6772614 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -10,7 +10,6 @@ import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; - import org.junit.*; import org.mozilla.javascript.engine.RhinoScriptEngineFactory; @@ -35,13 +34,13 @@ public void setup() { public void printStdout() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream original = System.out; - PrintStream modified = new PrintStream(bos,false); + 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 ); + 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 { From 5286d414c7801316b7e75b3b2add1974331765d2 Mon Sep 17 00:00:00 2001 From: noga Date: Mon, 24 Jul 2023 21:39:59 +0530 Subject: [PATCH 8/8] Updating to bring back earlier test and add our test as a new one, as suggested. --- .../javascript/tests/scriptengine/BuiltinsTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java index e7b6772614..a2bc1a0706 100644 --- a/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java +++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java @@ -31,7 +31,12 @@ public void setup() { } @Test - public void printStdout() throws Exception { + 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);