Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bad4b9b

Browse filesBrowse files
dario-piotrowicztargos
authored andcommitted
test: add new startNewREPLSever testing utility
PR-URL: #59964 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent ea0f5e5 commit bad4b9b
Copy full SHA for bad4b9b

File tree

Expand file treeCollapse file tree

53 files changed

+522
-992
lines changed
Open diff view settings
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

53 files changed

+522
-992
lines changed
Open diff view settings
Collapse file

‎test/common/repl.js‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const ArrayStream = require('../common/arraystream');
4+
const repl = require('node:repl');
5+
const assert = require('node:assert');
6+
7+
function startNewREPLServer(replOpts = {}, testingOpts = {}) {
8+
const input = new ArrayStream();
9+
const output = new ArrayStream();
10+
11+
output.accumulator = '';
12+
output.write = (data) => (output.accumulator += `${data}`.replaceAll('\r', ''));
13+
14+
const replServer = repl.start({
15+
prompt: '',
16+
input,
17+
output,
18+
terminal: true,
19+
allowBlockingCompletions: true,
20+
...replOpts,
21+
});
22+
23+
if (!testingOpts.disableDomainErrorAssert) {
24+
// Some errors are passed to the domain, but do not callback
25+
replServer._domain.on('error', assert.ifError);
26+
}
27+
28+
return { replServer, input, output };
29+
}
30+
31+
module.exports = { startNewREPLServer };
Collapse file

‎test/parallel/test-repl-autolibs.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-repl-autolibs.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
'use strict';
2323
const common = require('../common');
2424
const ArrayStream = require('../common/arraystream');
25+
const { startNewREPLServer } = require('../common/repl');
2526
const assert = require('assert');
2627
const util = require('util');
27-
const repl = require('repl');
2828

2929
const putIn = new ArrayStream();
30-
repl.start('', putIn, null, true);
30+
startNewREPLServer({ input: putIn, output: putIn, useGlobal: true, terminal: false });
3131

3232
test1();
3333

Collapse file

‎test/parallel/test-repl-completion-on-getters-disabled.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-repl-completion-on-getters-disabled.js
+3-11Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@ const common = require('../common');
44
const assert = require('node:assert');
55
const { describe, test } = require('node:test');
66

7-
const ArrayStream = require('../common/arraystream');
8-
9-
const repl = require('node:repl');
7+
const { startNewREPLServer } = require('../common/repl');
108

