Skip to content

Commit

Permalink
test: move common.ArrayStream to separate module
Browse files Browse the repository at this point in the history
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: #22447
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
jasnell committed Aug 23, 2018
1 parent 2d64a51 commit fa543c0
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 66 deletions.
17 changes: 14 additions & 3 deletions test/common/README.md
Expand Up @@ -38,9 +38,6 @@ tasks.


Takes `whitelist` and concats that with predefined `knownGlobals`. Takes `whitelist` and concats that with predefined `knownGlobals`.


### arrayStream
A stream to push an array into a REPL

### busyLoop(time) ### busyLoop(time)
* `time` [&lt;number>] * `time` [&lt;number>]


Expand Down Expand Up @@ -413,6 +410,20 @@ Platform normalizes the `pwd` command.


Synchronous version of `spawnPwd`. Synchronous version of `spawnPwd`.


## ArrayStream Module

The `ArrayStream` module provides a simple `Stream` that pushes elements from
a given array.

<!-- eslint-disable no-undef, node-core/required-modules -->
```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 ## Countdown Module


The `Countdown` module provides a simple countdown mechanism for tests that The `Countdown` module provides a simple countdown mechanism for tests that
Expand Down
24 changes: 24 additions & 0 deletions 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;
18 changes: 0 additions & 18 deletions test/common/index.js
Expand Up @@ -27,7 +27,6 @@ const fs = require('fs');
const assert = require('assert'); const assert = require('assert');
const os = require('os'); const os = require('os');
const { exec, execSync, spawn, spawnSync } = require('child_process'); const { exec, execSync, spawn, spawnSync } = require('child_process');
const stream = require('stream');
const util = require('util'); const util = require('util');
const { fixturesDir } = require('./fixtures'); const { fixturesDir } = require('./fixtures');
const tmpdir = require('./tmpdir'); const tmpdir = require('./tmpdir');
Expand Down Expand Up @@ -511,23 +510,6 @@ exports.skip = function(msg) {
process.exit(0); 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" // 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, // represent the exit code and/or signal name of a node process that aborted,
// false otherwise. // false otherwise.
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-autolibs.js
Expand Up @@ -21,11 +21,12 @@


'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const util = require('util'); const util = require('util');
const repl = require('repl'); const repl = require('repl');


const putIn = new common.ArrayStream(); const putIn = new ArrayStream();
repl.start('', putIn, null, true); repl.start('', putIn, null, true);


test1(); test1();
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-context.js
@@ -1,11 +1,12 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
const vm = require('vm'); const vm = require('vm');


// Create a dummy stream that does nothing. // Create a dummy stream that does nothing.
const stream = new common.ArrayStream(); const stream = new ArrayStream();


// Test context when useGlobal is false. // Test context when useGlobal is false.
{ {
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-domain.js
Expand Up @@ -20,11 +20,12 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.


'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');


const repl = require('repl'); const repl = require('repl');


const putIn = new common.ArrayStream(); const putIn = new ArrayStream();
repl.start('', putIn); repl.start('', putIn);


putIn.write = function(data) { putIn.write = function(data) {
Expand Down
9 changes: 5 additions & 4 deletions test/parallel/test-repl-editor.js
@@ -1,8 +1,9 @@
'use strict'; 'use strict';


const common = require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
const ArrayStream = require('../common/arraystream');


// \u001b[1G - Moves the cursor to 1st column // \u001b[1G - Moves the cursor to 1st column
// \u001b[0J - Clear screen // \u001b[0J - Clear screen
Expand All @@ -11,7 +12,7 @@ const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g'); const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');


function run({ input, output, event, checkTerminalCodes = true }) { function run({ input, output, event, checkTerminalCodes = true }) {
const stream = new common.ArrayStream(); const stream = new ArrayStream();
let found = ''; let found = '';


stream.write = (msg) => found += msg.replace('\r', ''); stream.write = (msg) => found += msg.replace('\r', '');
Expand Down Expand Up @@ -74,8 +75,8 @@ tests.forEach(run);


// Auto code alignment for .editor mode // Auto code alignment for .editor mode
function testCodeAligment({ input, cursor = 0, line = '' }) { function testCodeAligment({ input, cursor = 0, line = '' }) {
const stream = new common.ArrayStream(); const stream = new ArrayStream();
const outputStream = new common.ArrayStream(); const outputStream = new ArrayStream();


stream.write = () => { throw new Error('Writing not allowed!'); }; stream.write = () => { throw new Error('Writing not allowed!'); };


Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-end-emits-exit.js
Expand Up @@ -20,14 +20,15 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.


'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
let terminalExit = 0; let terminalExit = 0;
let regularExit = 0; let regularExit = 0;


// Create a dummy stream that does nothing // Create a dummy stream that does nothing
const stream = new common.ArrayStream(); const stream = new ArrayStream();


function testTerminalMode() { function testTerminalMode() {
const r1 = repl.start({ const r1 = repl.start({
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-eval-scope.js
@@ -1,10 +1,11 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');


{ {
const stream = new common.ArrayStream(); const stream = new ArrayStream();
const options = { const options = {
eval: common.mustCall((cmd, context) => { eval: common.mustCall((cmd, context) => {
assert.strictEqual(cmd, '.scope\n'); assert.strictEqual(cmd, '.scope\n');
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-inspector.js
@@ -1,14 +1,15 @@
'use strict'; 'use strict';


const common = require('../common'); const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');


common.skipIfInspectorDisabled(); common.skipIfInspectorDisabled();


// This test verifies that the V8 inspector API is usable in the REPL. // This test verifies that the V8 inspector API is usable in the REPL.


const putIn = new common.ArrayStream(); const putIn = new ArrayStream();
let output = ''; let output = '';
putIn.write = function(data) { putIn.write = function(data) {
output += data; output += data;
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-let-process.js
@@ -1,8 +1,9 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const repl = require('repl'); const repl = require('repl');


// Regression test for https://github.com/nodejs/node/issues/6802 // 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 }); repl.start({ input, output: process.stdout, useGlobal: true });
input.run(['let process']); input.run(['let process']);
7 changes: 4 additions & 3 deletions test/parallel/test-repl-load-multiline.js
@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
Expand All @@ -20,8 +21,8 @@ undefined


let accum = ''; let accum = '';


const inputStream = new common.ArrayStream(); const inputStream = new ArrayStream();
const outputStream = new common.ArrayStream(); const outputStream = new ArrayStream();


outputStream.write = (data) => accum += data.replace('\r', ''); outputStream.write = (data) => accum += data.replace('\r', '');


Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-multiline.js
@@ -1,9 +1,10 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
const inputStream = new common.ArrayStream(); const inputStream = new ArrayStream();
const outputStream = new common.ArrayStream(); const outputStream = new ArrayStream();
const input = ['var foo = {', '};', 'foo;']; const input = ['var foo = {', '};', 'foo;'];
let output = ''; let output = '';


Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-options.js
Expand Up @@ -21,11 +21,12 @@


'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');


// Create a dummy stream that does nothing // Create a dummy stream that does nothing
const stream = new common.ArrayStream(); const stream = new ArrayStream();


// 1, mostly defaults // 1, mostly defaults
const r1 = repl.start({ const r1 = repl.start({
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-repl-pretty-custom-stack.js
@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
Expand All @@ -8,8 +9,8 @@ const repl = require('repl');
function run({ command, expected }) { function run({ command, expected }) {
let accum = ''; let accum = '';


const inputStream = new common.ArrayStream(); const inputStream = new ArrayStream();
const outputStream = new common.ArrayStream(); const outputStream = new ArrayStream();


outputStream.write = (data) => accum += data.replace('\r', ''); outputStream.write = (data) => accum += data.replace('\r', '');


Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-repl-pretty-stack.js
@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
Expand All @@ -8,8 +9,8 @@ const repl = require('repl');
function run({ command, expected }) { function run({ command, expected }) {
let accum = ''; let accum = '';


const inputStream = new common.ArrayStream(); const inputStream = new ArrayStream();
const outputStream = new common.ArrayStream(); const outputStream = new ArrayStream();


outputStream.write = (data) => accum += data.replace('\r', ''); outputStream.write = (data) => accum += data.replace('\r', '');


Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-recoverable.js
@@ -1,6 +1,7 @@
'use strict'; 'use strict';


const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');


Expand All @@ -14,7 +15,7 @@ function customEval(code, context, file, cb) {
return cb(evalCount === 1 ? new repl.Recoverable() : null, true); return cb(evalCount === 1 ? new repl.Recoverable() : null, true);
} }


const putIn = new common.ArrayStream(); const putIn = new ArrayStream();


putIn.write = function(msg) { putIn.write = function(msg) {
if (msg === '... ') { if (msg === '... ') {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-repl-reset-event.js
Expand Up @@ -21,15 +21,15 @@


'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');

const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
const util = require('util'); const util = require('util');


common.allowGlobals(42); common.allowGlobals(42);


// Create a dummy stream that does nothing // Create a dummy stream that does nothing
const dummy = new common.ArrayStream(); const dummy = new ArrayStream();


function testReset(cb) { function testReset(cb) {
const r = repl.start({ const r = repl.start({
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-repl-save-load.js
Expand Up @@ -20,7 +20,8 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.


'use strict'; 'use strict';
const common = require('../common'); require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert'); const assert = require('assert');
const join = require('path').join; const join = require('path').join;
const fs = require('fs'); const fs = require('fs');
Expand All @@ -32,7 +33,7 @@ const repl = require('repl');


const works = [['inner.one'], 'inner.o']; const works = [['inner.one'], 'inner.o'];


const putIn = new common.ArrayStream(); const putIn = new ArrayStream();
const testMe = repl.start('', putIn); const testMe = repl.start('', putIn);




Expand All @@ -59,7 +60,7 @@ assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
'return "saved";', 'return "saved";',
'}' '}'
]; ];
const putIn = new common.ArrayStream(); const putIn = new ArrayStream();
const replServer = repl.start('', putIn); const replServer = repl.start('', putIn);


putIn.run(['.editor']); putIn.run(['.editor']);
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-syntax-error-stack.js
@@ -1,6 +1,7 @@
'use strict'; 'use strict';


const common = require('../common'); const common = require('../common');
const ArrayStream = require('../common/arraystream');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
Expand All @@ -10,7 +11,7 @@ process.on('exit', () => {
assert.strictEqual(found, true); 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 // Matching only on a minimal piece of the stack because the string will vary
// greatly depending on the JavaScript engine. V8 includes `;` because it // greatly depending on the JavaScript engine. V8 includes `;` because it
// displays the line of code (`var foo bar;`) that is causing a problem. // displays the line of code (`var foo bar;`) that is causing a problem.
Expand All @@ -20,7 +21,7 @@ common.ArrayStream.prototype.write = function(output) {
found = true; found = true;
}; };


const putIn = new common.ArrayStream(); const putIn = new ArrayStream();
repl.start('', putIn); repl.start('', putIn);
let file = fixtures.path('syntax', 'bad_syntax'); let file = fixtures.path('syntax', 'bad_syntax');


Expand Down

0 comments on commit fa543c0

Please sign in to comment.