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 authored and targos committed Sep 3, 2018
1 parent e684382 commit 98af170
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
Original file line number Diff line number Diff line change
Expand Up @@ -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>]

Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/common/arraystream.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-autolibs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-context.js
Original file line number Diff line number Diff line change
@@ -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.
{
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions test/parallel/test-repl-editor.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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', '');
Expand Down Expand Up @@ -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!'); };

Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-end-emits-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-eval-scope.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-inspector.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-let-process.js
Original file line number Diff line number Diff line change
@@ -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']);
7 changes: 4 additions & 3 deletions test/parallel/test-repl-load-multiline.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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', '');

Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-multiline.js
Original file line number Diff line number Diff line change
@@ -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 = '';

Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-repl-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-repl-pretty-custom-stack.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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', '');

Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-repl-pretty-stack.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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', '');

Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-recoverable.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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 === '... ') {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-repl-reset-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-repl-save-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);


Expand All @@ -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']);
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-repl-syntax-error-stack.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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.
Expand All @@ -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');

Expand Down
Loading

0 comments on commit 98af170

Please sign in to comment.