Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: simplify ReplStream.wait() #43857

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions test/parallel/test-repl-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const common = require('../common');
const assert = require('assert');
const events = require('events');
const { REPLServer } = require('repl');
const { Stream } = require('stream');
const { inspect } = require('util');
Expand Down Expand Up @@ -32,26 +33,16 @@ class REPLStream extends Stream {
if (chunkLines.length > 1) {
this.lines.push(...chunkLines.slice(1));
}
this.emit('line');
this.emit('line', this.lines[this.lines.length - 1]);
return true;
}
wait() {
async wait() {
this.lines = [''];
return new Promise((resolve, reject) => {
const onError = (err) => {
this.removeListener('line', onLine);
reject(err);
};
const onLine = () => {
if (this.lines[this.lines.length - 1].includes(PROMPT)) {
this.removeListener('error', onError);
this.removeListener('line', onLine);
resolve(this.lines);
}
};
this.once('error', onError);
this.on('line', onLine);
});
for await (const [line] of events.on(this, 'line')) {
if (line.includes(PROMPT)) {
return this.lines;
}
}
}
pause() {}
resume() {}
Expand Down
25 changes: 8 additions & 17 deletions test/parallel/test-repl-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert');
const events = require('events');
const { stripVTControlCharacters } = require('internal/util/inspect');
const repl = require('repl');

Expand All @@ -27,31 +28,21 @@ class REPLStream extends ArrayStream {
if (chunkLines.length > 1) {
this.lines.push(...chunkLines.slice(1));
}
this.emit('line');
this.emit('line', this.lines[this.lines.length - 1]);
if (callback) callback();
return true;
}

wait() {
async wait() {
if (this.waitingForResponse) {
throw new Error('Currently waiting for response to another command');
}
this.lines = [''];
return new Promise((resolve, reject) => {
const onError = (err) => {
this.removeListener('line', onLine);
reject(err);
};
const onLine = () => {
if (this.lines[this.lines.length - 1].includes(PROMPT)) {
this.removeListener('error', onError);
this.removeListener('line', onLine);
resolve(this.lines);
}
};
this.once('error', onError);
this.on('line', onLine);
});
for await (const [line] of events.on(this, 'line')) {
if (line.includes(PROMPT)) {
return this.lines;
}
}
}
}

Expand Down