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,
|
repeatEachIndex: testGroup.repeatEachIndex,
|
||||||
projectIndex: testGroup.projectIndex,
|
projectIndex: testGroup.projectIndex,
|
||||||
loader: loaderData,
|
loader: loaderData,
|
||||||
stdoutDimension: {
|
stdoutParams: {
|
||||||
rows: process.stdout.rows,
|
rows: process.stdout.rows,
|
||||||
columns: process.stdout.columns
|
columns: process.stdout.columns,
|
||||||
|
colorDepth: process.stdout.getColorDepth?.() || 8
|
||||||
},
|
},
|
||||||
stderrDimension: {
|
stderrParams: {
|
||||||
rows: process.stderr.rows,
|
rows: process.stderr.rows,
|
||||||
columns: process.stderr.columns
|
columns: process.stderr.columns,
|
||||||
|
colorDepth: process.stderr.getColorDepth?.() || 8
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
this.send({ method: 'init', params });
|
this.send({ method: 'init', params });
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,10 @@ export type SerializedLoaderData = {
|
||||||
configCLIOverrides: ConfigCLIOverrides;
|
configCLIOverrides: ConfigCLIOverrides;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TtyDimension = {
|
export type TtyParams = {
|
||||||
rows: number | undefined;
|
rows: number | undefined;
|
||||||
columns: number | undefined;
|
columns: number | undefined;
|
||||||
|
colorDepth: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WorkerInitParams = {
|
export type WorkerInitParams = {
|
||||||
|
|
@ -35,8 +36,8 @@ export type WorkerInitParams = {
|
||||||
repeatEachIndex: number;
|
repeatEachIndex: number;
|
||||||
projectIndex: number;
|
projectIndex: number;
|
||||||
loader: SerializedLoaderData;
|
loader: SerializedLoaderData;
|
||||||
stdoutDimension: TtyDimension;
|
stdoutParams: TtyParams;
|
||||||
stderrDimension: TtyDimension;
|
stderrParams: TtyParams;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TestBeginPayload = {
|
export type TestBeginPayload = {
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,9 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import type { WriteStream } from 'tty';
|
||||||
import * as util from 'util';
|
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 { startProfiling, stopProfiling } from './profiler';
|
||||||
import { serializeError } from './util';
|
import { serializeError } from './util';
|
||||||
import { WorkerRunner } from './workerRunner';
|
import { WorkerRunner } from './workerRunner';
|
||||||
|
|
@ -124,14 +125,21 @@ function chunkToParams(chunk: Buffer | string): { text?: string, buffer?: strin
|
||||||
|
|
||||||
function initConsoleParameters(initParams: WorkerInitParams) {
|
function initConsoleParameters(initParams: WorkerInitParams) {
|
||||||
// Make sure the output supports colors.
|
// Make sure the output supports colors.
|
||||||
process.stdout.isTTY = true;
|
setTtyParams(process.stdout, initParams.stdoutParams);
|
||||||
process.stderr.isTTY = true;
|
setTtyParams(process.stderr, initParams.stderrParams);
|
||||||
if (initParams.stdoutDimension.rows)
|
}
|
||||||
process.stdout.rows = initParams.stdoutDimension.rows;
|
|
||||||
if (initParams.stdoutDimension.columns)
|
function setTtyParams(stream: WriteStream, params: TtyParams) {
|
||||||
process.stdout.columns = initParams.stdoutDimension.columns;
|
stream.isTTY = true;
|
||||||
if (initParams.stderrDimension.rows)
|
if (params.rows)
|
||||||
process.stderr.rows = initParams.stderrDimension.rows;
|
stream.rows = params.rows;
|
||||||
if (initParams.stderrDimension.columns)
|
if (params.columns)
|
||||||
process.stderr.columns = initParams.stderrDimension.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[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 }`);
|
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…
Reference in a new issue