From 218f2ffe3546bc32ae346183616aa3954764799d Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 28 Mar 2024 17:25:27 -0700 Subject: [PATCH] Modernize 1 to 2 --- .../playwright/src/isomorphic/teleReceiver.ts | 2 - packages/playwright/src/reporters/blob.ts | 2 +- packages/playwright/src/reporters/merge.ts | 83 ++++++++++++------- tests/playwright-test/reporter-blob.spec.ts | 2 +- 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/packages/playwright/src/isomorphic/teleReceiver.ts b/packages/playwright/src/isomorphic/teleReceiver.ts index 69fa49649a..d99849c1fd 100644 --- a/packages/playwright/src/isomorphic/teleReceiver.ts +++ b/packages/playwright/src/isomorphic/teleReceiver.ts @@ -58,8 +58,6 @@ export type JsonSuite = { title: string; location?: JsonLocation; entries: (JsonSuite | JsonTestCase)[]; - suites?: JsonSuite[]; // used before v1.44 - tests?: JsonTestCase[]; // used before v1.44 }; export type JsonTestCase = { diff --git a/packages/playwright/src/reporters/blob.ts b/packages/playwright/src/reporters/blob.ts index 019fa8b7ca..1c95658f6c 100644 --- a/packages/playwright/src/reporters/blob.ts +++ b/packages/playwright/src/reporters/blob.ts @@ -32,7 +32,7 @@ type BlobReporterOptions = { fileName?: string; }; -export const currentBlobReportVersion = 1; +export const currentBlobReportVersion = 2; export type BlobReportMetadata = { version: number; diff --git a/packages/playwright/src/reporters/merge.ts b/packages/playwright/src/reporters/merge.ts index 80ab58fb36..82925710a4 100644 --- a/packages/playwright/src/reporters/merge.ts +++ b/packages/playwright/src/reporters/merge.ts @@ -27,6 +27,7 @@ import { ZipFile } from 'playwright-core/lib/utils'; import { currentBlobReportVersion, type BlobReportMetadata } from './blob'; import { relativeFilePath } from '../util'; import type { TestError } from '../../types/testReporter'; +import type * as blobV1 from './versions/blobV1'; type StatusCallback = (message: string) => void; @@ -136,15 +137,17 @@ async function extractAndParseReports(dir: string, shardFiles: string[], interna const content = await zipFile.read(entryName); if (entryName.endsWith('.jsonl')) { fileName = reportNames.makeUnique(fileName); - const parsedEvents = parseCommonEvents(content); + let parsedEvents = parseCommonEvents(content); // Passing reviver to JSON.parse doesn't work, as the original strings // keep beeing used. To work around that we traverse the parsed events // as a post-processing step. internalizer.traverse(parsedEvents); + const metadata = findMetadata(parsedEvents, file); + parsedEvents = modernizer.modernize(metadata.version, parsedEvents); shardEvents.push({ file, localPath: fileName, - metadata: findMetadata(parsedEvents, file), + metadata, parsedEvents }); } @@ -387,23 +390,12 @@ class IdsPatcher { } private _updateTestIds(suite: JsonSuite) { - if (suite.entries) { // Starting 1.44 - suite.entries.forEach(entry => { - if ('testId' in entry) - this._updateTestId(entry); - else - this._updateTestIds(entry); - }); - } else { // before 1.44 - suite.tests!.forEach(test => { - test.testId = this._mapTestId(test.testId); - if (this._botName) { - test.tags = test.tags || []; - test.tags.unshift('@' + this._botName); - } - }); - suite.suites!.forEach(suite => this._updateTestIds(suite)); - } + suite.entries.forEach(entry => { + if ('testId' in entry) + this._updateTestId(entry); + else + this._updateTestIds(entry); + }); } private _updateTestId(test: JsonTestCase) { @@ -477,18 +469,11 @@ class PathSeparatorPatcher { this._updateLocation(suite.location); if (isFileSuite) suite.title = this._updatePath(suite.title); - if (suite.entries) { // starting 1.44 - for (const entry of suite.entries) { - if ('testId' in entry) - this._updateLocation(entry.location); - else - this._updateSuite(entry); - } - } else { // before 1.44 - for (const child of suite.suites!) - this._updateSuite(child); - for (const test of suite.tests!) - this._updateLocation(test.location); + for (const entry of suite.entries) { + if ('testId' in entry) + this._updateLocation(entry.location); + else + this._updateSuite(entry); } } @@ -534,3 +519,39 @@ class JsonEventPatchers { } } } + +class BlobModernizer { + modernize(fromVersion: number, events: JsonEvent[]): JsonEvent[] { + const result = []; + for (const event of events) + result.push(...this._modernize(fromVersion, event)); + return result; + } + + private _modernize(fromVersion: number, event: JsonEvent): JsonEvent[] { + let events = [event]; + for (let version = fromVersion; version < currentBlobReportVersion; ++version) + events = (this as any)[`_modernize_${version}_to_${version + 1}`].call(this, events); + return events; + } + + _modernize_1_to_2(events: JsonEvent[]): JsonEvent[] { + return events.map(event => { + if (event.method === 'onProject') { + const modernizeSuite = (suite: blobV1.JsonSuite) => { + for (const child of suite.suites) + modernizeSuite(child); + (suite as any).entries = [...suite.suites, ...suite.tests]; + (suite as any).suites = undefined; + (suite as any).tests = undefined; + }; + const project = event.params.project as blobV1.JsonProject; + for (const suite of project.suites) + modernizeSuite(suite); + } + return event; + }); + } +} + +const modernizer = new BlobModernizer(); \ No newline at end of file diff --git a/tests/playwright-test/reporter-blob.spec.ts b/tests/playwright-test/reporter-blob.spec.ts index adf1f56ab0..4f1ee4fc0f 100644 --- a/tests/playwright-test/reporter-blob.spec.ts +++ b/tests/playwright-test/reporter-blob.spec.ts @@ -1319,7 +1319,7 @@ test('blob report should include version', async ({ runInlineTest }) => { const events = await extractReport(test.info().outputPath('blob-report', 'report.zip'), test.info().outputPath('tmp')); const metadataEvent = events.find(e => e.method === 'onBlobReportMetadata'); - expect(metadataEvent.params.version).toBe(1); + expect(metadataEvent.params.version).toBe(2); expect(metadataEvent.params.userAgent).toBe(getUserAgent()); });