From 98af1704ae587a8f8ec79e5e53379640bc0b255a Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 21 Aug 2018 14:47:47 -0700 Subject: [PATCH] test: move common.ArrayStream to separate module In a continuing effort to de-monolithize `require('../common')`, move `common.ArrayStream` out to a separate module that is imported only when it is needed. PR-URL: https://github.com/nodejs/node/pull/22447 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Trivikram Kamat Reviewed-By: Refael Ackermann Reviewed-By: Luigi Pinca --- test/common/README.md | 17 ++++++++++--- test/common/arraystream.js | 24 +++++++++++++++++++ test/common/index.js | 18 -------------- test/parallel/test-repl-autolibs.js | 3 ++- test/parallel/test-repl-context.js | 5 ++-- test/parallel/test-repl-domain.js | 5 ++-- test/parallel/test-repl-editor.js | 9 +++---- test/parallel/test-repl-end-emits-exit.js | 5 ++-- test/parallel/test-repl-eval-scope.js | 3 ++- test/parallel/test-repl-inspector.js | 3 ++- test/parallel/test-repl-let-process.js | 5 ++-- test/parallel/test-repl-load-multiline.js | 7 +++--- test/parallel/test-repl-multiline.js | 5 ++-- test/parallel/test-repl-options.js | 3 ++- .../parallel/test-repl-pretty-custom-stack.js | 7 +++--- test/parallel/test-repl-pretty-stack.js | 7 +++--- test/parallel/test-repl-recoverable.js | 5 ++-- test/parallel/test-repl-reset-event.js | 4 ++-- test/parallel/test-repl-save-load.js | 7 +++--- test/parallel/test-repl-syntax-error-stack.js | 5 ++-- test/parallel/test-repl-tab-complete-crash.js | 5 ++-- .../test-repl-tab-complete-no-warn.js | 5 ++-- test/parallel/test-repl-tab-complete.js | 7 +++--- test/parallel/test-repl-top-level-await.js | 5 ++-- 24 files changed, 103 insertions(+), 66 deletions(-) create mode 100644 test/common/arraystream.js diff --git a/test/common/README.md b/test/common/README.md index 27634c97e11e17..e44dcd299c4caa 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -38,9 +38,6 @@ tasks. Takes `whitelist` and concats that with predefined `knownGlobals`. -### arrayStream -A stream to push an array into a REPL - ### busyLoop(time) * `time` [<number>] @@ -413,6 +410,20 @@ Platform normalizes the `pwd` command. Synchronous version of `spawnPwd`. +## ArrayStream Module + +The `ArrayStream` module provides a simple `Stream` that pushes elements from +a given array. + + +```js +const ArrayStream = require('../common/arraystream'); +const stream = new ArrayStream(); +stream.run(['a', 'b', 'c']); +``` + +It can be used within tests as a simple mock stream. + ## Countdown Module The `Countdown` module provides a simple countdown mechanism for tests that diff --git a/test/common/arraystream.js b/test/common/arraystream.js new file mode 100644 index 00000000000000..dadd6ff0481c60 --- /dev/null +++ b/test/common/arraystream.js @@ -0,0 +1,24 @@ +/* eslint-disable node-core/required-modules */ +'use strict'; + +const { Stream } = require('stream'); +const { inherits } = require('util'); +function noop() {} + +// A stream to push an array into a REPL +function ArrayStream() { + this.run = function(data) { + data.forEach((line) => { + this.emit('data', `${line}\n`); + }); + }; +} + +inherits(ArrayStream, Stream); +ArrayStream.prototype.readable = true; +ArrayStream.prototype.writable = true; +ArrayStream.prototype.pause = noop; +ArrayStream.prototype.resume = noop; +ArrayStream.prototype.write = noop; + +module.exports = ArrayStream; diff --git a/test/common/index.js b/test/common/index.js index c393eac9c3a1f1..696d9201226ed9 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -27,7 +27,6 @@ const fs = require('fs'); const assert = require('assert'); const os = require('os'); const { exec, execSync, spawn, spawnSync } = require('child_process'); -const stream = require('stream'); const util = require('util'); const Timer = process.binding('timer_wrap').Timer; const { fixturesDir } = require('./fixtures'); @@ -512,23 +511,6 @@ exports.skip = function(msg) { process.exit(0); }; -// A stream to push an array into a REPL -function ArrayStream() { - this.run = function(data) { - data.forEach((line) => { - this.emit('data', `${line}\n`); - }); - }; -} - -util.inherits(ArrayStream, stream.Stream); -exports.ArrayStream = ArrayStream; -ArrayStream.prototype.readable = true; -ArrayStream.prototype.writable = true; -ArrayStream.prototype.pause = noop; -ArrayStream.prototype.resume = noop; -ArrayStream.prototype.write = noop; - // Returns true if the exit code "exitCode" and/or signal name "signal" // represent the exit code and/or signal name of a node process that aborted, // false otherwise. diff --git a/test/parallel/test-repl-autolibs.js b/test/parallel/test-repl-autolibs.js index 024dd971bf44e1..f069d8bf409799 100644 --- a/test/parallel/test-repl-autolibs.js +++ b/test/parallel/test-repl-autolibs.js @@ -21,11 +21,12 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const util = require('util'); const repl = require('repl'); -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); repl.start('', putIn, null, true); test1(); diff --git a/test/parallel/test-repl-context.js b/test/parallel/test-repl-context.js index 914aa563bd50fb..0394129d45df19 100644 --- a/test/parallel/test-repl-context.js +++ b/test/parallel/test-repl-context.js @@ -1,11 +1,12 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); const vm = require('vm'); // Create a dummy stream that does nothing. -const stream = new common.ArrayStream(); +const stream = new ArrayStream(); // Test context when useGlobal is false. { diff --git a/test/parallel/test-repl-domain.js b/test/parallel/test-repl-domain.js index ff36eeaf3a4af4..ce6da4bedb9e28 100644 --- a/test/parallel/test-repl-domain.js +++ b/test/parallel/test-repl-domain.js @@ -20,11 +20,12 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const repl = require('repl'); -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); repl.start('', putIn); putIn.write = function(data) { diff --git a/test/parallel/test-repl-editor.js b/test/parallel/test-repl-editor.js index 091eaec1ab41be..8db7789f029a10 100644 --- a/test/parallel/test-repl-editor.js +++ b/test/parallel/test-repl-editor.js @@ -1,8 +1,9 @@ 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); const repl = require('repl'); +const ArrayStream = require('../common/arraystream'); // \u001b[1G - Moves the cursor to 1st column // \u001b[0J - Clear screen @@ -11,7 +12,7 @@ const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G'; const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g'); function run({ input, output, event, checkTerminalCodes = true }) { - const stream = new common.ArrayStream(); + const stream = new ArrayStream(); let found = ''; stream.write = (msg) => found += msg.replace('\r', ''); @@ -74,8 +75,8 @@ tests.forEach(run); // Auto code alignment for .editor mode function testCodeAligment({ input, cursor = 0, line = '' }) { - const stream = new common.ArrayStream(); - const outputStream = new common.ArrayStream(); + const stream = new ArrayStream(); + const outputStream = new ArrayStream(); stream.write = () => { throw new Error('Writing not allowed!'); }; diff --git a/test/parallel/test-repl-end-emits-exit.js b/test/parallel/test-repl-end-emits-exit.js index 67f667eeb3d8db..d01be957d3990b 100644 --- a/test/parallel/test-repl-end-emits-exit.js +++ b/test/parallel/test-repl-end-emits-exit.js @@ -20,14 +20,15 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); let terminalExit = 0; let regularExit = 0; // Create a dummy stream that does nothing -const stream = new common.ArrayStream(); +const stream = new ArrayStream(); function testTerminalMode() { const r1 = repl.start({ diff --git a/test/parallel/test-repl-eval-scope.js b/test/parallel/test-repl-eval-scope.js index 00b577cba73f76..702b6056f101a5 100644 --- a/test/parallel/test-repl-eval-scope.js +++ b/test/parallel/test-repl-eval-scope.js @@ -1,10 +1,11 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); { - const stream = new common.ArrayStream(); + const stream = new ArrayStream(); const options = { eval: common.mustCall((cmd, context) => { assert.strictEqual(cmd, '.scope\n'); diff --git a/test/parallel/test-repl-inspector.js b/test/parallel/test-repl-inspector.js index b02f6139e72d60..acec99208bcf02 100644 --- a/test/parallel/test-repl-inspector.js +++ b/test/parallel/test-repl-inspector.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); @@ -8,7 +9,7 @@ common.skipIfInspectorDisabled(); // This test verifies that the V8 inspector API is usable in the REPL. -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); let output = ''; putIn.write = function(data) { output += data; diff --git a/test/parallel/test-repl-let-process.js b/test/parallel/test-repl-let-process.js index dd8fa60f463d8b..d0524953d74650 100644 --- a/test/parallel/test-repl-let-process.js +++ b/test/parallel/test-repl-let-process.js @@ -1,8 +1,9 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const repl = require('repl'); // Regression test for https://github.com/nodejs/node/issues/6802 -const input = new common.ArrayStream(); +const input = new ArrayStream(); repl.start({ input, output: process.stdout, useGlobal: true }); input.run(['let process']); diff --git a/test/parallel/test-repl-load-multiline.js b/test/parallel/test-repl-load-multiline.js index fd58a3c21dd884..87b866c01be165 100644 --- a/test/parallel/test-repl-load-multiline.js +++ b/test/parallel/test-repl-load-multiline.js @@ -1,5 +1,6 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const fixtures = require('../common/fixtures'); const assert = require('assert'); const repl = require('repl'); @@ -20,8 +21,8 @@ undefined let accum = ''; -const inputStream = new common.ArrayStream(); -const outputStream = new common.ArrayStream(); +const inputStream = new ArrayStream(); +const outputStream = new ArrayStream(); outputStream.write = (data) => accum += data.replace('\r', ''); diff --git a/test/parallel/test-repl-multiline.js b/test/parallel/test-repl-multiline.js index 54048bf31f2f6f..f789c06bf39378 100644 --- a/test/parallel/test-repl-multiline.js +++ b/test/parallel/test-repl-multiline.js @@ -1,9 +1,10 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); -const inputStream = new common.ArrayStream(); -const outputStream = new common.ArrayStream(); +const inputStream = new ArrayStream(); +const outputStream = new ArrayStream(); const input = ['var foo = {', '};', 'foo;']; let output = ''; diff --git a/test/parallel/test-repl-options.js b/test/parallel/test-repl-options.js index 1de49c8e861391..8d9f8e5b0703c4 100644 --- a/test/parallel/test-repl-options.js +++ b/test/parallel/test-repl-options.js @@ -21,11 +21,12 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); // Create a dummy stream that does nothing -const stream = new common.ArrayStream(); +const stream = new ArrayStream(); // 1, mostly defaults const r1 = repl.start({ diff --git a/test/parallel/test-repl-pretty-custom-stack.js b/test/parallel/test-repl-pretty-custom-stack.js index be102c1d677a9c..3c758fb2f2ef13 100644 --- a/test/parallel/test-repl-pretty-custom-stack.js +++ b/test/parallel/test-repl-pretty-custom-stack.js @@ -1,5 +1,6 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const fixtures = require('../common/fixtures'); const assert = require('assert'); const repl = require('repl'); @@ -8,8 +9,8 @@ const repl = require('repl'); function run({ command, expected }) { let accum = ''; - const inputStream = new common.ArrayStream(); - const outputStream = new common.ArrayStream(); + const inputStream = new ArrayStream(); + const outputStream = new ArrayStream(); outputStream.write = (data) => accum += data.replace('\r', ''); diff --git a/test/parallel/test-repl-pretty-stack.js b/test/parallel/test-repl-pretty-stack.js index 0fc6b3ada04c79..f4754d315df44f 100644 --- a/test/parallel/test-repl-pretty-stack.js +++ b/test/parallel/test-repl-pretty-stack.js @@ -1,5 +1,6 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const fixtures = require('../common/fixtures'); const assert = require('assert'); const repl = require('repl'); @@ -8,8 +9,8 @@ const repl = require('repl'); function run({ command, expected }) { let accum = ''; - const inputStream = new common.ArrayStream(); - const outputStream = new common.ArrayStream(); + const inputStream = new ArrayStream(); + const outputStream = new ArrayStream(); outputStream.write = (data) => accum += data.replace('\r', ''); diff --git a/test/parallel/test-repl-recoverable.js b/test/parallel/test-repl-recoverable.js index 6788d84595066c..a975dac782cfe4 100644 --- a/test/parallel/test-repl-recoverable.js +++ b/test/parallel/test-repl-recoverable.js @@ -1,6 +1,7 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); @@ -14,7 +15,7 @@ function customEval(code, context, file, cb) { return cb(evalCount === 1 ? new repl.Recoverable() : null, true); } -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); putIn.write = function(msg) { if (msg === '... ') { diff --git a/test/parallel/test-repl-reset-event.js b/test/parallel/test-repl-reset-event.js index 96d1d199af34c9..1f1347547e95f8 100644 --- a/test/parallel/test-repl-reset-event.js +++ b/test/parallel/test-repl-reset-event.js @@ -21,7 +21,7 @@ 'use strict'; const common = require('../common'); - +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); const util = require('util'); @@ -29,7 +29,7 @@ const util = require('util'); common.allowGlobals(42); // Create a dummy stream that does nothing -const dummy = new common.ArrayStream(); +const dummy = new ArrayStream(); function testReset(cb) { const r = repl.start({ diff --git a/test/parallel/test-repl-save-load.js b/test/parallel/test-repl-save-load.js index 3778ffac3ec379..7ca0e9c0164056 100644 --- a/test/parallel/test-repl-save-load.js +++ b/test/parallel/test-repl-save-load.js @@ -20,7 +20,8 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const join = require('path').join; const fs = require('fs'); @@ -32,7 +33,7 @@ const repl = require('repl'); const works = [['inner.one'], 'inner.o']; -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); const testMe = repl.start('', putIn); @@ -59,7 +60,7 @@ assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'), 'return "saved";', '}' ]; - const putIn = new common.ArrayStream(); + const putIn = new ArrayStream(); const replServer = repl.start('', putIn); putIn.run(['.editor']); diff --git a/test/parallel/test-repl-syntax-error-stack.js b/test/parallel/test-repl-syntax-error-stack.js index 3f55959fda067e..2794ded4924a97 100644 --- a/test/parallel/test-repl-syntax-error-stack.js +++ b/test/parallel/test-repl-syntax-error-stack.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const fixtures = require('../common/fixtures'); const assert = require('assert'); const repl = require('repl'); @@ -10,7 +11,7 @@ process.on('exit', () => { assert.strictEqual(found, true); }); -common.ArrayStream.prototype.write = function(output) { +ArrayStream.prototype.write = function(output) { // Matching only on a minimal piece of the stack because the string will vary // greatly depending on the JavaScript engine. V8 includes `;` because it // displays the line of code (`var foo bar;`) that is causing a problem. @@ -20,7 +21,7 @@ common.ArrayStream.prototype.write = function(output) { found = true; }; -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); repl.start('', putIn); let file = fixtures.path('syntax', 'bad_syntax'); diff --git a/test/parallel/test-repl-tab-complete-crash.js b/test/parallel/test-repl-tab-complete-crash.js index ba8de7888e3b09..95dfe0bd4c4e13 100644 --- a/test/parallel/test-repl-tab-complete-crash.js +++ b/test/parallel/test-repl-tab-complete-crash.js @@ -1,12 +1,13 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const repl = require('repl'); -common.ArrayStream.prototype.write = () => {}; +ArrayStream.prototype.write = () => {}; -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); const testMe = repl.start('', putIn); // https://github.com/nodejs/node/issues/3346 diff --git a/test/parallel/test-repl-tab-complete-no-warn.js b/test/parallel/test-repl-tab-complete-no-warn.js index 3379cec8453cba..cef2026dfba853 100644 --- a/test/parallel/test-repl-tab-complete-no-warn.js +++ b/test/parallel/test-repl-tab-complete-no-warn.js @@ -1,13 +1,14 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const repl = require('repl'); const DEFAULT_MAX_LISTENERS = require('events').defaultMaxListeners; -common.ArrayStream.prototype.write = () => { +ArrayStream.prototype.write = () => { }; -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); const testMe = repl.start('', putIn); // https://github.com/nodejs/node/issues/18284 diff --git a/test/parallel/test-repl-tab-complete.js b/test/parallel/test-repl-tab-complete.js index ca7d2054758d21..a336058fa16d01 100644 --- a/test/parallel/test-repl-tab-complete.js +++ b/test/parallel/test-repl-tab-complete.js @@ -22,6 +22,7 @@ 'use strict'; const common = require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const fixtures = require('../common/fixtures'); const hasInspector = process.config.variables.v8_enable_inspector === 1; @@ -44,7 +45,7 @@ function getNoResultsFunction() { } const works = [['inner.one'], 'inner.o']; -const putIn = new common.ArrayStream(); +const putIn = new ArrayStream(); const testMe = repl.start('', putIn); // Some errors are passed to the domain, but do not callback @@ -525,7 +526,7 @@ testCustomCompleterAsyncMode.complete('a', common.mustCall((error, data) => { })); // tab completion in editor mode -const editorStream = new common.ArrayStream(); +const editorStream = new ArrayStream(); const editor = repl.start({ stream: editorStream, terminal: true, @@ -548,7 +549,7 @@ editor.completer('var log = console.l', common.mustCall((error, data) => { { // tab completion of lexically scoped variables - const stream = new common.ArrayStream(); + const stream = new ArrayStream(); const testRepl = repl.start({ stream }); stream.run([` diff --git a/test/parallel/test-repl-top-level-await.js b/test/parallel/test-repl-top-level-await.js index 762def7c003d78..7cb6bf20fd4fd5 100644 --- a/test/parallel/test-repl-top-level-await.js +++ b/test/parallel/test-repl-top-level-await.js @@ -1,6 +1,7 @@ 'use strict'; -const common = require('../common'); +require('../common'); +const ArrayStream = require('../common/arraystream'); const assert = require('assert'); const { stripVTControlCharacters } = require('internal/readline'); const repl = require('repl'); @@ -9,7 +10,7 @@ const repl = require('repl'); const PROMPT = 'await repl > '; -class REPLStream extends common.ArrayStream { +class REPLStream extends ArrayStream { constructor() { super(); this.waitingForResponse = false;