test,repl: add coverage for repl .clear+useGlobal
Add a test to cover situation where REPL is initialized with `useGlobal` set to `true` and `.clear` is called. This adds coverage for code in repl.js that is not currently covered. Includes minor refactor of rocket functions in repl.js for concision. PR-URL: https://github.com/nodejs/node/pull/10777 Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
34bf31ea8a
commit
338d09d25b
11
lib/repl.js
11
lib/repl.js
@ -721,9 +721,7 @@ REPLServer.prototype.createContext = function() {
|
|||||||
|
|
||||||
Object.defineProperty(context, '_', {
|
Object.defineProperty(context, '_', {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
get: () => {
|
get: () => this.last,
|
||||||
return this.last;
|
|
||||||
},
|
|
||||||
set: (value) => {
|
set: (value) => {
|
||||||
this.last = value;
|
this.last = value;
|
||||||
if (!this.underscoreAssigned) {
|
if (!this.underscoreAssigned) {
|
||||||
@ -1266,9 +1264,10 @@ function defineDefaultCommands(repl) {
|
|||||||
help: 'Print this help message',
|
help: 'Print this help message',
|
||||||
action: function() {
|
action: function() {
|
||||||
const names = Object.keys(this.commands).sort();
|
const names = Object.keys(this.commands).sort();
|
||||||
const longestNameLength = names.reduce((max, name) => {
|
const longestNameLength = names.reduce(
|
||||||
return Math.max(max, name.length);
|
(max, name) => Math.max(max, name.length),
|
||||||
}, 0);
|
0
|
||||||
|
);
|
||||||
names.forEach((name) => {
|
names.forEach((name) => {
|
||||||
const cmd = this.commands[name];
|
const cmd = this.commands[name];
|
||||||
const spaces = ' '.repeat(longestNameLength - name.length + 3);
|
const spaces = ' '.repeat(longestNameLength - name.length + 3);
|
||||||
|
@ -8,6 +8,7 @@ const stream = require('stream');
|
|||||||
testSloppyMode();
|
testSloppyMode();
|
||||||
testStrictMode();
|
testStrictMode();
|
||||||
testResetContext();
|
testResetContext();
|
||||||
|
testResetContextGlobal();
|
||||||
testMagicMode();
|
testMagicMode();
|
||||||
|
|
||||||
function testSloppyMode() {
|
function testSloppyMode() {
|
||||||
@ -131,7 +132,28 @@ function testResetContext() {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initRepl(mode) {
|
function testResetContextGlobal() {
|
||||||
|
const r = initRepl(repl.REPL_MODE_STRICT, true);
|
||||||
|
|
||||||
|
r.write(`_ = 10; // explicitly set to 10
|
||||||
|
_; // 10 from user input
|
||||||
|
.clear // No output because useGlobal is true
|
||||||
|
_; // remains 10
|
||||||
|
`);
|
||||||
|
|
||||||
|
assertOutput(r.output, [
|
||||||
|
'Expression assignment to _ now disabled.',
|
||||||
|
'10',
|
||||||
|
'10',
|
||||||
|
'10',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// delete globals leaked by REPL when `useGlobal` is `true`
|
||||||
|
delete global.module;
|
||||||
|
delete global.require;
|
||||||
|
}
|
||||||
|
|
||||||
|
function initRepl(mode, useGlobal) {
|
||||||
const inputStream = new stream.PassThrough();
|
const inputStream = new stream.PassThrough();
|
||||||
const outputStream = new stream.PassThrough();
|
const outputStream = new stream.PassThrough();
|
||||||
outputStream.accum = '';
|
outputStream.accum = '';
|
||||||
@ -146,7 +168,8 @@ function initRepl(mode) {
|
|||||||
useColors: false,
|
useColors: false,
|
||||||
terminal: false,
|
terminal: false,
|
||||||
prompt: '',
|
prompt: '',
|
||||||
replMode: mode
|
replMode: mode,
|
||||||
|
useGlobal: useGlobal
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user