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

repl: Default useGlobal to false in CLI REPL. #5703

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createRepl(env, opts, cb) {
opts = opts || {
ignoreUndefined: false,
terminal: process.stdout.isTTY,
useGlobal: true,
useGlobal: false,
breakEvalOnSigint: true
};

Expand Down
85 changes: 85 additions & 0 deletions test/parallel/test-repl-use-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

// Flags: --expose-internals

const common = require('../common');
const stream = require('stream');
const repl = require('internal/repl');
const assert = require('assert');

common.globalCheck = false;

// Array of [useGlobal, expectedResult] pairs
const globalTestCases = [
[false, 'undefined'],
[true, '\'tacos\''],
[undefined, 'undefined']
];

const globalTest = (useGlobal, cb, output) => (err, repl) => {
if (err)
return cb(err);

let str = '';
output.on('data', (data) => (str += data));
global.lunch = 'tacos';
repl.write('global.lunch;\n');
repl.close();
delete global.lunch;
cb(null, str.trim());
};

// Test how the global object behaves in each state for useGlobal
for (const [option, expected] of globalTestCases) {
runRepl(option, globalTest, common.mustCall((err, output) => {
assert.ifError(err);
assert.strictEqual(output, expected);
}));
}

// Test how shadowing the process object via `let`
// behaves in each useGlobal state. Note: we can't
// actually test the state when useGlobal is true,
// because the exception that's generated is caught
// (see below), but errors are printed, and the test
// suite is aware of it, causing a failure to be flagged.
//
const processTestCases = [false, undefined];
const processTest = (useGlobal, cb, output) => (err, repl) => {
if (err)
return cb(err);

let str = '';
output.on('data', (data) => (str += data));

// if useGlobal is false, then `let process` should work
repl.write('let process;\n');
repl.write('21 * 2;\n');
repl.close();
cb(null, str.trim());
};

for (const option of processTestCases) {
runRepl(option, processTest, common.mustCall((err, output) => {
assert.ifError(err);
assert.strictEqual(output, 'undefined\n42');
}));
}

function runRepl(useGlobal, testFunc, cb) {
const inputStream = new stream.PassThrough();
const outputStream = new stream.PassThrough();
const opts = {
input: inputStream,
output: outputStream,
useGlobal: useGlobal,
useColors: false,
terminal: false,
prompt: ''
};

repl.createInternalRepl(
process.env,
opts,
testFunc(useGlobal, cb, opts.output));
}