-
Notifications
You must be signed in to change notification settings - Fork 12k
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: watch full stdout when waiting for match #23767
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce | |
// Create a separate instance to prevent unintended global changes to the color configuration | ||
const colors = ansiColors.create(); | ||
|
||
let stdout = ''; | ||
let stderr = ''; | ||
const cwd = options.cwd ?? process.cwd(); | ||
const env = options.env ?? process.env; | ||
console.log( | ||
|
@@ -69,48 +67,65 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce | |
} | ||
|
||
const childProcess = child_process.spawn(cmd, args, spawnOptions); | ||
childProcess.stdout!.on('data', (data: Buffer) => { | ||
stdout += data.toString('utf-8'); | ||
if (options.silent) { | ||
return; | ||
} | ||
data | ||
.toString('utf-8') | ||
.split(/[\n\r]+/) | ||
.filter((line) => line !== '') | ||
.forEach((line) => console.log(' ' + line)); | ||
}); | ||
|
||
childProcess.stderr!.on('data', (data: Buffer) => { | ||
stderr += data.toString('utf-8'); | ||
if (options.silent) { | ||
return; | ||
} | ||
data | ||
.toString('utf-8') | ||
.split(/[\n\r]+/) | ||
.filter((line) => line !== '') | ||
.forEach((line) => console.error(colors.yellow(' ' + line))); | ||
}); | ||
|
||
_processes.push(childProcess); | ||
|
||
// Create the error here so the stack shows who called this function. | ||
const error = new Error(); | ||
|
||
// Return log info about the current process status | ||
function envDump() { | ||
return [ | ||
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`, | ||
`STDOUT:\n${stdout}`, | ||
`STDERR:\n${stderr}`, | ||
].join('\n\n'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything above here was just moved into the |
||
|
||
return new Promise<ProcessOutput>((resolve, reject) => { | ||
let stdout = ''; | ||
let stderr = ''; | ||
let matched = false; | ||
|
||
childProcess.on('exit', (code: number) => { | ||
// Return log info about the current process status | ||
function envDump() { | ||
return [ | ||
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`, | ||
`STDOUT:\n${stdout}`, | ||
`STDERR:\n${stderr}`, | ||
].join('\n\n'); | ||
} | ||
|
||
childProcess.stdout!.on('data', (data: Buffer) => { | ||
stdout += data.toString('utf-8'); | ||
|
||
if (options.waitForMatch && stdout.match(options.waitForMatch)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously this did |
||
resolve({ stdout, stderr }); | ||
matched = true; | ||
} | ||
|
||
if (options.silent) { | ||
return; | ||
} | ||
|
||
data | ||
.toString('utf-8') | ||
.split(/[\n\r]+/) | ||
.filter((line) => line !== '') | ||
.forEach((line) => console.log(' ' + line)); | ||
}); | ||
|
||
childProcess.stderr!.on('data', (data: Buffer) => { | ||
stderr += data.toString('utf-8'); | ||
|
||
if (options.waitForMatch && stderr.match(options.waitForMatch)) { | ||
resolve({ stdout, stderr }); | ||
matched = true; | ||
} | ||
|
||
if (options.silent) { | ||
return; | ||
} | ||
|
||
data | ||
.toString('utf-8') | ||
.split(/[\n\r]+/) | ||
.filter((line) => line !== '') | ||
.forEach((line) => console.error(colors.yellow(' ' + line))); | ||
}); | ||
|
||
childProcess.on('close', (code: number) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note this changed from https://nodejs.org/api/child_process.html#event-close
https://nodejs.org/api/child_process.html#event-exit
|
||
_processes = _processes.filter((p) => p !== childProcess); | ||
|
||
if (options.waitForMatch && !matched) { | ||
|
@@ -134,24 +149,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce | |
reject(`Process error - "${cmd} ${args.join(' ')}": ${err}...\n\n${envDump()}\n`); | ||
}); | ||
|
||
if (options.waitForMatch) { | ||
const match = options.waitForMatch; | ||
|
||
childProcess.stdout!.on('data', (data: Buffer) => { | ||
if (data.toString().match(match)) { | ||
resolve({ stdout, stderr }); | ||
matched = true; | ||
} | ||
}); | ||
|
||
childProcess.stderr!.on('data', (data: Buffer) => { | ||
if (data.toString().match(match)) { | ||
resolve({ stdout, stderr }); | ||
matched = true; | ||
} | ||
}); | ||
} | ||
|
||
// Provide input to stdin if given. | ||
if (options.stdin) { | ||
childProcess.stdin!.write(options.stdin); | ||
|
@@ -192,14 +189,14 @@ export function waitForAnyProcessOutputToMatch( | |
|
||
childProcess.stdout!.on('data', (data: Buffer) => { | ||
stdout += data.toString(); | ||
if (data.toString().match(match)) { | ||
if (stdout.match(match)) { | ||
resolve({ stdout, stderr }); | ||
} | ||
}); | ||
|
||
childProcess.stderr!.on('data', (data: Buffer) => { | ||
stderr += data.toString(); | ||
if (data.toString().match(match)) { | ||
if (stderr.match(match)) { | ||
resolve({ stdout, stderr }); | ||
} | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errorMessage
is anError
and this was relying onerrorMessage.toString()
in the.test
below. When making theexpectToFail
return type stricter this becomes an error.