From 1c59cefc44709159343ac6ef76c9a09603934be9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 20 Oct 2016 16:47:33 +0200 Subject: [PATCH] repl: make `key` of `repl.write()` optional always MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In `.editor` mode, `repl.write()` would have crashed when the `key` argument was not present, because the overwritten `_ttyWrite` of REPLs doesn’t check for the absence of a second argument like `readline.write()` does. Since the docs indicate that the argument is optional, add a check paralleling the one in `readline.write()`. PR-URL: https://github.com/nodejs/node/pull/9207 Reviewed-By: Prince John Wesley Reviewed-By: James M Snell --- lib/repl.js | 1 + test/parallel/test-repl-.editor.js | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 470b42a0b7a94f..399bcba979660d 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -601,6 +601,7 @@ function REPLServer(prompt, // Wrap readline tty to enable editor mode const ttyWrite = self._ttyWrite.bind(self); self._ttyWrite = (d, key) => { + key = key || {}; if (!self.editorMode || !self.terminal) { ttyWrite(d, key); return; diff --git a/test/parallel/test-repl-.editor.js b/test/parallel/test-repl-.editor.js index 556662181f7831..4077840c596376 100644 --- a/test/parallel/test-repl-.editor.js +++ b/test/parallel/test-repl-.editor.js @@ -8,16 +8,17 @@ const repl = require('repl'); // \u001b[0J - Clear screen // \u001b[3G - Moves the cursor to 3rd column const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G'; +const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g'); -function run({input, output, event}) { +function run({input, output, event, checkTerminalCodes = true}) { const stream = new common.ArrayStream(); let found = ''; stream.write = (msg) => found += msg.replace('\r', ''); - const expected = `${terminalCode}.editor\n` + - '// Entering editor mode (^D to finish, ^C to cancel)\n' + - `${input}${output}\n${terminalCode}`; + let expected = `${terminalCode}.editor\n` + + '// Entering editor mode (^D to finish, ^C to cancel)\n' + + `${input}${output}\n${terminalCode}`; const replServer = repl.start({ prompt: '> ', @@ -31,6 +32,12 @@ function run({input, output, event}) { stream.emit('data', input); replServer.write('', event); replServer.close(); + + if (!checkTerminalCodes) { + found = found.replace(terminalCodeRegex, '').replace(/\n/g, ''); + expected = expected.replace(terminalCodeRegex, '').replace(/\n/g, ''); + } + assert.strictEqual(found, expected); } @@ -54,6 +61,12 @@ const tests = [ input: ' var i = 1;\ni + 3', output: '\n4', event: {ctrl: true, name: 'd'} + }, + { + input: '', + output: '', + checkTerminalCodes: false, + event: null, } ];