Skip to content
Permalink
Browse files

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: #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 fa543c00cdaca6a6c68d1df4c68c061e2b82ca21
@@ -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` [&lt;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.

<!-- 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

The `Countdown` module provides a simple countdown mechanism for tests that
@@ -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;
@@ -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 { fixturesDir } = require('./fixtures');
const tmpdir = require('./tmpdir');
@@ -511,23 +510,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.
@@ -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();
@@ -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.
{
@@ -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) {
@@ -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!'); };

@@ -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({
@@ -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');
@@ -1,14 +1,15 @@
'use strict';

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

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;
@@ -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']);
@@ -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', '');

@@ -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 = '';

@@ -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({
@@ -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', '');

@@ -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', '');

@@ -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 === '... ') {
@@ -21,15 +21,15 @@

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

const ArrayStream = require('../common/arraystream');
const assert = require('assert');
const repl = require('repl');
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({
@@ -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']);
@@ -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');

0 comments on commit fa543c0

Please sign in to comment.
You can’t perform that action at this time.