Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(line reporter): wrap chinese characters correctly (#33322)
Simon Knott64802fc28451
diff --git a/packages/playwright/ThirdPartyNotices.txt b/packages/playwright/ThirdPartyNotices.txtindex f2bb64d661..2931da55c9 100644--- a/packages/playwright/ThirdPartyNotices.txt+++ b/packages/playwright/ThirdPartyNotices.txt@@ -112,6 +112,7 @@ This project incorporates components from the projects listed below. The origina- [email protected] (https://github.com/sindresorhus/escape-string-regexp)- [email protected] (https://github.com/jonschlinkert/fill-range)- [email protected] (https://github.com/loganfsmyth/gensync)+- [email protected] (https://github.com/sindresorhus/get-east-asian-width)- [email protected] (https://github.com/gulpjs/glob-parent)- [email protected] (https://github.com/sindresorhus/globals)- [email protected] (https://github.com/isaacs/node-graceful-fs)@@ -3410,6 +3411,20 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI=========================================END OF [email protected] AND INFORMATION+%% [email protected] NOTICES AND INFORMATION BEGIN HERE+=========================================+MIT License++Copyright (c) Sindre Sorhus <[email protected]> (https://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 [email protected] AND INFORMATION+%% [email protected] NOTICES AND INFORMATION BEGIN HERE=========================================The ISC License@@ -4399,6 +4414,6 @@ END OF [email protected] AND INFORMATIONSUMMARY BEGIN HERE=========================================-Total Packages: 151+Total Packages: 152=========================================END OF SUMMARY\ No newline at end of filediff --git a/packages/playwright/bundles/utils/package-lock.json b/packages/playwright/bundles/utils/package-lock.jsonindex fcf9f972fe..5fd1392d33 100644--- a/packages/playwright/bundles/utils/package-lock.json+++ b/packages/playwright/bundles/utils/package-lock.json@@ -10,6 +10,7 @@"dependencies": {"chokidar": "3.6.0","enquirer": "2.3.6",+ "get-east-asian-width": "1.3.0","json5": "2.2.3","pirates": "4.0.4","source-map-support": "0.5.21",@@ -146,6 +147,18 @@"node": "^8.16.0 || ^10.6.0 || >=11.0.0"}},+ "node_modules/get-east-asian-width": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz",+ "integrity": "sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ==",+ "license": "MIT",+ "engines": {+ "node": ">=18"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },"node_modules/glob-parent": {"version": "5.1.2","resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",@@ -376,6 +389,11 @@"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==","optional": true},+ "get-east-asian-width": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz",+ "integrity": "sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ=="+ },"glob-parent": {"version": "5.1.2","resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",diff --git a/packages/playwright/bundles/utils/package.json b/packages/playwright/bundles/utils/package.jsonindex 69477909c5..3fb09c1d17 100644--- a/packages/playwright/bundles/utils/package.json+++ b/packages/playwright/bundles/utils/package.json@@ -11,6 +11,7 @@"dependencies": {"chokidar": "3.6.0","enquirer": "2.3.6",+ "get-east-asian-width": "1.3.0","json5": "2.2.3","pirates": "4.0.4","source-map-support": "0.5.21",diff --git a/packages/playwright/bundles/utils/src/utilsBundleImpl.ts b/packages/playwright/bundles/utils/src/utilsBundleImpl.tsindex 7c29c301a8..6cd35e2885 100644--- a/packages/playwright/bundles/utils/src/utilsBundleImpl.ts+++ b/packages/playwright/bundles/utils/src/utilsBundleImpl.ts@@ -31,3 +31,6 @@ export const enquirer = enquirerLibrary;import chokidarLibrary from 'chokidar';export const chokidar = chokidarLibrary;++import * as getEastAsianWidthLibrary from 'get-east-asian-width';+export const getEastAsianWidth = getEastAsianWidthLibrary;diff --git a/packages/playwright/src/reporters/base.ts b/packages/playwright/src/reporters/base.tsindex 9bb5da81f6..3e452d6c44 100644--- a/packages/playwright/src/reporters/base.ts+++ b/packages/playwright/src/reporters/base.ts@@ -18,6 +18,7 @@ import { colors as realColors, ms as milliseconds, parseStackTraceLine } from 'pimport path from 'path';import type { FullConfig, TestCase, Suite, TestResult, TestError, FullResult, TestStep, Location } from '../../types/testReporter';import { getPackageManagerExecCommand } from 'playwright-core/lib/utils';+import { getEastAsianWidth } from '../utilsBundle';import type { ReporterV2 } from './reporterV2';import { resolveReporterOutputPath } from '../util';export type TestResultOutput = { chunk: string | Buffer, type: 'stdout' | 'stderr' };@@ -490,11 +491,35 @@ export function stripAnsiEscapes(str: string): string {return str.replace(ansiRegex, '');}+function characterWidth(c: string) {+ return getEastAsianWidth.eastAsianWidth(c.codePointAt(0)!);+}++function stringWidth(v: string) {+ let width = 0;+ for (const { segment } of new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(v))+ width += characterWidth(segment);+ return width;+}++function suffixOfWidth(v: string, width: number) {+ const segments = [...new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(v)];+ let suffixBegin = v.length;+ for (const { segment, index } of segments.reverse()) {+ const segmentWidth = stringWidth(segment);+ if (segmentWidth > width)+ break;+ width -= segmentWidth;+ suffixBegin = index;+ }+ return v.substring(suffixBegin);+}+// Leaves enough space for the "prefix" to also fit.-function fitToWidth(line: string, width: number, prefix?: string): string {+export function fitToWidth(line: string, width: number, prefix?: string): string {const prefixLength = prefix ? stripAnsiEscapes(prefix).length : 0;width -= prefixLength;- if (line.length <= width)+ if (stringWidth(line) <= width)return line;// Even items are plain text, odd items are control sequences.@@ -505,13 +530,14 @@ function fitToWidth(line: string, width: number, prefix?: string): string {// Include all control sequences to preserve formatting.taken.push(parts[i]);} else {- let part = parts[i].substring(parts[i].length - width);- if (part.length < parts[i].length && part.length > 0) {+ let part = suffixOfWidth(parts[i], width);+ const wasTruncated = part.length < parts[i].length;+ if (wasTruncated && parts[i].length > 0) {// Add ellipsis if we are truncating.- part = '\u2026' + part.substring(1);+ part = '\u2026' + suffixOfWidth(parts[i], width - 1);}taken.push(part);- width -= part.length;+ width -= stringWidth(part);}}return taken.reverse().join('');diff --git a/packages/playwright/src/utilsBundle.ts b/packages/playwright/src/utilsBundle.tsindex eec0cf9a5a..d0e2a37e77 100644--- a/packages/playwright/src/utilsBundle.ts+++ b/packages/playwright/src/utilsBundle.ts@@ -20,4 +20,5 @@ export const sourceMapSupport: typeof import('../bundles/utils/node_modules/@typexport const stoppable: typeof import('../bundles/utils/node_modules/@types/stoppable') = require('./utilsBundleImpl').stoppable;export const enquirer: typeof import('../bundles/utils/node_modules/enquirer') = require('./utilsBundleImpl').enquirer;export const chokidar: typeof import('../bundles/utils/node_modules/chokidar') = require('./utilsBundleImpl').chokidar;+export const getEastAsianWidth: typeof import('../bundles/utils/node_modules/get-east-asian-width') = require('./utilsBundleImpl').getEastAsianWidth;export type { RawSourceMap } from '../bundles/utils/node_modules/source-map';diff --git a/tests/playwright-test/fit-to-width.spec.ts b/tests/playwright-test/fit-to-width.spec.tsnew file mode 100644index 0000000000..94e73cb381--- /dev/null+++ b/tests/playwright-test/fit-to-width.spec.ts@@ -0,0 +1,32 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { fitToWidth } from 'packages/playwright/lib/reporters/base';+import { test, expect } from './playwright-test-fixtures';++test('chinese characters', () => {+ expect(fitToWidth('你你好', 3)).toBe('…好');+ expect(fitToWidth('你好你好', 4)).toBe('…好');+});++test('surrogate pairs', () => {+ expect(fitToWidth('🫣🤗', 2)).toBe('…');+ expect(fitToWidth('🫣🤗', 3)).toBe('…🤗');+ expect(fitToWidth('🚄🚄', 1)).toBe('…');+ expect(fitToWidth('🚄🚄', 2)).toBe('…');+ expect(fitToWidth('🚄🚄', 3)).toBe('…🚄');+ expect(fitToWidth('🚄🚄', 4)).toBe('🚄🚄');+});