119
function runCompletionTests(replInit, tests) {
12-
const stream = new ArrayStream();
13-
const testRepl = repl.start({ stream });
14-
15-
// Some errors are passed to the domain
16-
testRepl._domain.on('error', assert.ifError);
17-
18-
testRepl.write(replInit);
19-
testRepl.write('\n');
10+
const { replServer: testRepl, input } = startNewREPLServer();
11+
input.run([replInit]);
2012

2113
tests.forEach(([query, expectedCompletions]) => {
2214
testRepl.complete(query, common.mustCall((error, data) => {
Collapse file

‎test/parallel/test-repl-context.js‎

Copy file name to clipboard
+32-40Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
'use strict';
22
require('../common');
3-
const ArrayStream = require('../common/arraystream');
43
const assert = require('assert');
5-
const repl = require('repl');
64
const vm = require('vm');
7-
8-
// Create a dummy stream that does nothing.
9-
const stream = new ArrayStream();
5+
const { startNewREPLServer } = require('../common/repl');
106

117
// Test context when useGlobal is false.
128
{
13-
const r = repl.start({
14-
input: stream,
15-
output: stream,
9+
const { replServer, output } = startNewREPLServer({
10+
terminal: false,
1611
useGlobal: false
1712
});
1813

19-
let output = '';
20-
stream.write = function(d) {
21-
output += d;
22-
};
23-
2414
// Ensure that the repl context gets its own "console" instance.
25-
assert(r.context.console);
15+
assert(replServer.context.console);
2616

2717
// Ensure that the repl console instance is not the global one.
28-
assert.notStrictEqual(r.context.console, console);
29-
assert.notStrictEqual(r.context.Object, Object);
18+
assert.notStrictEqual(replServer.context.console, console);
19+
assert.notStrictEqual(replServer.context.Object, Object);
3020

31-
stream.run(['({} instanceof Object)']);
21+
replServer.write('({} instanceof Object)\n');
3222

33-
assert.strictEqual(output, 'true\n> ');
23+
assert.strictEqual(output.accumulator, 'true\n');
3424

35-
const context = r.createContext();
25+
const context = replServer.createContext();
3626
// Ensure that the repl context gets its own "console" instance.
3727
assert(context.console instanceof require('console').Console);
3828

@@ -41,44 +31,46 @@ const stream = new ArrayStream();
4131

4232
// Ensure that the repl console instance is writable.
4333
context.console = 'foo';
44-
r.close();
34+
replServer.close();
4535
}
4636

4737
// Test for context side effects.
4838
{
49-
const server = repl.start({ input: stream, output: stream });
39+
const { replServer } = startNewREPLServer({
40+
useGlobal: false
41+
});
5042

51-
assert.ok(!server.underscoreAssigned);
52-
assert.strictEqual(server.lines.length, 0);
43+
assert.ok(!replServer.underscoreAssigned);
44+
assert.strictEqual(replServer.lines.length, 0);
5345

5446
// An assignment to '_' in the repl server
55-
server.write('_ = 500;\n');
56-
assert.ok(server.underscoreAssigned);
57-
assert.strictEqual(server.lines.length, 1);
58-
assert.strictEqual(server.lines[0], '_ = 500;');
59-
assert.strictEqual(server.last, 500);
47+
replServer.write('_ = 500;\n');
48+
assert.ok(replServer.underscoreAssigned);
49+
assert.strictEqual(replServer.lines.length, 1);
50+
assert.strictEqual(replServer.lines[0], '_ = 500;');
51+
assert.strictEqual(replServer.last, 500);
6052

6153
// Use the server to create a new context
62-
const context = server.createContext();
54+
const context = replServer.createContext();
6355

6456
// Ensure that creating a new context does not
6557
// have side effects on the server
66-
assert.ok(server.underscoreAssigned);
67-
assert.strictEqual(server.lines.length, 1);
68-
assert.strictEqual(server.lines[0], '_ = 500;');
69-
assert.strictEqual(server.last, 500);
58+
assert.ok(replServer.underscoreAssigned);
59+
assert.strictEqual(replServer.lines.length, 1);
60+
assert.strictEqual(replServer.lines[0], '_ = 500;');
61+
assert.strictEqual(replServer.last, 500);
7062

7163
// Reset the server context
72-
server.resetContext();
73-
assert.ok(!server.underscoreAssigned);
74-
assert.strictEqual(server.lines.length, 0);
64+
replServer.resetContext();
65+
assert.ok(!replServer.underscoreAssigned);
66+
assert.strictEqual(replServer.lines.length, 0);
7567

7668
// Ensure that assigning to '_' in the new context
7769
// does not change the value in our server.
78-
assert.ok(!server.underscoreAssigned);
70+
assert.ok(!replServer.underscoreAssigned);
7971
vm.runInContext('_ = 1000;\n', context);
8072

81-
assert.ok(!server.underscoreAssigned);
82-
assert.strictEqual(server.lines.length, 0);
83-
server.close();
73+
assert.ok(!replServer.underscoreAssigned);
74+
assert.strictEqual(replServer.lines.length, 0);
75+
replServer.close();
8476
}
Collapse file

‎test/parallel/test-repl-custom-eval-previews.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-repl-custom-eval-previews.js
+10-19Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
11
'use strict';
22

33
const common = require('../common');
4-
const ArrayStream = require('../common/arraystream');
54
const assert = require('assert');
65
const { describe, it } = require('node:test');
76

87
common.skipIfInspectorDisabled();
98

10-
const repl = require('repl');
9+
const { startNewREPLServer } = require('../common/repl');
1110

1211
const testingReplPrompt = '_REPL_TESTING_PROMPT_>';
1312

1413
// Processes some input in a REPL instance and returns a promise that
1514
// resolves to the produced output (as a string).
16-
function getReplRunOutput(input, replOptions) {
15+
function getReplRunOutput(inputStr, replOptions) {
1716
return new Promise((resolve) => {
18-
const inputStream = new ArrayStream();
19-
const outputStream = new ArrayStream();
17+
const { replServer, input, output } = startNewREPLServer({ prompt: testingReplPrompt, ...replOptions });
2018

21-
const replServer = repl.start({
22-
input: inputStream,
23-
output: outputStream,
24-
prompt: testingReplPrompt,
25-
...replOptions,
26-
});
19+
output.accumulator = '';
2720

28-
let output = '';
29-
30-
outputStream.write = (chunk) => {
31-
output += chunk;
21+
output.write = (chunk) => {
22+
output.accumulator += chunk;
3223
// The prompt appears after the input has been processed
33-
if (output.includes(testingReplPrompt)) {
24+
if (output.accumulator.includes(testingReplPrompt)) {
3425
replServer.close();
35-
resolve(output);
26+
resolve(output.accumulator);
3627
}
3728
};
3829

39-
inputStream.emit('data', input);
30+
input.emit('data', inputStr);
4031

41-
inputStream.run(['']);
32+
input.run(['']);
4233
});
4334
}
4435

Collapse file

‎test/parallel/test-repl-custom-eval.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-repl-custom-eval.js
+21-31Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
11
'use strict';
22

33
require('../common');
4-
const ArrayStream = require('../common/arraystream');
4+
const { startNewREPLServer } = require('../common/repl');
55
const assert = require('assert');
66
const { describe, it } = require('node:test');
77

8-
const repl = require('repl');
8+
const testingReplPrompt = '_REPL_TESTING_PROMPT_>';
99

1010
// Processes some input in a REPL instance and returns a promise that
1111
// resolves to the produced output (as a string).
12-
function getReplRunOutput(input, replOptions) {
12+
function getReplRunOutput(inputStr, replOptions) {
1313
return new Promise((resolve) => {
14-
const inputStream = new ArrayStream();
15-
const outputStream = new ArrayStream();
14+
const { replServer, input, output } = startNewREPLServer({ prompt: testingReplPrompt, ...replOptions });
1615

17-
const testingReplPrompt = '_REPL_TESTING_PROMPT_>';
16+
output.accumulator = '';
1817

19-
const replServer = repl.start({
20-
input: inputStream,
21-
output: outputStream,
22-
prompt: testingReplPrompt,
23-
...replOptions,
24-
});
25-
26-
let output = '';
27-
28-
outputStream.write = (chunk) => {
29-
output += chunk;
18+
output.write = (chunk) => {
19+
output.accumulator += chunk;
3020
// The prompt appears after the input has been processed
31-
if (output.includes(testingReplPrompt)) {
21+
if (output.accumulator.includes(testingReplPrompt)) {
3222
replServer.close();
33-
resolve(output);
23+
resolve(output.accumulator);
3424
}
3525
};
3626

37-
inputStream.emit('data', input);
27+
input.emit('data', inputStr);
3828

39-
inputStream.run(['']);
29+
input.run(['']);
4030
});
4131
}
4232

@@ -62,23 +52,23 @@ describe('repl with custom eval', { concurrency: true }, () => {
6252

6353
it('provides a repl context to the eval callback', async () => {
6454
const context = await new Promise((resolve) => {
65-
const r = repl.start({
55+
const { replServer } = startNewREPLServer({
6656
eval: (_cmd, context) => resolve(context),
6757
});
68-
r.context = { foo: 'bar' };
69-
r.write('\n.exit\n');
58+
replServer.context = { foo: 'bar' };
59+
replServer.write('\n.exit\n');
7060
});
7161
assert.strictEqual(context.foo, 'bar');
7262
});
7363

7464
it('provides the global context to the eval callback', async () => {
7565
const context = await new Promise((resolve) => {
76-
const r = repl.start({
77-
useGlobal: true,
66+
const { replServer } = startNewREPLServer({
7867
eval: (_cmd, context) => resolve(context),
68+
useGlobal: true
7969
});
8070
global.foo = 'global_foo';
81-
r.write('\n.exit\n');
71+
replServer.write('\n.exit\n');
8272
});
8373

8474
assert.strictEqual(context.foo, 'global_foo');
@@ -88,12 +78,12 @@ describe('repl with custom eval', { concurrency: true }, () => {
8878
it('inherits variables from the global context but does not use it afterwords if `useGlobal` is false', async () => {
8979
global.bar = 'global_bar';
9080
const context = await new Promise((resolve) => {
91-
const r = repl.start({
81+
const { replServer } = startNewREPLServer({
9282
useGlobal: false,
9383
eval: (_cmd, context) => resolve(context),
9484
});
9585
global.baz = 'global_baz';
96-
r.write('\n.exit\n');
86+
replServer.write('\n.exit\n');
9787
});
9888

9989
assert.strictEqual(context.bar, 'global_bar');
@@ -111,10 +101,10 @@ describe('repl with custom eval', { concurrency: true }, () => {
111101
*/
112102
it('preserves the original input', async () => {
113103
const cmd = await new Promise((resolve) => {
114-
const r = repl.start({
104+
const { replServer } = startNewREPLServer({
115105
eval: (cmd) => resolve(cmd),
116106
});
117-
r.write('function f() {}\n.exit\n');
107+
replServer.write('function f() {}\n.exit\n');
118108
});
119109
assert.strictEqual(cmd, 'function f() {}\n');
120110
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.