Skip to content

Commit

Permalink
test(cosmic-swingset): Include the last three lines of stderr when a …
Browse files Browse the repository at this point in the history
…`make` test fails (#10438)

## Description
Extracted from #10165 to independently resolve a preëxisting TODO.

### Security Considerations
n/a

### Scaling Considerations
n/a

### Documentation Considerations
n/a

### Testing Considerations
Makes it easier to debug failures in `make`-dependent cosmic-swingset tests.

### Upgrade Considerations
n/a
  • Loading branch information
gibson042 authored Nov 10, 2024
1 parent 2837beb commit fd68b6a
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions packages/cosmic-swingset/test/scenario2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-check
import { text } from 'node:stream/consumers';

const { freeze, entries } = Object;

const onlyStderr = ['ignore', 'ignore', 'inherit'];
Expand All @@ -10,13 +12,35 @@ export const pspawn = (bin, { spawn, cwd }) => {
/** @type {import('child_process').ChildProcess} */
let child;
const exit = new Promise((resolve, reject) => {
// console.debug('spawn', bin, args, { cwd: makefileDir, ...opts });
child = spawn(bin, args, { cwd, ...opts });
child.addListener('exit', code => {
// When stderr is otherwise ignored, spy on it for inclusion in error messages.
/** @type {Promise<string> | undefined} */
let stderrP;
// https://nodejs.org/docs/latest/api/child_process.html#optionsstdio
let { stdio = 'pipe' } = opts;
if (stdio === 'ignore' || stdio[2] === 'ignore') {
stdio = typeof stdio === 'string' ? [stdio, stdio, stdio] : [...stdio];
stdio[2] = 'pipe';
} else {
// The caller is not ignoring stderr, so we pretend it is empty here.
stderrP = Promise.resolve('');
}
const resolvedOpts = { cwd, ...opts, stdio };
// console.debug('spawn', bin, args, resolvedOpts);
child = spawn(bin, args, resolvedOpts);
if (!stderrP) {
assert(child.stderr);
stderrP = text(child.stderr);
child.stderr = null;
}
child.addListener('exit', async code => {
if (code !== 0) {
// TODO: Include ~3 lines from child.stderr or child.stdout if present.
// see https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
reject(Error(`exit ${code} from command: ${bin} ${args}`));
let msg = `exit ${code} from command: ${bin} ${args}`;
const errStr = await stderrP;
if (errStr) {
const errTail = errStr.split('\n').slice(-3);
msg = `${msg}\n${errTail.map(line => ` ${line}`).join('\n')}`;
}
reject(Error(msg));
return;
}
resolve(0);
Expand Down

0 comments on commit fd68b6a

Please sign in to comment.