mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix: support hasColors and getColorDepth in worker (#15590)
This commit is contained in:
parent
3939b9f36e
commit
a6daf600a9
@ -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 });
|
||||
|
||||
@ -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 = {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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`);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user