From 6e6cf367611c265343ad489e48dc869cc9090f96 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 19 Jul 2016 14:41:47 -0400 Subject: [PATCH] repl: default useGlobal to true This is a partial revert of 15157c3c3d7594cefb7f5941cbe925657e7d88bd. This change lead to a regression that broke require() in the CLI REPL, as imported files were evaluated in a different context. Refs: https://github.com/nodejs/node/pull/5703 Fixes: https://github.com/nodejs/node/issues/7788 PR-URL: https://github.com/nodejs/node/pull/7795 Reviewed-By: Anna Henningsen Reviewed-By: Minwoo Jung --- lib/internal/repl.js | 2 +- test/parallel/test-repl-require-context.js | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-repl-require-context.js diff --git a/lib/internal/repl.js b/lib/internal/repl.js index d59b40eb763bdb..dd14f42fa5273c 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -22,7 +22,7 @@ function createRepl(env, opts, cb) { opts = opts || { ignoreUndefined: false, terminal: process.stdout.isTTY, - useGlobal: false, + useGlobal: true, breakEvalOnSigint: true }; diff --git a/test/parallel/test-repl-require-context.js b/test/parallel/test-repl-require-context.js new file mode 100644 index 00000000000000..798d6f2db30f49 --- /dev/null +++ b/test/parallel/test-repl-require-context.js @@ -0,0 +1,28 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cp = require('child_process'); +const path = require('path'); +const child = cp.spawn(process.execPath, ['--interactive']); +const fixture = path.join(common.fixturesDir, 'is-object.js').replace(/\\/g, + '/'); +let output = ''; + +child.stdout.setEncoding('utf8'); +child.stdout.on('data', (data) => { + output += data; +}); + +child.on('exit', common.mustCall(() => { + const results = output.split('\n').map((line) => { + return line.replace(/\w*>\w*/, '').trim(); + }); + + assert.deepStrictEqual(results, ['undefined', 'true', 'true', '']); +})); + +child.stdin.write('const isObject = (obj) => obj.constructor === Object;\n'); +child.stdin.write('isObject({});\n'); +child.stdin.write(`require('${fixture}').isObject({});\n`); +child.stdin.write('.exit'); +child.stdin.end();