From a6daf600a9fc5710a055c6354ac4478e7a4aef6d Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 12 Jul 2022 14:47:14 -0700 Subject: [PATCH] fix: support hasColors and getColorDepth in worker (#15590) --- packages/playwright-test/src/dispatcher.ts | 10 ++++--- packages/playwright-test/src/ipc.ts | 7 +++-- packages/playwright-test/src/worker.ts | 30 ++++++++++++-------- tests/playwright-test/stdio.spec.ts | 32 ++++++++++++++++++++++ 4 files changed, 61 insertions(+), 18 deletions(-) diff --git a/packages/playwright-test/src/dispatcher.ts b/packages/playwright-test/src/dispatcher.ts index 831af5b864..8f12892559 100644 --- a/packages/playwright-test/src/dispatcher.ts +++ b/packages/playwright-test/src/dispatcher.ts @@ -529,13 +529,15 @@ class Worker extends EventEmitter { repeatEachIndex: testGroup.repeatEachIndex, projectIndex: testGroup.projectIndex, loader: loaderData, - stdoutDimension: { + stdoutParams: { rows: process.stdout.rows, - columns: process.stdout.columns + columns: process.stdout.columns, + colorDepth: process.stdout.getColorDepth?.() || 8 }, - stderrDimension: { + stderrParams: { rows: process.stderr.rows, - columns: process.stderr.columns + columns: process.stderr.columns, + colorDepth: process.stderr.getColorDepth?.() || 8 }, }; this.send({ method: 'init', params }); diff --git a/packages/playwright-test/src/ipc.ts b/packages/playwright-test/src/ipc.ts index 64806f92e2..44663a377b 100644 --- a/packages/playwright-test/src/ipc.ts +++ b/packages/playwright-test/src/ipc.ts @@ -24,9 +24,10 @@ export type SerializedLoaderData = { configCLIOverrides: ConfigCLIOverrides; }; -export type TtyDimension = { +export type TtyParams = { rows: number | undefined; columns: number | undefined; + colorDepth: number; }; export type WorkerInitParams = { @@ -35,8 +36,8 @@ export type WorkerInitParams = { repeatEachIndex: number; projectIndex: number; loader: SerializedLoaderData; - stdoutDimension: TtyDimension; - stderrDimension: TtyDimension; + stdoutParams: TtyParams; + stderrParams: TtyParams; }; export type TestBeginPayload = { diff --git a/packages/playwright-test/src/worker.ts b/packages/playwright-test/src/worker.ts index 7a4b067b3e..a6d149a98f 100644 --- a/packages/playwright-test/src/worker.ts +++ b/packages/playwright-test/src/worker.ts @@ -14,8 +14,9 @@ * limitations under the License. */ +import type { WriteStream } from 'tty'; import * as util from 'util'; -import type { RunPayload, TeardownErrorsPayload, TestOutputPayload, WorkerInitParams } from './ipc'; +import type { RunPayload, TeardownErrorsPayload, TestOutputPayload, TtyParams, WorkerInitParams } from './ipc'; import { startProfiling, stopProfiling } from './profiler'; import { serializeError } from './util'; import { WorkerRunner } from './workerRunner'; @@ -124,14 +125,21 @@ function chunkToParams(chunk: Buffer | string): { text?: string, buffer?: strin function initConsoleParameters(initParams: WorkerInitParams) { // Make sure the output supports colors. - process.stdout.isTTY = true; - process.stderr.isTTY = true; - if (initParams.stdoutDimension.rows) - process.stdout.rows = initParams.stdoutDimension.rows; - if (initParams.stdoutDimension.columns) - process.stdout.columns = initParams.stdoutDimension.columns; - if (initParams.stderrDimension.rows) - process.stderr.rows = initParams.stderrDimension.rows; - if (initParams.stderrDimension.columns) - process.stderr.columns = initParams.stderrDimension.columns; + setTtyParams(process.stdout, initParams.stdoutParams); + setTtyParams(process.stderr, initParams.stderrParams); +} + +function setTtyParams(stream: WriteStream, params: TtyParams) { + stream.isTTY = true; + if (params.rows) + stream.rows = params.rows; + if (params.columns) + stream.columns = params.columns; + stream.getColorDepth = () => params.colorDepth; + stream.hasColors = ((count = 16) => { + // count is optional and the first argument may actually be env. + if (typeof count !== 'number') + count = 16; + return count <= 2 ** params.colorDepth; + })as any; } diff --git a/tests/playwright-test/stdio.spec.ts b/tests/playwright-test/stdio.spec.ts index 14af8015d3..7f67953030 100644 --- a/tests/playwright-test/stdio.spec.ts +++ b/tests/playwright-test/stdio.spec.ts @@ -95,3 +95,35 @@ test('should support console colors', async ({ runInlineTest }) => { expect(result.output).toContain(`{ b: \x1b[33mtrue\x1b[39m, n: \x1b[33m123\x1b[39m, s: \x1b[32m'abc'\x1b[39m }`); expect(result.output).toContain(`{ b: \x1b[33mfalse\x1b[39m, n: \x1b[33m123\x1b[39m, s: \x1b[32m'abc'\x1b[39m }`); }); + +test('should override hasColors and getColorDepth', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.spec.js': ` + const { test } = pwt; + test('console log', () => { + console.log('process.stdout.hasColors(1) = ' + process.stdout.hasColors(1)); + console.log('process.stderr.hasColors(1) = ' + process.stderr.hasColors(1)); + console.log('process.stdout.getColorDepth() > 0 = ' + (process.stdout.getColorDepth() > 0)); + console.log('process.stderr.getColorDepth() > 0 = ' + (process.stderr.getColorDepth() > 0)); + }); + ` + }); + expect(result.output).toContain(`process.stdout.hasColors(1) = true`); + expect(result.output).toContain(`process.stderr.hasColors(1) = true`); + expect(result.output).toContain(`process.stdout.getColorDepth() > 0 = true`); + expect(result.output).toContain(`process.stderr.getColorDepth() > 0 = true`); +}); + +test('should not throw type error when using assert', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.spec.js': ` + const { test } = pwt; + const assert = require('assert'); + test('assert no type error', () => { + assert.strictEqual(1, 2); + }); + ` + }); + expect(result.output).not.toContain(`TypeError: process.stderr.hasColors is not a function`); + expect(result.output).toContain(`AssertionError`); +});