From 13d9587bcb33c3e631ee5c24572e2d39d5a4f77c Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Fri, 8 Mar 2024 18:12:51 -0800 Subject: [PATCH] fix: tty.WriteStream method stubs for process.stdout/stderr --- packages/playwright/src/common/process.ts | 22 ++++++++++++++++++ tests/playwright-test/stdio.spec.ts | 27 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/packages/playwright/src/common/process.ts b/packages/playwright/src/common/process.ts index f082e958c2..23de620649 100644 --- a/packages/playwright/src/common/process.ts +++ b/packages/playwright/src/common/process.ts @@ -131,4 +131,26 @@ function setTtyParams(stream: WriteStream, params: TtyParams) { count = 16; return count <= 2 ** params.colorDepth; })as any; + + // Stubs for the rest of the methods to avoid exceptions in user code. + stream.clearLine = (dir: any, callback?: () => void) => { + callback?.(); + return true; + }; + stream.clearScreenDown = (callback?: () => void) => { + callback?.(); + return true; + }; + (stream as any).cursorTo = (x: number, y?: number | (() => void), callback?: () => void) => { + if (callback) + callback(); + else if (y instanceof Function) + y(); + return true; + }; + stream.moveCursor = (dx: number, dy: number, callback?: () => void) => { + callback?.(); + return true; + }; + stream.getWindowSize = () => [stream.columns, stream.rows]; } diff --git a/tests/playwright-test/stdio.spec.ts b/tests/playwright-test/stdio.spec.ts index 251cf527bb..e0609b1ac6 100644 --- a/tests/playwright-test/stdio.spec.ts +++ b/tests/playwright-test/stdio.spec.ts @@ -128,3 +128,30 @@ test('should not throw type error when using assert', async ({ runInlineTest }) expect(result.output).not.toContain(`TypeError: process.stderr.hasColors is not a function`); expect(result.output).toContain(`AssertionError`); }); + +test('should provide stubs for tty.WriteStream methods on process.stdout/stderr', async ({ runInlineTest }) => { + test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29839' }); + const result = await runInlineTest({ + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + import type * as tty from 'tty'; + async function checkMethods(stream: tty.WriteStream) { + expect(stream.isTTY).toBe(true); + await new Promise(r => stream.clearLine(-1, r));; + await new Promise(r => stream.clearScreenDown(r));; + await new Promise(r => stream.cursorTo(0, 0, r));; + await new Promise(r => stream.cursorTo(0, r));; + await new Promise(r => stream.moveCursor(1, 1, r));; + expect(stream.getWindowSize()).toEqual([stream.columns, stream.rows]); + // getColorDepth() and hasColors() are covered in other tests. + } + test('process.stdout implementd tty.WriteStream methods', () => { + checkMethods(process.stdout); + }); + test('process.stderr implementd tty.WriteStream methods', () => { + checkMethods(process.stderr); + }); + ` + }); + expect(result.exitCode).toBe(0); +});