Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
truffle(test): modify runInREPL submit command
Browse files Browse the repository at this point in the history
Currently runInREPL sends all queued commands to the child process on
the first detected REPL prompt and closes the input stream on the
second REPL prompt detection which terminates the child process (bad).

This PR reworks the logic to send one queued command per REPL prompt.
  • Loading branch information
cds-amal committed Aug 22, 2022
1 parent c29faa2 commit 6ca54a5
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions packages/truffle/test/scenarios/commandRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ module.exports = {
? `debug(${displayHost})>`
: `truffle(${displayHost})>`;

let seenChildPrompt = false;
// seems safe to escape parens only, as the readyprompt is constructed from
// [a-zA-Z] strings and wrapping parens.
const escapedPrompt = readyPrompt.replace("(", "\\(").replace(")", "\\)");
const readyPromptRex = new RegExp(`^${escapedPrompt}`, "gm");

let outputBuffer = "";

return new Promise((resolve, reject) => {
const child = exec(cmdLine, { cwd: config.working_directory });
let numSeenPrompts = 0;

if (child.error) return reject(child.error);

Expand All @@ -74,17 +79,20 @@ module.exports = {

child.stdout.on("data", data => {
// accumulate buffer from chunks
if (!seenChildPrompt) {
outputBuffer += data;
}
outputBuffer += data;

// child process is ready for input when it displays the readyPrompt
if (!seenChildPrompt && outputBuffer.includes(readyPrompt)) {
seenChildPrompt = true;
inputCommands.forEach(command => {
child.stdin.write(command + EOL);
});
child.stdin.end();
// count prompt
const foundPrompts = (outputBuffer.match(readyPromptRex) || []).length;
if (foundPrompts > numSeenPrompts) {
numSeenPrompts++;
if (inputCommands.length === 0) {
// commands exhausted, close stdin
child.stdin.end();
} else {
// fifo pop next command and submit
const nextCmd = inputCommands.shift();
child.stdin.write(nextCmd + EOL);
}
}

config.logger.log("OUT: ", data);
Expand Down

0 comments on commit 6ca54a5

Please sign in to comment.