Skip to content

Commit 98af170

Browse files
jasnelltargos
authored andcommitted
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent e684382 commit 98af170

24 files changed

+103
-66
lines changed

test/common/README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ tasks.
3838

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

41-
### arrayStream
42-
A stream to push an array into a REPL
43-
4441
### busyLoop(time)
4542
* `time` [&lt;number>]
4643

@@ -413,6 +410,20 @@ Platform normalizes the `pwd` command.
413410

414411
Synchronous version of `spawnPwd`.
415412

413+
## ArrayStream Module
414+
415+
The `ArrayStream` module provides a simple `Stream` that pushes elements from
416+
a given array.
417+
418+
<!-- eslint-disable no-undef, node-core/required-modules -->
419+
```js
420+
const ArrayStream = require('../common/arraystream');
421+
const stream = new ArrayStream();
422+
stream.run(['a', 'b', 'c']);
423+
```
424+
425+
It can be used within tests as a simple mock stream.
426+
416427
## Countdown Module
417428

418429
The `Countdown` module provides a simple countdown mechanism for tests that

test/common/arraystream.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-disable node-core/required-modules */
2+
'use strict';
3+
4+
const { Stream } = require('stream');
5+
const { inherits } = require('util');
6+
function noop() {}
7+
8+
// A stream to push an array into a REPL
9+
function ArrayStream() {
10+
this.run = function(data) {
11+
data.forEach((line) => {
12+
this.emit('data', `${line}\n`);
13+
});
14+
};
15+
}
16+
17+
inherits(ArrayStream, Stream);
18+
ArrayStream.prototype.readable = true;
19+
ArrayStream.prototype.writable = true;
20+
ArrayStream.prototype.pause = noop;
21+
ArrayStream.prototype.resume = noop;
22+
ArrayStream.prototype.write = noop;
23+
24+
module.exports = ArrayStream;

test/common/index.js

-18
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const fs = require('fs');
2727
const assert = require('assert');
2828
const os = require('os');
2929
const { exec, execSync, spawn, spawnSync } = require('child_process');
30-
const stream = require('stream');
3130
const util = require('util');
3231
const Timer = process.binding('timer_wrap').Timer;
3332
const { fixturesDir } = require('./fixtures');
@@ -512,23 +511,6 @@ exports.skip = function(msg) {
512511
process.exit(0);
513512
};
514513

515-
// A stream to push an array into a REPL
516-
function ArrayStream() {
517-
this.run = function(data) {
518-
data.forEach((line) => {
519-
this.emit('data', `${line}\n`);
520-
});
521-
};
522-
}
523-
524-
util.inherits(ArrayStream, stream.Stream);
525-
exports.ArrayStream = ArrayStream;
526-
ArrayStream.prototype.readable = true;
527-
ArrayStream.prototype.writable = true;
528-
ArrayStream.prototype.pause = noop;
529-
ArrayStream.prototype.resume = noop;
530-
ArrayStream.prototype.write = noop;
531-
532514
// Returns true if the exit code "exitCode" and/or signal name "signal"
533515
// represent the exit code and/or signal name of a node process that aborted,
534516
// false otherwise.

test/parallel/test-repl-autolibs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
const ArrayStream = require('../common/arraystream');
2425
const assert = require('assert');
2526
const util = require('util');
2627
const repl = require('repl');
2728

28-
const putIn = new common.ArrayStream();
29+
const putIn = new ArrayStream();
2930
repl.start('', putIn, null, true);
3031

3132
test1();

test/parallel/test-repl-context.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const assert = require('assert');
45
const repl = require('repl');
56
const vm = require('vm');
67

78
// Create a dummy stream that does nothing.
8-
const stream = new common.ArrayStream();
9+
const stream = new ArrayStream();
910

