Skip to content

Commit dc0ed57

Browse files
committed
test: address cubic findings on the shared stream-capture helper
- Call the saved originalWrite with the stream as receiver on the Buffer passthrough path; Writable.write dereferences this._writableState. The new empty-Buffer test fails with the unbound call and passes bound. - Add withStreamTtyAsync: the sync form restored isTTY as soon as an async run returned its promise, before awaited work ran. - stream-capture.test.ts now snapshots and compares full property descriptors instead of assuming a non-TTY start, and restores the stream in a finally even when a sentinel assertion fails. - cli-capture.ts starts the captures after the env/chdir/exit setup so a throw there can no longer leave the streams swallowed, matching the pre-helper ordering.
1 parent 9ffa08a commit dc0ed57

3 files changed

Lines changed: 82 additions & 32 deletions

File tree

‎src/__tests__/cli-capture.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ export async function runCliCapture(
6262
const stateDir = options.stateDirPrefix ? mkdtempForTestSync(options.stateDirPrefix) : undefined;
6363

6464
const originalExit = process.exit;
65-
const stdoutCapture = beginStdoutCapture({
66-
passthroughBufferWrites: options.passthroughBufferWrites,
67-
});
68-
const stderrCapture = beginStderrCapture({
69-
passthroughBufferWrites: options.passthroughBufferWrites,
70-
});
7165
const originalCwd = process.cwd();
7266
const restoreEnv = installIsolatedCliTestEnv({
7367
...(options.env ?? {}),
@@ -81,6 +75,12 @@ export async function runCliCapture(
8175
(process as any).exit = ((nextCode?: number) => {
8276
throw new ExitSignal(nextCode ?? 0);
8377
}) as typeof process.exit;
78+
const stdoutCapture = beginStdoutCapture({
79+
passthroughBufferWrites: options.passthroughBufferWrites,
80+
});
81+
const stderrCapture = beginStderrCapture({
82+
passthroughBufferWrites: options.passthroughBufferWrites,
83+
});
8484

8585
const sendToDaemon = async (
8686
req: CapturedDaemonRequest,
Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { test } from 'vitest';
22
import assert from 'node:assert/strict';
3-
import { withCapturedStderr, withInteractiveStderr, withStreamTty } from './stream-capture.ts';
3+
import {
4+
beginStderrCapture,
5+
withCapturedStderr,
6+
withInteractiveStderr,
7+
withStreamTty,
8+
withStreamTtyAsync,
9+
} from './stream-capture.ts';
410

511
function hasOwnWrite(stream: NodeJS.WriteStream): boolean {
612
return Object.prototype.hasOwnProperty.call(stream, 'write');
713
}
814

915
test('withCapturedStderr restores an inherited write without leaving an own property', () => {
10-
assert.equal(hasOwnWrite(process.stderr), false, 'precondition: write comes from the prototype');
16+
const writeBefore = Object.getOwnPropertyDescriptor(process.stderr, 'write');
1117
const captured = withCapturedStderr(() => {
1218
process.stderr.write('into the capture\n');
1319
});
1420
assert.equal(captured, 'into the capture\n');
15-
assert.equal(hasOwnWrite(process.stderr), false, 'a real write must flow after restore');
16-
assert.equal(process.stderr.write('back to the real stream\n'), true);
21+
assert.deepEqual(Object.getOwnPropertyDescriptor(process.stderr, 'write'), writeBefore);
1722
});
1823

1924
test('withCapturedStderr puts back a pre-existing own-property write', () => {
@@ -27,43 +32,61 @@ test('withCapturedStderr puts back a pre-existing own-property write', () => {
2732
withCapturedStderr(() => {
2833
process.stderr.write('captured\n');
2934
});
35+
assert.equal(hasOwnWrite(process.stderr), true);
36+
process.stderr.write('sentinel\n');
37+
assert.equal(sentinelHits, 1, 'the pre-capture own write must be the live one again');
3038
} finally {
31-
// If restore left its own stub in place, the sentinel below never runs.
39+
// Success: drop the restored sentinel so the prototype write is live again.
40+
// Failure mid-assert: drop whatever capture stub is still installed.
41+
delete stream.write;
42+
}
43+
});
44+
45+
test('passthroughBufferWrites forwards Buffers to the real stream with the stream as receiver', () => {
46+
const capture = beginStderrCapture({ passthroughBufferWrites: true });
47+
try {
48+
// An empty Buffer reaches the real Writable.write without emitting output; called
49+
// unbound it throws while reading `this._writableState`.
50+
assert.equal(process.stderr.write(Buffer.alloc(0)), true);
51+
} finally {
52+
capture.restore();
3253
}
33-
assert.equal(hasOwnWrite(process.stderr), true);
34-
process.stderr.write('sentinel\n');
35-
assert.equal(sentinelHits, 1, 'the pre-capture own write must be the live one again');
36-
delete stream.write;
3754
});
3855

3956
test('withInteractiveStderr hides CI, marks the stream a TTY, and restores all three', () => {
4057
const originalCi = process.env.CI;
58+
const originalIsTTY = Object.getOwnPropertyDescriptor(process.stderr, 'isTTY');
4159
process.env.CI = '1';
4260
try {
4361
const captured = withInteractiveStderr(() => {
4462
assert.equal(process.env.CI, undefined);
4563
assert.equal(process.stderr.isTTY, true);
46-
const reporterNeedsTTY = process.stderr.isTTY === true && !process.env.CI;
47-
assert.equal(reporterNeedsTTY, true);
4864
process.stderr.write('interactive line\n');
4965
});
5066
assert.equal(captured, 'interactive line\n');
5167
assert.equal(process.env.CI, '1');
52-
assert.equal(process.stderr.isTTY, undefined);
53-
assert.equal(hasOwnWrite(process.stderr), false);
68+
assert.deepEqual(Object.getOwnPropertyDescriptor(process.stderr, 'isTTY'), originalIsTTY);
69+
assert.deepEqual(Object.getOwnPropertyDescriptor(process.stderr, 'write'), undefined);
5470
} finally {
5571
if (originalCi === undefined) delete process.env.CI;
5672
else process.env.CI = originalCi;
5773
}
5874
});
5975

60-
test('withStreamTty leaves no own isTTY behind on a stream that never owned one', () => {
61-
assert.equal(
62-
Object.prototype.hasOwnProperty.call(process.stdout, 'isTTY'),
63-
false,
64-
'precondition: stdout inherits isTTY',
65-
);
66-
const sawTty = withStreamTty(process.stdout, true, () => process.stdout.isTTY === true);
67-
assert.equal(sawTty, true);
68-
assert.equal(Object.prototype.hasOwnProperty.call(process.stdout, 'isTTY'), false);
76+
test('withStreamTty restores the original isTTY descriptor state', () => {
77+
const isTTYBefore = Object.getOwnPropertyDescriptor(process.stdout, 'isTTY');
78+
const toggled = !isTTYBefore?.value;
79+
const sawToggled = withStreamTty(process.stdout, toggled, () => process.stdout.isTTY === toggled);
80+
assert.equal(sawToggled, true);
81+
assert.deepEqual(Object.getOwnPropertyDescriptor(process.stdout, 'isTTY'), isTTYBefore);
82+
});
83+
84+
test('withStreamTtyAsync keeps the toggle across awaits and restores the original descriptor', async () => {
85+
const isTTYBefore = Object.getOwnPropertyDescriptor(process.stderr, 'isTTY');
86+
const survivedAwait = await withStreamTtyAsync(process.stderr, true, async () => {
87+
await Promise.resolve();
88+
return process.stderr.isTTY === true;
89+
});
90+
assert.equal(survivedAwait, true);
91+
assert.deepEqual(Object.getOwnPropertyDescriptor(process.stderr, 'isTTY'), isTTYBefore);
6992
});

‎src/__tests__/test-utils/stream-capture.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ function captureStream(
5050
}
5151
mutableStream.write = (chunk: unknown, ...args: unknown[]) => {
5252
if (options.passthroughBufferWrites && Buffer.isBuffer(chunk)) {
53-
return (originalWrite as (c: unknown, ...a: unknown[]) => boolean)(chunk, ...args);
53+
// Call with the stream as receiver: Writable.prototype.write reads `this._writableState`.
54+
return (
55+
originalWrite as (this: NodeJS.WriteStream, c: unknown, ...a: unknown[]) => boolean
56+
).call(stream, chunk, ...args);
5457
}
5558
output += String(chunk);
5659
return true;
@@ -128,15 +131,39 @@ export async function withCapturedOutput(
128131

129132
/** Toggles `isTTY` on a real process stream for the duration of `run`, then restores it. */
130133
export function withStreamTty<T>(stream: NodeJS.WriteStream, isTTY: boolean, run: () => T): T {
131-
const descriptor = Object.getOwnPropertyDescriptor(stream, 'isTTY');
132-
Object.defineProperty(stream, 'isTTY', { configurable: true, value: isTTY });
134+
const undo = setIsTTY(stream, isTTY);
133135
try {
134136
return run();
135137
} finally {
136-
restoreInheritedProperty(stream, 'isTTY', descriptor);
138+
undo();
137139
}
138140
}
139141

142+
/**
143+
* Async form of {@link withStreamTty}. Without it, an async `run` would resume only after the
144+
* sync form's `finally` had already restored `isTTY`, so awaited work would see the real TTY.
145+
*/
146+
export async function withStreamTtyAsync<T>(
147+
stream: NodeJS.WriteStream,
148+
isTTY: boolean,
149+
run: () => Promise<T>,
150+
): Promise<T> {
151+
const undo = setIsTTY(stream, isTTY);
152+
try {
153+
return await run();
154+
} finally {
155+
undo();
156+
}
157+
}
158+
159+
function setIsTTY(stream: NodeJS.WriteStream, isTTY: boolean): () => void {
160+
const descriptor = Object.getOwnPropertyDescriptor(stream, 'isTTY');
161+
Object.defineProperty(stream, 'isTTY', { configurable: true, value: isTTY });
162+
return () => {
163+
restoreInheritedProperty(stream, 'isTTY', descriptor);
164+
};
165+
}
166+
140167
/**
141168
* Runs `run` as if attached to an interactive terminal with CI off: `process.stderr` reports
142169
* `isTTY`, `CI` is unset, and everything `process.stderr.write` receives is captured and

0 commit comments

Comments
 (0)