merge.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var merge_exports = {};
  30. __export(merge_exports, {
  31. createMergedReport: () => createMergedReport
  32. });
  33. module.exports = __toCommonJS(merge_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_path = __toESM(require("path"));
  36. var import_utils = require("playwright-core/lib/utils");
  37. var import_blob = require("./blob");
  38. var import_multiplexer = require("./multiplexer");
  39. var import_stringInternPool = require("../isomorphic/stringInternPool");
  40. var import_teleReceiver = require("../isomorphic/teleReceiver");
  41. var import_reporters = require("../runner/reporters");
  42. var import_util = require("../util");
  43. async function createMergedReport(config, dir, reporterDescriptions, rootDirOverride) {
  44. const reporters = await (0, import_reporters.createReporters)(config, "merge", false, reporterDescriptions);
  45. const multiplexer = new import_multiplexer.Multiplexer(reporters);
  46. const stringPool = new import_stringInternPool.StringInternPool();
  47. let printStatus = () => {
  48. };
  49. if (!multiplexer.printsToStdio()) {
  50. printStatus = printStatusToStdout;
  51. printStatus(`merging reports from ${dir}`);
  52. }
  53. const shardFiles = await sortedShardFiles(dir);
  54. if (shardFiles.length === 0)
  55. throw new Error(`No report files found in ${dir}`);
  56. const eventData = await mergeEvents(dir, shardFiles, stringPool, printStatus, rootDirOverride);
  57. const pathSeparator = rootDirOverride ? import_path.default.sep : eventData.pathSeparatorFromMetadata ?? import_path.default.sep;
  58. const receiver = new import_teleReceiver.TeleReporterReceiver(multiplexer, {
  59. mergeProjects: false,
  60. mergeTestCases: false,
  61. resolvePath: (rootDir, relativePath) => stringPool.internString(rootDir + pathSeparator + relativePath),
  62. configOverrides: config.config
  63. });
  64. printStatus(`processing test events`);
  65. const dispatchEvents = async (events) => {
  66. for (const event of events) {
  67. if (event.method === "onEnd")
  68. printStatus(`building final report`);
  69. await receiver.dispatch(event);
  70. if (event.method === "onEnd")
  71. printStatus(`finished building report`);
  72. }
  73. };
  74. await dispatchEvents(eventData.prologue);
  75. for (const { reportFile, eventPatchers, metadata } of eventData.reports) {
  76. const reportJsonl = await import_fs.default.promises.readFile(reportFile);
  77. const events = parseTestEvents(reportJsonl);
  78. new import_stringInternPool.JsonStringInternalizer(stringPool).traverse(events);
  79. eventPatchers.patchers.push(new AttachmentPathPatcher(dir));
  80. if (metadata.name)
  81. eventPatchers.patchers.push(new GlobalErrorPatcher(metadata.name));
  82. eventPatchers.patchEvents(events);
  83. await dispatchEvents(events);
  84. }
  85. await dispatchEvents(eventData.epilogue);
  86. }
  87. const commonEventNames = ["onBlobReportMetadata", "onConfigure", "onProject", "onBegin", "onEnd"];
  88. const commonEvents = new Set(commonEventNames);
  89. const commonEventRegex = new RegExp(`${commonEventNames.join("|")}`);
  90. function parseCommonEvents(reportJsonl) {
  91. return splitBufferLines(reportJsonl).map((line) => line.toString("utf8")).filter((line) => commonEventRegex.test(line)).map((line) => JSON.parse(line)).filter((event) => commonEvents.has(event.method));
  92. }
  93. function parseTestEvents(reportJsonl) {
  94. return splitBufferLines(reportJsonl).map((line) => line.toString("utf8")).filter((line) => line.length).map((line) => JSON.parse(line)).filter((event) => !commonEvents.has(event.method));
  95. }
  96. function splitBufferLines(buffer) {
  97. const lines = [];
  98. let start = 0;
  99. while (start < buffer.length) {
  100. const end = buffer.indexOf(10, start);
  101. if (end === -1) {
  102. lines.push(buffer.slice(start));
  103. break;
  104. }
  105. lines.push(buffer.slice(start, end));
  106. start = end + 1;
  107. }
  108. return lines;
  109. }
  110. async function extractAndParseReports(dir, shardFiles, internalizer, printStatus) {
  111. const shardEvents = [];
  112. await import_fs.default.promises.mkdir(import_path.default.join(dir, "resources"), { recursive: true });
  113. const reportNames = new UniqueFileNameGenerator();
  114. for (const file of shardFiles) {
  115. const absolutePath = import_path.default.join(dir, file);
  116. printStatus(`extracting: ${(0, import_util.relativeFilePath)(absolutePath)}`);
  117. const zipFile = new import_utils.ZipFile(absolutePath);
  118. const entryNames = await zipFile.entries();
  119. for (const entryName of entryNames.sort()) {
  120. let fileName = import_path.default.join(dir, entryName);
  121. const content = await zipFile.read(entryName);
  122. if (entryName.endsWith(".jsonl")) {
  123. fileName = reportNames.makeUnique(fileName);
  124. let parsedEvents = parseCommonEvents(content);
  125. internalizer.traverse(parsedEvents);
  126. const metadata = findMetadata(parsedEvents, file);
  127. parsedEvents = modernizer.modernize(metadata.version, parsedEvents);
  128. shardEvents.push({
  129. file,
  130. localPath: fileName,
  131. metadata,
  132. parsedEvents
  133. });
  134. }
  135. await import_fs.default.promises.writeFile(fileName, content);
  136. }
  137. zipFile.close();
  138. }
  139. return shardEvents;
  140. }
  141. function findMetadata(events, file) {
  142. if (events[0]?.method !== "onBlobReportMetadata")
  143. throw new Error(`No metadata event found in ${file}`);
  144. const metadata = events[0].params;
  145. if (metadata.version > import_blob.currentBlobReportVersion)
  146. throw new Error(`Blob report ${file} was created with a newer version of Playwright.`);
  147. return metadata;
  148. }
  149. async function mergeEvents(dir, shardReportFiles, stringPool, printStatus, rootDirOverride) {
  150. const internalizer = new import_stringInternPool.JsonStringInternalizer(stringPool);
  151. const configureEvents = [];
  152. const projectEvents = [];
  153. const endEvents = [];
  154. const blobs = await extractAndParseReports(dir, shardReportFiles, internalizer, printStatus);
  155. blobs.sort((a, b) => {
  156. const nameA = a.metadata.name ?? "";
  157. const nameB = b.metadata.name ?? "";
  158. if (nameA !== nameB)
  159. return nameA.localeCompare(nameB);
  160. const shardA = a.metadata.shard?.current ?? 0;
  161. const shardB = b.metadata.shard?.current ?? 0;
  162. if (shardA !== shardB)
  163. return shardA - shardB;
  164. return a.file.localeCompare(b.file);
  165. });
  166. printStatus(`merging events`);
  167. const reports = [];
  168. const globalTestIdSet = /* @__PURE__ */ new Set();
  169. for (let i = 0; i < blobs.length; ++i) {
  170. const { parsedEvents, metadata, localPath } = blobs[i];
  171. const eventPatchers = new JsonEventPatchers();
  172. eventPatchers.patchers.push(new IdsPatcher(
  173. stringPool,
  174. metadata.name,
  175. String(i),
  176. globalTestIdSet
  177. ));
  178. if (rootDirOverride)
  179. eventPatchers.patchers.push(new PathSeparatorPatcher(metadata.pathSeparator));
  180. eventPatchers.patchEvents(parsedEvents);
  181. for (const event of parsedEvents) {
  182. if (event.method === "onConfigure")
  183. configureEvents.push(event);
  184. else if (event.method === "onProject")
  185. projectEvents.push(event);
  186. else if (event.method === "onEnd")
  187. endEvents.push(event);
  188. }
  189. reports.push({
  190. eventPatchers,
  191. reportFile: localPath,
  192. metadata
  193. });
  194. }
  195. return {
  196. prologue: [
  197. mergeConfigureEvents(configureEvents, rootDirOverride),
  198. ...projectEvents,
  199. { method: "onBegin", params: void 0 }
  200. ],
  201. reports,
  202. epilogue: [
  203. mergeEndEvents(endEvents),
  204. { method: "onExit", params: void 0 }
  205. ],
  206. pathSeparatorFromMetadata: blobs[0]?.metadata.pathSeparator
  207. };
  208. }
  209. function mergeConfigureEvents(configureEvents, rootDirOverride) {
  210. if (!configureEvents.length)
  211. throw new Error("No configure events found");
  212. let config = {
  213. configFile: void 0,
  214. globalTimeout: 0,
  215. maxFailures: 0,
  216. metadata: {},
  217. rootDir: "",
  218. version: "",
  219. workers: 0
  220. };
  221. for (const event of configureEvents)
  222. config = mergeConfigs(config, event.params.config);
  223. if (rootDirOverride) {
  224. config.rootDir = rootDirOverride;
  225. } else {
  226. const rootDirs = new Set(configureEvents.map((e) => e.params.config.rootDir));
  227. if (rootDirs.size > 1) {
  228. throw new Error([
  229. `Blob reports being merged were recorded with different test directories, and`,
  230. `merging cannot proceed. This may happen if you are merging reports from`,
  231. `machines with different environments, like different operating systems or`,
  232. `if the tests ran with different playwright configs.`,
  233. ``,
  234. `You can force merge by specifying a merge config file with "-c" option. If`,
  235. `you'd like all test paths to be correct, make sure 'testDir' in the merge config`,
  236. `file points to the actual tests location.`,
  237. ``,
  238. `Found directories:`,
  239. ...rootDirs
  240. ].join("\n"));
  241. }
  242. }
  243. return {
  244. method: "onConfigure",
  245. params: {
  246. config
  247. }
  248. };
  249. }
  250. function mergeConfigs(to, from) {
  251. return {
  252. ...to,
  253. ...from,
  254. metadata: {
  255. ...to.metadata,
  256. ...from.metadata,
  257. actualWorkers: (to.metadata.actualWorkers || 0) + (from.metadata.actualWorkers || 0)
  258. },
  259. workers: to.workers + from.workers
  260. };
  261. }
  262. function mergeEndEvents(endEvents) {
  263. let startTime = endEvents.length ? 1e13 : Date.now();
  264. let status = "passed";
  265. let duration = 0;
  266. for (const event of endEvents) {
  267. const shardResult = event.params.result;
  268. if (shardResult.status === "failed")
  269. status = "failed";
  270. else if (shardResult.status === "timedout" && status !== "failed")
  271. status = "timedout";
  272. else if (shardResult.status === "interrupted" && status !== "failed" && status !== "timedout")
  273. status = "interrupted";
  274. startTime = Math.min(startTime, shardResult.startTime);
  275. duration = Math.max(duration, shardResult.duration);
  276. }
  277. const result = {
  278. status,
  279. startTime,
  280. duration
  281. };
  282. return {
  283. method: "onEnd",
  284. params: {
  285. result
  286. }
  287. };
  288. }
  289. async function sortedShardFiles(dir) {
  290. const files = await import_fs.default.promises.readdir(dir);
  291. return files.filter((file) => file.endsWith(".zip")).sort();
  292. }
  293. function printStatusToStdout(message) {
  294. process.stdout.write(`${message}
  295. `);
  296. }
  297. class UniqueFileNameGenerator {
  298. constructor() {
  299. this._usedNames = /* @__PURE__ */ new Set();
  300. }
  301. makeUnique(name) {
  302. if (!this._usedNames.has(name)) {
  303. this._usedNames.add(name);
  304. return name;
  305. }
  306. const extension = import_path.default.extname(name);
  307. name = name.substring(0, name.length - extension.length);
  308. let index = 0;
  309. while (true) {
  310. const candidate = `${name}-${++index}${extension}`;
  311. if (!this._usedNames.has(candidate)) {
  312. this._usedNames.add(candidate);
  313. return candidate;
  314. }
  315. }
  316. }
  317. }
  318. class IdsPatcher {
  319. constructor(stringPool, botName, salt, globalTestIdSet) {
  320. this._stringPool = stringPool;
  321. this._botName = botName;
  322. this._salt = salt;
  323. this._testIdsMap = /* @__PURE__ */ new Map();
  324. this._globalTestIdSet = globalTestIdSet;
  325. }
  326. patchEvent(event) {
  327. const { method, params } = event;
  328. switch (method) {
  329. case "onProject":
  330. this._onProject(params.project);
  331. return;
  332. case "onAttach":
  333. case "onTestBegin":
  334. case "onStepBegin":
  335. case "onStepEnd":
  336. case "onStdIO":
  337. params.testId = params.testId ? this._mapTestId(params.testId) : void 0;
  338. return;
  339. case "onTestEnd":
  340. params.test.testId = this._mapTestId(params.test.testId);
  341. return;
  342. }
  343. }
  344. _onProject(project) {
  345. project.metadata ??= {};
  346. project.suites.forEach((suite) => this._updateTestIds(suite));
  347. }
  348. _updateTestIds(suite) {
  349. suite.entries.forEach((entry) => {
  350. if ("testId" in entry)
  351. this._updateTestId(entry);
  352. else
  353. this._updateTestIds(entry);
  354. });
  355. }
  356. _updateTestId(test) {
  357. test.testId = this._mapTestId(test.testId);
  358. if (this._botName) {
  359. test.tags = test.tags || [];
  360. test.tags.unshift("@" + this._botName);
  361. }
  362. }
  363. _mapTestId(testId) {
  364. const t1 = this._stringPool.internString(testId);
  365. if (this._testIdsMap.has(t1))
  366. return this._testIdsMap.get(t1);
  367. if (this._globalTestIdSet.has(t1)) {
  368. const t2 = this._stringPool.internString(testId + this._salt);
  369. this._globalTestIdSet.add(t2);
  370. this._testIdsMap.set(t1, t2);
  371. return t2;
  372. }
  373. this._globalTestIdSet.add(t1);
  374. this._testIdsMap.set(t1, t1);
  375. return t1;
  376. }
  377. }
  378. class AttachmentPathPatcher {
  379. constructor(_resourceDir) {
  380. this._resourceDir = _resourceDir;
  381. }
  382. patchEvent(event) {
  383. if (event.method === "onAttach")
  384. this._patchAttachments(event.params.attachments);
  385. else if (event.method === "onTestEnd")
  386. this._patchAttachments(event.params.result.attachments ?? []);
  387. }
  388. _patchAttachments(attachments) {
  389. for (const attachment of attachments) {
  390. if (!attachment.path)
  391. continue;
  392. attachment.path = import_path.default.join(this._resourceDir, attachment.path);
  393. }
  394. }
  395. }
  396. class PathSeparatorPatcher {
  397. constructor(from) {
  398. this._from = from ?? (import_path.default.sep === "/" ? "\\" : "/");
  399. this._to = import_path.default.sep;
  400. }
  401. patchEvent(jsonEvent) {
  402. if (this._from === this._to)
  403. return;
  404. if (jsonEvent.method === "onProject") {
  405. this._updateProject(jsonEvent.params.project);
  406. return;
  407. }
  408. if (jsonEvent.method === "onTestEnd") {
  409. const test = jsonEvent.params.test;
  410. test.annotations?.forEach((annotation) => this._updateAnnotationLocation(annotation));
  411. const testResult = jsonEvent.params.result;
  412. testResult.annotations?.forEach((annotation) => this._updateAnnotationLocation(annotation));
  413. testResult.errors.forEach((error) => this._updateErrorLocations(error));
  414. (testResult.attachments ?? []).forEach((attachment) => {
  415. if (attachment.path)
  416. attachment.path = this._updatePath(attachment.path);
  417. });
  418. return;
  419. }
  420. if (jsonEvent.method === "onStepBegin") {
  421. const step = jsonEvent.params.step;
  422. this._updateLocation(step.location);
  423. return;
  424. }
  425. if (jsonEvent.method === "onStepEnd") {
  426. const step = jsonEvent.params.step;
  427. this._updateErrorLocations(step.error);
  428. step.annotations?.forEach((annotation) => this._updateAnnotationLocation(annotation));
  429. return;
  430. }
  431. if (jsonEvent.method === "onAttach") {
  432. const attach = jsonEvent.params;
  433. attach.attachments.forEach((attachment) => {
  434. if (attachment.path)
  435. attachment.path = this._updatePath(attachment.path);
  436. });
  437. return;
  438. }
  439. }
  440. _updateProject(project) {
  441. project.outputDir = this._updatePath(project.outputDir);
  442. project.testDir = this._updatePath(project.testDir);
  443. project.snapshotDir = this._updatePath(project.snapshotDir);
  444. project.suites.forEach((suite) => this._updateSuite(suite, true));
  445. }
  446. _updateSuite(suite, isFileSuite = false) {
  447. this._updateLocation(suite.location);
  448. if (isFileSuite)
  449. suite.title = this._updatePath(suite.title);
  450. for (const entry of suite.entries) {
  451. if ("testId" in entry) {
  452. this._updateLocation(entry.location);
  453. entry.annotations?.forEach((annotation) => this._updateAnnotationLocation(annotation));
  454. } else {
  455. this._updateSuite(entry);
  456. }
  457. }
  458. }
  459. _updateErrorLocations(error) {
  460. while (error) {
  461. this._updateLocation(error.location);
  462. error = error.cause;
  463. }
  464. }
  465. _updateAnnotationLocation(annotation) {
  466. this._updateLocation(annotation.location);
  467. }
  468. _updateLocation(location) {
  469. if (location)
  470. location.file = this._updatePath(location.file);
  471. }
  472. _updatePath(text) {
  473. return text.split(this._from).join(this._to);
  474. }
  475. }
  476. class GlobalErrorPatcher {
  477. constructor(botName) {
  478. this._prefix = `(${botName}) `;
  479. }
  480. patchEvent(event) {
  481. if (event.method !== "onError")
  482. return;
  483. const error = event.params.error;
  484. if (error.message !== void 0)
  485. error.message = this._prefix + error.message;
  486. if (error.stack !== void 0)
  487. error.stack = this._prefix + error.stack;
  488. }
  489. }
  490. class JsonEventPatchers {
  491. constructor() {
  492. this.patchers = [];
  493. }
  494. patchEvents(events) {
  495. for (const event of events) {
  496. for (const patcher of this.patchers)
  497. patcher.patchEvent(event);
  498. }
  499. }
  500. }
  501. class BlobModernizer {
  502. modernize(fromVersion, events) {
  503. const result = [];
  504. for (const event of events)
  505. result.push(...this._modernize(fromVersion, event));
  506. return result;
  507. }
  508. _modernize(fromVersion, event) {
  509. let events = [event];
  510. for (let version = fromVersion; version < import_blob.currentBlobReportVersion; ++version)
  511. events = this[`_modernize_${version}_to_${version + 1}`].call(this, events);
  512. return events;
  513. }
  514. _modernize_1_to_2(events) {
  515. return events.map((event) => {
  516. if (event.method === "onProject") {
  517. const modernizeSuite = (suite) => {
  518. const newSuites = suite.suites.map(modernizeSuite);
  519. const { suites, tests, ...remainder } = suite;
  520. return { entries: [...newSuites, ...tests], ...remainder };
  521. };
  522. const project = event.params.project;
  523. project.suites = project.suites.map(modernizeSuite);
  524. }
  525. return event;
  526. });
  527. }
  528. }
  529. const modernizer = new BlobModernizer();
  530. // Annotate the CommonJS export names for ESM import in node:
  531. 0 && (module.exports = {
  532. createMergedReport
  533. });