1011
// Test context when useGlobal is false.
1112
{

test/parallel/test-repl-domain.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
24+
const ArrayStream = require('../common/arraystream');
2425

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

27-
const putIn = new common.ArrayStream();
28+
const putIn = new ArrayStream();
2829
repl.start('', putIn);
2930

3031
putIn.write = function(data) {

test/parallel/test-repl-editor.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55
const repl = require('repl');
6+
const ArrayStream = require('../common/arraystream');
67

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

1314
function run({ input, output, event, checkTerminalCodes = true }) {
14-
const stream = new common.ArrayStream();
15+
const stream = new ArrayStream();
1516
let found = '';
1617

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

7576
// Auto code alignment for .editor mode
7677
function testCodeAligment({ input, cursor = 0, line = '' }) {
77-
const stream = new common.ArrayStream();
78-
const outputStream = new common.ArrayStream();
78+
const stream = new ArrayStream();
79+
const outputStream = new ArrayStream();
7980

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

test/parallel/test-repl-end-emits-exit.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
24+
const ArrayStream = require('../common/arraystream');
2425
const assert = require('assert');
2526
const repl = require('repl');
2627
let terminalExit = 0;
2728
let regularExit = 0;
2829

2930
// Create a dummy stream that does nothing
30-
const stream = new common.ArrayStream();
31+
const stream = new ArrayStream();
3132

3233
function testTerminalMode() {
3334
const r1 = repl.start({

test/parallel/test-repl-eval-scope.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22
const common = require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const assert = require('assert');
45
const repl = require('repl');
56

67
{
7-
const stream = new common.ArrayStream();
8+
const stream = new ArrayStream();
89
const options = {
910
eval: common.mustCall((cmd, context) => {
1011
assert.strictEqual(cmd, '.scope\n');

test/parallel/test-repl-inspector.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
22

33
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
45
const assert = require('assert');
56
const repl = require('repl');
67

78
common.skipIfInspectorDisabled();
89

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

11-
const putIn = new common.ArrayStream();
12+
const putIn = new ArrayStream();
1213
let output = '';
1314
putIn.write = function(data) {
1415
output += data;
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const repl = require('repl');
45

56
// Regression test for https://github.com/nodejs/node/issues/6802
6-
const input = new common.ArrayStream();
7+
const input = new ArrayStream();
78
repl.start({ input, output: process.stdout, useGlobal: true });
89
input.run(['let process']);

test/parallel/test-repl-load-multiline.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const fixtures = require('../common/fixtures');
45
const assert = require('assert');
56
const repl = require('repl');
@@ -20,8 +21,8 @@ undefined
2021

2122
let accum = '';
2223

23-
const inputStream = new common.ArrayStream();
24-
const outputStream = new common.ArrayStream();
24+
const inputStream = new ArrayStream();
25+
const outputStream = new ArrayStream();
2526

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

test/parallel/test-repl-multiline.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22
const common = require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const assert = require('assert');
45
const repl = require('repl');
5-
const inputStream = new common.ArrayStream();
6-
const outputStream = new common.ArrayStream();
6+
const inputStream = new ArrayStream();
7+
const outputStream = new ArrayStream();
78
const input = ['var foo = {', '};', 'foo;'];
89
let output = '';
910

test/parallel/test-repl-options.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
const ArrayStream = require('../common/arraystream');
2425
const assert = require('assert');
2526
const repl = require('repl');
2627

2728
// Create a dummy stream that does nothing
28-
const stream = new common.ArrayStream();
29+
const stream = new ArrayStream();
2930

3031
// 1, mostly defaults
3132
const r1 = repl.start({

test/parallel/test-repl-pretty-custom-stack.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const fixtures = require('../common/fixtures');
45
const assert = require('assert');
56
const repl = require('repl');
@@ -8,8 +9,8 @@ const repl = require('repl');
89
function run({ command, expected }) {
910
let accum = '';
1011

11-
const inputStream = new common.ArrayStream();
12-
const outputStream = new common.ArrayStream();
12+
const inputStream = new ArrayStream();
13+
const outputStream = new ArrayStream();
1314

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

test/parallel/test-repl-pretty-stack.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
3+
const ArrayStream = require('../common/arraystream');
34
const fixtures = require('../common/fixtures');
45
const assert = require('assert');
56
const repl = require('repl');
@@ -8,8 +9,8 @@ const repl = require('repl');
89
function run({ command, expected }) {
910
let accum = '';
1011

11-
const inputStream = new common.ArrayStream();
12-
const outputStream = new common.ArrayStream();
12+
const inputStream = new ArrayStream();
13+
const outputStream = new ArrayStream();
1314

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

test/parallel/test-repl-recoverable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
4+
const ArrayStream = require('../common/arraystream');
45
const assert = require('assert');
56
const repl = require('repl');
67

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

17-
const putIn = new common.ArrayStream();
18+
const putIn = new ArrayStream();
1819

1920
putIn.write = function(msg) {
2021
if (msg === '... ') {

test/parallel/test-repl-reset-event.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121

2222
'use strict';
2323
const common = require('../common');
24-
24+
const ArrayStream = require('../common/arraystream');
2525
const assert = require('assert');
2626
const repl = require('repl');
2727
const util = require('util');
2828

2929
common.allowGlobals(42);
3030

3131
// Create a dummy stream that does nothing
32-
const dummy = new common.ArrayStream();
32+
const dummy = new ArrayStream();
3333

3434
function testReset(cb) {
3535
const r = repl.start({

test/parallel/test-repl-save-load.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
24+
const ArrayStream = require('../common/arraystream');
2425
const assert = require('assert');
2526
const join = require('path').join;
2627
const fs = require('fs');
@@ -32,7 +33,7 @@ const repl = require('repl');
3233

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

35-
const putIn = new common.ArrayStream();
36+
const putIn = new ArrayStream();
3637
const testMe = repl.start('', putIn);
3738

3839

@@ -59,7 +60,7 @@ assert.strictEqual(fs.readFileSync(saveFileName, 'utf8'),
5960
'return "saved";',
6061
'}'
6162
];
62-
const putIn = new common.ArrayStream();
63+
const putIn = new ArrayStream();
6364
const replServer = repl.start('', putIn);
6465

6566
putIn.run(['.editor']);

test/parallel/test-repl-syntax-error-stack.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const common = require('../common');
4+
const ArrayStream = require('../common/arraystream');
45
const fixtures = require('../common/fixtures');
56
const assert = require('assert');
67
const repl = require('repl');
@@ -10,7 +11,7 @@ process.on('exit', () => {
1011
assert.strictEqual(found, true);
1112
});
1213

13-
common.ArrayStream.prototype.write = function(output) {
14+
ArrayStream.prototype.write = function(output) {
1415
// Matching only on a minimal piece of the stack because the string will vary
1516
// greatly depending on the JavaScript engine. V8 includes `;` because it
1617
// displays the line of code (`var foo bar;`) that is causing a problem.
@@ -20,7 +21,7 @@ common.ArrayStream.prototype.write = function(output) {
2021
found = true;
2122
};
2223

23-
const putIn = new common.ArrayStream();
24+
const putIn = new ArrayStream();
2425
repl.start('', putIn);
2526
let file = fixtures.path('syntax', 'bad_syntax');
2627

0 commit comments

Comments
 (0)