From a8e0781ec579ad8ed4fef875739c1a7a482b7402 Mon Sep 17 00:00:00 2001 From: Alex Gorbatchev Date: Wed, 1 Jun 2016 10:18:48 -0700 Subject: [PATCH] Fixes Node.js compilers writing to STDOUT and/or STDERR during compilation --- lib/execjs/support/node_runner.js | 9 +++++++++ test/test_execjs.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/execjs/support/node_runner.js b/lib/execjs/support/node_runner.js index ab0c6ec..d3e3551 100644 --- a/lib/execjs/support/node_runner.js +++ b/lib/execjs/support/node_runner.js @@ -4,7 +4,16 @@ process.stdout.write('' + string); }; try { + var stdoutWrite = process.stdout.write; + var stderrWrite = process.stderr.write; + + process.stdout.write = process.stderr.write = function () {} + result = program(); + + process.stdout.write = stdoutWrite; + process.stderr.write = stderrWrite; + if (typeof result == 'undefined' && result !== null) { print('["ok"]'); } else { diff --git a/test/test_execjs.rb b/test/test_execjs.rb index 7fd967f..562de9c 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -376,4 +376,20 @@ def test_uglify assert_equal "function foo(bar){return bar}", context.call("uglify", "function foo(bar) {\n return bar;\n}") end + + def test_node_runtime_with_compiler_writing_to_stdio + skip if not ExecJS.runtime.name =~ /Node/ + + source = <<-JS + process.stdout.write('WARNING'); + process.stderr.write('ERROR'); + + function compile(code) { + return code; + } + JS + + context = ExecJS.compile(source) + assert_equal "foo()", context.call("compile", "foo()") + end end