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 <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>
1 parent e684382 commit 98af170

24 files changed

+103
-66
lines changed

test/common/README.md

Lines changed: 14 additions & 3 deletions
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

Lines changed: 24 additions & 0 deletions
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

Lines changed: 0 additions & 18 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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;

0 commit comments

Comments
 (0)