From ded909c13f354b1dc626cdb9d96bb734c34a481a Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 12 Feb 2025 21:13:25 -0800 Subject: [PATCH] chrome: restore colors enabled/levels logic (#34767) --- .../playwright-core/ThirdPartyNotices.txt | 33 ++++++++++- .../bundles/utils/package-lock.json | 10 ++++ .../bundles/utils/package.json | 1 + .../bundles/utils/src/utilsBundleImpl.ts | 3 + .../playwright-core/src/client/connection.ts | 2 +- .../playwright-core/src/common/platform.ts | 10 ++++ .../src/server/registry/browserFetcher.ts | 3 +- .../src/server/utils/comparators.ts | 3 +- .../src/server/utils/nodePlatform.ts | 3 + packages/playwright-core/src/utils.ts | 3 + .../src/utils/isomorphic/colors.ts | 55 +------------------ .../src/utils/isomorphic/stackTrace.ts | 6 +- packages/playwright-core/src/utilsBundle.ts | 1 + packages/playwright/src/reporters/base.ts | 4 +- packages/playwright/src/util.ts | 3 +- 15 files changed, 75 insertions(+), 65 deletions(-) diff --git a/packages/playwright-core/ThirdPartyNotices.txt b/packages/playwright-core/ThirdPartyNotices.txt index f9d2225d75..420d698fa5 100644 --- a/packages/playwright-core/ThirdPartyNotices.txt +++ b/packages/playwright-core/ThirdPartyNotices.txt @@ -11,6 +11,7 @@ This project incorporates components from the projects listed below. The origina - brace-expansion@1.1.11 (https://github.com/juliangruber/brace-expansion) - buffer-crc32@0.2.13 (https://github.com/brianloveswords/buffer-crc32) - codemirror@5.65.18 (https://github.com/codemirror/CodeMirror) +- colors@1.4.0 (https://github.com/Marak/colors.js) - commander@8.3.0 (https://github.com/tj/commander.js) - concat-map@0.0.1 (https://github.com/substack/node-concat-map) - debug@4.3.4 (https://github.com/debug-js/debug) @@ -354,6 +355,36 @@ THE SOFTWARE. ========================================= END OF codemirror@5.65.18 AND INFORMATION +%% colors@1.4.0 NOTICES AND INFORMATION BEGIN HERE +========================================= +MIT License + +Original Library + - Copyright (c) Marak Squires + +Additional Functionality + - Copyright (c) Sindre Sorhus (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +========================================= +END OF colors@1.4.0 AND INFORMATION + %% commander@8.3.0 NOTICES AND INFORMATION BEGIN HERE ========================================= (The MIT License) @@ -1524,6 +1555,6 @@ END OF yazl@2.5.1 AND INFORMATION SUMMARY BEGIN HERE ========================================= -Total Packages: 45 +Total Packages: 46 ========================================= END OF SUMMARY \ No newline at end of file diff --git a/packages/playwright-core/bundles/utils/package-lock.json b/packages/playwright-core/bundles/utils/package-lock.json index a70731aa0e..bc58353c99 100644 --- a/packages/playwright-core/bundles/utils/package-lock.json +++ b/packages/playwright-core/bundles/utils/package-lock.json @@ -8,6 +8,7 @@ "name": "utils-bundle", "version": "0.0.1", "dependencies": { + "colors": "1.4.0", "commander": "8.3.0", "debug": "^4.3.4", "diff": "^7.0.0", @@ -162,6 +163,15 @@ "concat-map": "0.0.1" } }, + "node_modules/colors": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==", + "license": "MIT", + "engines": { + "node": ">=0.1.90" + } + }, "node_modules/commander": { "version": "8.3.0", "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", diff --git a/packages/playwright-core/bundles/utils/package.json b/packages/playwright-core/bundles/utils/package.json index 76f6a4986a..e7f09cf3a0 100644 --- a/packages/playwright-core/bundles/utils/package.json +++ b/packages/playwright-core/bundles/utils/package.json @@ -9,6 +9,7 @@ "generate-license": "node ../../../../utils/generate_third_party_notice.js" }, "dependencies": { + "colors": "1.4.0", "commander": "8.3.0", "debug": "^4.3.4", "diff": "^7.0.0", diff --git a/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts b/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts index 284ca5e7ad..409372e894 100644 --- a/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts +++ b/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts @@ -16,6 +16,9 @@ /* eslint-disable import/order */ +import colorsLibrary from 'colors/safe'; +export const colors = colorsLibrary; + import debugLibrary from 'debug'; export const debug = debugLibrary; diff --git a/packages/playwright-core/src/client/connection.ts b/packages/playwright-core/src/client/connection.ts index 124fcc15d5..e37c949f1f 100644 --- a/packages/playwright-core/src/client/connection.ts +++ b/packages/playwright-core/src/client/connection.ts @@ -166,7 +166,7 @@ export class Connection extends EventEmitter { this._callbacks.delete(id); if (error && !result) { const parsedError = parseError(error); - rewriteErrorMessage(parsedError, parsedError.message + formatCallLog(log)); + rewriteErrorMessage(parsedError, parsedError.message + formatCallLog(this.platform, log)); callback.reject(parsedError); } else { const validator = findValidator(callback.type, callback.method, 'Result'); diff --git a/packages/playwright-core/src/common/platform.ts b/packages/playwright-core/src/common/platform.ts index 390dbf80ff..64cef77aab 100644 --- a/packages/playwright-core/src/common/platform.ts +++ b/packages/playwright-core/src/common/platform.ts @@ -18,8 +18,14 @@ import * as crypto from 'crypto'; import * as fs from 'fs'; import * as path from 'path'; +import { webColors, noColors } from '../utils/isomorphic/colors'; + +import type { Colors } from '../utils/isomorphic/colors'; + + export type Platform = { calculateSha1(text: string): Promise; + colors: Colors; createGuid: () => string; fs: () => typeof fs; inspectCustom: symbol | undefined; @@ -37,6 +43,8 @@ export const webPlatform: Platform = { return Array.from(new Uint8Array(hashBuffer), b => b.toString(16).padStart(2, '0')).join(''); }, + colors: webColors, + createGuid: () => { return Array.from(crypto.getRandomValues(new Uint8Array(16)), b => b.toString(16).padStart(2, '0')).join(''); }, @@ -68,6 +76,8 @@ export const emptyPlatform: Platform = { throw new Error('Not implemented'); }, + colors: noColors, + createGuid: () => { throw new Error('Not implemented'); }, diff --git a/packages/playwright-core/src/server/registry/browserFetcher.ts b/packages/playwright-core/src/server/registry/browserFetcher.ts index fb6afb3f10..6a84f2573a 100644 --- a/packages/playwright-core/src/server/registry/browserFetcher.ts +++ b/packages/playwright-core/src/server/registry/browserFetcher.ts @@ -23,8 +23,7 @@ import * as path from 'path'; import { debugLogger } from '../utils/debugLogger'; import { ManualPromise } from '../../utils/isomorphic/manualPromise'; import { getUserAgent } from '../utils/userAgent'; -import { progress as ProgressBar } from '../../utilsBundle'; -import { colors } from '../../utils/isomorphic/colors'; +import { progress as ProgressBar, colors } from '../../utilsBundle'; import { existsAsync } from '../utils/fileUtils'; import { browserDirectoryToMarkerFilePath } from '.'; diff --git a/packages/playwright-core/src/server/utils/comparators.ts b/packages/playwright-core/src/server/utils/comparators.ts index 19b343cc03..06b6bdc6ff 100644 --- a/packages/playwright-core/src/server/utils/comparators.ts +++ b/packages/playwright-core/src/server/utils/comparators.ts @@ -19,8 +19,7 @@ import { compare } from './image_tools/compare'; // @ts-ignore import pixelmatch from '../../third_party/pixelmatch'; import { jpegjs } from '../../utilsBundle'; -import { colors } from '../../utils/isomorphic/colors'; -import { diff } from '../../utilsBundle'; +import { colors, diff } from '../../utilsBundle'; import { PNG } from '../../utilsBundle'; export type ImageComparatorOptions = { threshold?: number, maxDiffPixels?: number, maxDiffPixelRatio?: number, comparator?: string }; diff --git a/packages/playwright-core/src/server/utils/nodePlatform.ts b/packages/playwright-core/src/server/utils/nodePlatform.ts index 1a6f2ddc25..d4b8b5d7ad 100644 --- a/packages/playwright-core/src/server/utils/nodePlatform.ts +++ b/packages/playwright-core/src/server/utils/nodePlatform.ts @@ -19,6 +19,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as util from 'util'; +import { colors } from '../../utilsBundle'; import { Platform } from '../../common/platform'; import { debugLogger } from './debugLogger'; @@ -29,6 +30,8 @@ export const nodePlatform: Platform = { return Promise.resolve(sha1.digest('hex')); }, + colors, + createGuid: () => crypto.randomBytes(16).toString('hex'), fs: () => fs, diff --git a/packages/playwright-core/src/utils.ts b/packages/playwright-core/src/utils.ts index b4829cb78c..1a0c446c6b 100644 --- a/packages/playwright-core/src/utils.ts +++ b/packages/playwright-core/src/utils.ts @@ -44,9 +44,12 @@ export * from './server/utils/fileUtils'; export * from './server/utils/hostPlatform'; export * from './server/utils/httpServer'; export * from './server/utils/network'; +export * from './server/utils/nodePlatform'; export * from './server/utils/processLauncher'; export * from './server/utils/profiler'; export * from './server/utils/socksProxy'; export * from './server/utils/spawnAsync'; export * from './server/utils/userAgent'; export * from './server/utils/wsServer'; + +export { colors } from './utilsBundle'; diff --git a/packages/playwright-core/src/utils/isomorphic/colors.ts b/packages/playwright-core/src/utils/isomorphic/colors.ts index 93ed823628..7be0955ad3 100644 --- a/packages/playwright-core/src/utils/isomorphic/colors.ts +++ b/packages/playwright-core/src/utils/isomorphic/colors.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export const colors = { +export const webColors = { enabled: true, reset: (text: string) => applyStyle(0, 0, text), @@ -36,36 +36,9 @@ export const colors = { white: (text: string) => applyStyle(37, 39, text), gray: (text: string) => applyStyle(90, 39, text), grey: (text: string) => applyStyle(90, 39, text), - - brightRed: (text: string) => applyStyle(91, 39, text), - brightGreen: (text: string) => applyStyle(92, 39, text), - brightYellow: (text: string) => applyStyle(93, 39, text), - brightBlue: (text: string) => applyStyle(94, 39, text), - brightMagenta: (text: string) => applyStyle(95, 39, text), - brightCyan: (text: string) => applyStyle(96, 39, text), - brightWhite: (text: string) => applyStyle(97, 39, text), - - bgBlack: (text: string) => applyStyle(40, 49, text), - bgRed: (text: string) => applyStyle(41, 49, text), - bgGreen: (text: string) => applyStyle(42, 49, text), - bgYellow: (text: string) => applyStyle(43, 49, text), - bgBlue: (text: string) => applyStyle(44, 49, text), - bgMagenta: (text: string) => applyStyle(45, 49, text), - bgCyan: (text: string) => applyStyle(46, 49, text), - bgWhite: (text: string) => applyStyle(47, 49, text), - bgGray: (text: string) => applyStyle(100, 49, text), - bgGrey: (text: string) => applyStyle(100, 49, text), - - bgBrightRed: (text: string) => applyStyle(101, 49, text), - bgBrightGreen: (text: string) => applyStyle(102, 49, text), - bgBrightYellow: (text: string) => applyStyle(103, 49, text), - bgBrightBlue: (text: string) => applyStyle(104, 49, text), - bgBrightMagenta: (text: string) => applyStyle(105, 49, text), - bgBrightCyan: (text: string) => applyStyle(106, 49, text), - bgBrightWhite: (text: string) => applyStyle(107, 49, text), }; -type Colors = typeof colors; +export type Colors = typeof webColors; export const noColors: Colors = { enabled: false, @@ -87,30 +60,6 @@ export const noColors: Colors = { white: t => t, gray: t => t, grey: t => t, - brightRed: t => t, - brightGreen: t => t, - brightYellow: t => t, - brightBlue: t => t, - brightMagenta: t => t, - brightCyan: t => t, - brightWhite: t => t, - bgBlack: t => t, - bgRed: t => t, - bgGreen: t => t, - bgYellow: t => t, - bgBlue: t => t, - bgMagenta: t => t, - bgCyan: t => t, - bgWhite: t => t, - bgGray: t => t, - bgGrey: t => t, - bgBrightRed: t => t, - bgBrightGreen: t => t, - bgBrightYellow: t => t, - bgBrightBlue: t => t, - bgBrightMagenta: t => t, - bgBrightCyan: t => t, - bgBrightWhite: t => t }; diff --git a/packages/playwright-core/src/utils/isomorphic/stackTrace.ts b/packages/playwright-core/src/utils/isomorphic/stackTrace.ts index 7ce3f719a4..26b79c5c27 100644 --- a/packages/playwright-core/src/utils/isomorphic/stackTrace.ts +++ b/packages/playwright-core/src/utils/isomorphic/stackTrace.ts @@ -14,11 +14,11 @@ * limitations under the License. */ -import { colors } from './colors'; import { findRepeatedSubsequences } from './sequence'; import { parseStackFrame } from './stackUtils'; import type { StackFrame } from '@protocol/channels'; +import type { Platform } from '../../common/platform'; export function parseStackTraceLine(line: string, pathSeparator: string): StackFrame | null { const frame = parseStackFrame(line, pathSeparator); @@ -142,12 +142,12 @@ export function splitErrorMessage(message: string): { name: string, message: str }; } -export function formatCallLog(log: string[] | undefined): string { +export function formatCallLog(platform: Platform, log: string[] | undefined): string { if (!log || !log.some(l => !!l)) return ''; return ` Call log: -${colors.dim(log.join('\n'))} +${platform.colors.dim(log.join('\n'))} `; } diff --git a/packages/playwright-core/src/utilsBundle.ts b/packages/playwright-core/src/utilsBundle.ts index a72f18c30f..41cccd1f57 100644 --- a/packages/playwright-core/src/utilsBundle.ts +++ b/packages/playwright-core/src/utilsBundle.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +export const colors: typeof import('../bundles/utils/node_modules/colors/safe') = require('./utilsBundleImpl').colors; export const debug: typeof import('../bundles/utils/node_modules/@types/debug') = require('./utilsBundleImpl').debug; export const diff: typeof import('../bundles/utils/node_modules/@types/diff') = require('./utilsBundleImpl').diff; export const dotenv: typeof import('../bundles/utils/node_modules/dotenv') = require('./utilsBundleImpl').dotenv; diff --git a/packages/playwright/src/reporters/base.ts b/packages/playwright/src/reporters/base.ts index 8ba5b23af1..1748cac9b9 100644 --- a/packages/playwright/src/reporters/base.ts +++ b/packages/playwright/src/reporters/base.ts @@ -26,11 +26,11 @@ import { getEastAsianWidth } from '../utilsBundle'; import type { ReporterV2 } from './reporterV2'; import type { FullConfig, FullResult, Location, Suite, TestCase, TestError, TestResult, TestStep } from '../../types/testReporter'; +import type { Colors } from '@isomorphic/colors'; + export type TestResultOutput = { chunk: string | Buffer, type: 'stdout' | 'stderr' }; export const kOutputSymbol = Symbol('output'); -type Colors = typeof realColors; - type ErrorDetails = { message: string; location?: Location; diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts index a890535fee..b80a3f387d 100644 --- a/packages/playwright/src/util.ts +++ b/packages/playwright/src/util.ts @@ -21,6 +21,7 @@ import util from 'util'; import { parseStackTraceLine, sanitizeForFilePath, calculateSha1, formatCallLog, isRegExp, isString, stringifyStackFrames } from 'playwright-core/lib/utils'; import { debug, mime, minimatch } from 'playwright-core/lib/utilsBundle'; +import { nodePlatform } from 'playwright-core/lib/utils'; import type { Location } from './../types/testReporter'; import type { TestInfoErrorImpl } from './common/ipc'; @@ -224,7 +225,7 @@ export function getContainedPath(parentPath: string, subPath: string = ''): stri export const debugTest = debug('pw:test'); -export const callLogText = formatCallLog; +export const callLogText = (log: string[] | undefined) => formatCallLog(nodePlatform, log); const folderToPackageJsonPath = new Map();