merge.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createMergedReport = createMergedReport;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _teleReceiver = require("../isomorphic/teleReceiver");
  9. var _stringInternPool = require("../isomorphic/stringInternPool");
  10. var _reporters = require("../runner/reporters");
  11. var _multiplexer = require("./multiplexer");
  12. var _utils = require("playwright-core/lib/utils");
  13. var _blob = require("./blob");
  14. var _util = require("../util");
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. /**
  17. * Copyright (c) Microsoft Corporation.
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the "License");
  20. * you may not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * http://www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an "AS IS" BASIS,
  27. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. */
  31. async function createMergedReport(config, dir, reporterDescriptions, rootDirOverride) {
  32. var _eventData$pathSepara;
  33. const reporters = await (0, _reporters.createReporters)(config, 'merge', reporterDescriptions);
  34. const multiplexer = new _multiplexer.Multiplexer(reporters);
  35. const stringPool = new _stringInternPool.StringInternPool();
  36. let printStatus = () => {};
  37. if (!multiplexer.printsToStdio()) {
  38. printStatus = printStatusToStdout;
  39. printStatus(`merging reports from ${dir}`);
  40. }
  41. const shardFiles = await sortedShardFiles(dir);
  42. if (shardFiles.length === 0) throw new Error(`No report files found in ${dir}`);
  43. const eventData = await mergeEvents(dir, shardFiles, stringPool, printStatus, rootDirOverride);
  44. // If expicit config is provided, use platform path separator, otherwise use the one from the report (if any).
  45. const pathSep = rootDirOverride ? _path.default.sep : (_eventData$pathSepara = eventData.pathSeparatorFromMetadata) !== null && _eventData$pathSepara !== void 0 ? _eventData$pathSepara : _path.default.sep;
  46. const receiver = new _teleReceiver.TeleReporterReceiver(pathSep, multiplexer, false, config.config);
  47. printStatus(`processing test events`);
  48. const dispatchEvents = async events => {
  49. for (const event of events) {
  50. if (event.method === 'onEnd') printStatus(`building final report`);
  51. await receiver.dispatch(event);
  52. if (event.method === 'onEnd') printStatus(`finished building report`);
  53. }
  54. };
  55. await dispatchEvents(eventData.prologue);
  56. for (const {
  57. reportFile,
  58. eventPatchers
  59. } of eventData.reports) {
  60. const reportJsonl = await _fs.default.promises.readFile(reportFile);
  61. const events = parseTestEvents(reportJsonl);
  62. new _stringInternPool.JsonStringInternalizer(stringPool).traverse(events);
  63. eventPatchers.patchers.push(new AttachmentPathPatcher(dir));
  64. eventPatchers.patchEvents(events);
  65. await dispatchEvents(events);
  66. }
  67. await dispatchEvents(eventData.epilogue);
  68. }
  69. const commonEventNames = ['onBlobReportMetadata', 'onConfigure', 'onProject', 'onBegin', 'onEnd'];
  70. const commonEvents = new Set(commonEventNames);
  71. const commonEventRegex = new RegExp(`${commonEventNames.join('|')}`);
  72. function parseCommonEvents(reportJsonl) {
  73. return splitBufferLines(reportJsonl).map(line => line.toString('utf8')).filter(line => commonEventRegex.test(line)) // quick filter
  74. .map(line => JSON.parse(line)).filter(event => commonEvents.has(event.method));
  75. }
  76. function parseTestEvents(reportJsonl) {
  77. return splitBufferLines(reportJsonl).map(line => line.toString('utf8')).filter(line => line.length).map(line => JSON.parse(line)).filter(event => !commonEvents.has(event.method));
  78. }
  79. function splitBufferLines(buffer) {
  80. const lines = [];
  81. let start = 0;
  82. while (start < buffer.length) {
  83. // 0x0A is the byte for '\n'
  84. const end = buffer.indexOf(0x0A, start);
  85. if (end === -1) {
  86. lines.push(buffer.slice(start));
  87. break;
  88. }
  89. lines.push(buffer.slice(start, end));
  90. start = end + 1;
  91. }
  92. return lines;
  93. }
  94. async function extractAndParseReports(dir, shardFiles, internalizer, printStatus) {
  95. const shardEvents = [];
  96. await _fs.default.promises.mkdir(_path.default.join(dir, 'resources'), {
  97. recursive: true
  98. });
  99. const reportNames = new UniqueFileNameGenerator();
  100. for (const file of shardFiles) {
  101. const absolutePath = _path.default.join(dir, file);
  102. printStatus(`extracting: ${(0, _util.relativeFilePath)(absolutePath)}`);
  103. const zipFile = new _utils.ZipFile(absolutePath);
  104. const entryNames = await zipFile.entries();
  105. for (const entryName of entryNames.sort()) {
  106. let fileName = _path.default.join(dir, entryName);
  107. const content = await zipFile.read(entryName);
  108. if (entryName.endsWith('.jsonl')) {
  109. fileName = reportNames.makeUnique(fileName);
  110. const parsedEvents = parseCommonEvents(content);
  111. // Passing reviver to JSON.parse doesn't work, as the original strings
  112. // keep beeing used. To work around that we traverse the parsed events
  113. // as a post-processing step.
  114. internalizer.traverse(parsedEvents);
  115. shardEvents.push({
  116. file,
  117. localPath: fileName,
  118. metadata: findMetadata(parsedEvents, file),
  119. parsedEvents
  120. });
  121. }
  122. await _fs.default.promises.writeFile(fileName, content);
  123. }
  124. zipFile.close();
  125. }
  126. return shardEvents;
  127. }
  128. function findMetadata(events, file) {
  129. var _events$;
  130. if (((_events$ = events[0]) === null || _events$ === void 0 ? void 0 : _events$.method) !== 'onBlobReportMetadata') throw new Error(`No metadata event found in ${file}`);
  131. const metadata = events[0].params;
  132. if (metadata.version > _blob.currentBlobReportVersion) throw new Error(`Blob report ${file} was created with a newer version of Playwright.`);
  133. return metadata;
  134. }
  135. async function mergeEvents(dir, shardReportFiles, stringPool, printStatus, rootDirOverride) {
  136. var _blobs$;
  137. const internalizer = new _stringInternPool.JsonStringInternalizer(stringPool);
  138. const configureEvents = [];
  139. const projectEvents = [];
  140. const endEvents = [];
  141. const blobs = await extractAndParseReports(dir, shardReportFiles, internalizer, printStatus);
  142. // Sort by (report name; shard; file name), so that salt generation below is deterministic when:
  143. // - report names are unique;
  144. // - report names are missing;
  145. // - report names are clashing between shards.
  146. blobs.sort((a, b) => {
  147. var _a$metadata$name, _b$metadata$name, _a$metadata$shard$cur, _a$metadata$shard, _b$metadata$shard$cur, _b$metadata$shard;
  148. const nameA = (_a$metadata$name = a.metadata.name) !== null && _a$metadata$name !== void 0 ? _a$metadata$name : '';
  149. const nameB = (_b$metadata$name = b.metadata.name) !== null && _b$metadata$name !== void 0 ? _b$metadata$name : '';
  150. if (nameA !== nameB) return nameA.localeCompare(nameB);
  151. const shardA = (_a$metadata$shard$cur = (_a$metadata$shard = a.metadata.shard) === null || _a$metadata$shard === void 0 ? void 0 : _a$metadata$shard.current) !== null && _a$metadata$shard$cur !== void 0 ? _a$metadata$shard$cur : 0;
  152. const shardB = (_b$metadata$shard$cur = (_b$metadata$shard = b.metadata.shard) === null || _b$metadata$shard === void 0 ? void 0 : _b$metadata$shard.current) !== null && _b$metadata$shard$cur !== void 0 ? _b$metadata$shard$cur : 0;
  153. if (shardA !== shardB) return shardA - shardB;
  154. return a.file.localeCompare(b.file);
  155. });
  156. const saltSet = new Set();
  157. printStatus(`merging events`);
  158. const reports = [];
  159. for (const {
  160. file,
  161. parsedEvents,
  162. metadata,
  163. localPath
  164. } of blobs) {
  165. // Generate unique salt for each blob.
  166. const sha1 = (0, _utils.calculateSha1)(metadata.name || _path.default.basename(file)).substring(0, 16);
  167. let salt = sha1;
  168. for (let i = 0; saltSet.has(salt); i++) salt = sha1 + '-' + i;
  169. saltSet.add(salt);
  170. const eventPatchers = new JsonEventPatchers();
  171. eventPatchers.patchers.push(new IdsPatcher(stringPool, metadata.name, salt));
  172. // Only patch path separators if we are merging reports with explicit config.
  173. if (rootDirOverride) eventPatchers.patchers.push(new PathSeparatorPatcher(metadata.pathSeparator));
  174. eventPatchers.patchEvents(parsedEvents);
  175. for (const event of parsedEvents) {
  176. if (event.method === 'onConfigure') configureEvents.push(event);else if (event.method === 'onProject') projectEvents.push(event);else if (event.method === 'onEnd') endEvents.push(event);
  177. }
  178. // Save information about the reports to stream their test events later.
  179. reports.push({
  180. eventPatchers,
  181. reportFile: localPath
  182. });
  183. }
  184. return {
  185. prologue: [mergeConfigureEvents(configureEvents, rootDirOverride), ...projectEvents, {
  186. method: 'onBegin',
  187. params: undefined
  188. }],
  189. reports,
  190. epilogue: [mergeEndEvents(endEvents), {
  191. method: 'onExit',
  192. params: undefined
  193. }],
  194. pathSeparatorFromMetadata: (_blobs$ = blobs[0]) === null || _blobs$ === void 0 ? void 0 : _blobs$.metadata.pathSeparator
  195. };
  196. }
  197. function mergeConfigureEvents(configureEvents, rootDirOverride) {
  198. if (!configureEvents.length) throw new Error('No configure events found');
  199. let config = {
  200. configFile: undefined,
  201. globalTimeout: 0,
  202. maxFailures: 0,
  203. metadata: {},
  204. rootDir: '',
  205. version: '',
  206. workers: 0,
  207. listOnly: false
  208. };
  209. for (const event of configureEvents) config = mergeConfigs(config, event.params.config);
  210. if (rootDirOverride) {
  211. config.rootDir = rootDirOverride;
  212. } else {
  213. const rootDirs = new Set(configureEvents.map(e => e.params.config.rootDir));
  214. if (rootDirs.size > 1) {
  215. throw new Error([`Blob reports being merged were recorded with different test directories, and`, `merging cannot proceed. This may happen if you are merging reports from`, `machines with different environments, like different operating systems or`, `if the tests ran with different playwright configs.`, ``, `You can force merge by specifying a merge config file with "-c" option. If`, `you'd like all test paths to be correct, make sure 'testDir' in the merge config`, `file points to the actual tests location.`, ``, `Found directories:`, ...rootDirs].join('\n'));
  216. }
  217. }
  218. return {
  219. method: 'onConfigure',
  220. params: {
  221. config
  222. }
  223. };
  224. }
  225. function mergeConfigs(to, from) {
  226. return {
  227. ...to,
  228. ...from,
  229. metadata: {
  230. ...to.metadata,
  231. ...from.metadata,
  232. actualWorkers: (to.metadata.actualWorkers || 0) + (from.metadata.actualWorkers || 0)
  233. },
  234. workers: to.workers + from.workers
  235. };
  236. }
  237. function mergeEndEvents(endEvents) {
  238. let startTime = endEvents.length ? 10000000000000 : Date.now();
  239. let status = 'passed';
  240. let duration = 0;
  241. for (const event of endEvents) {
  242. const shardResult = event.params.result;
  243. if (shardResult.status === 'failed') status = 'failed';else if (shardResult.status === 'timedout' && status !== 'failed') status = 'timedout';else if (shardResult.status === 'interrupted' && status !== 'failed' && status !== 'timedout') status = 'interrupted';
  244. startTime = Math.min(startTime, shardResult.startTime);
  245. duration = Math.max(duration, shardResult.duration);
  246. }
  247. const result = {
  248. status,
  249. startTime,
  250. duration
  251. };
  252. return {
  253. method: 'onEnd',
  254. params: {
  255. result
  256. }
  257. };
  258. }
  259. async function sortedShardFiles(dir) {
  260. const files = await _fs.default.promises.readdir(dir);
  261. return files.filter(file => file.endsWith('.zip')).sort();
  262. }
  263. function printStatusToStdout(message) {
  264. process.stdout.write(`${message}\n`);
  265. }
  266. class UniqueFileNameGenerator {
  267. constructor() {
  268. this._usedNames = new Set();
  269. }
  270. makeUnique(name) {
  271. if (!this._usedNames.has(name)) {
  272. this._usedNames.add(name);
  273. return name;
  274. }
  275. const extension = _path.default.extname(name);
  276. name = name.substring(0, name.length - extension.length);
  277. let index = 0;
  278. while (true) {
  279. const candidate = `${name}-${++index}${extension}`;
  280. if (!this._usedNames.has(candidate)) {
  281. this._usedNames.add(candidate);
  282. return candidate;
  283. }
  284. }
  285. }
  286. }
  287. class IdsPatcher {
  288. constructor(_stringPool, _reportName, _salt) {
  289. this._stringPool = _stringPool;
  290. this._reportName = _reportName;
  291. this._salt = _salt;
  292. }
  293. patchEvent(event) {
  294. const {
  295. method,
  296. params
  297. } = event;
  298. switch (method) {
  299. case 'onProject':
  300. this._onProject(params.project);
  301. return;
  302. case 'onTestBegin':
  303. case 'onStepBegin':
  304. case 'onStepEnd':
  305. case 'onStdIO':
  306. params.testId = this._mapTestId(params.testId);
  307. return;
  308. case 'onTestEnd':
  309. params.test.testId = this._mapTestId(params.test.testId);
  310. return;
  311. }
  312. }
  313. _onProject(project) {
  314. var _project$metadata;
  315. project.metadata = (_project$metadata = project.metadata) !== null && _project$metadata !== void 0 ? _project$metadata : {};
  316. project.metadata.reportName = this._reportName;
  317. project.id = this._stringPool.internString(project.id + this._salt);
  318. project.suites.forEach(suite => this._updateTestIds(suite));
  319. }
  320. _updateTestIds(suite) {
  321. suite.tests.forEach(test => test.testId = this._mapTestId(test.testId));
  322. suite.suites.forEach(suite => this._updateTestIds(suite));
  323. }
  324. _mapTestId(testId) {
  325. return this._stringPool.internString(testId + this._salt);
  326. }
  327. }
  328. class AttachmentPathPatcher {
  329. constructor(_resourceDir) {
  330. this._resourceDir = _resourceDir;
  331. }
  332. patchEvent(event) {
  333. if (event.method !== 'onTestEnd') return;
  334. for (const attachment of event.params.result.attachments) {
  335. if (!attachment.path) continue;
  336. attachment.path = _path.default.join(this._resourceDir, attachment.path);
  337. }
  338. }
  339. }
  340. class PathSeparatorPatcher {
  341. constructor(from) {
  342. this._from = void 0;
  343. this._to = void 0;
  344. this._from = from !== null && from !== void 0 ? from : _path.default.sep === '/' ? '\\' : '/';
  345. this._to = _path.default.sep;
  346. }
  347. patchEvent(jsonEvent) {
  348. if (this._from === this._to) return;
  349. if (jsonEvent.method === 'onProject') {
  350. this._updateProject(jsonEvent.params.project);
  351. return;
  352. }
  353. if (jsonEvent.method === 'onTestEnd') {
  354. const testResult = jsonEvent.params.result;
  355. testResult.errors.forEach(error => this._updateLocation(error.location));
  356. testResult.attachments.forEach(attachment => {
  357. if (attachment.path) attachment.path = this._updatePath(attachment.path);
  358. });
  359. return;
  360. }
  361. if (jsonEvent.method === 'onStepBegin') {
  362. const step = jsonEvent.params.step;
  363. this._updateLocation(step.location);
  364. return;
  365. }
  366. }
  367. _updateProject(project) {
  368. project.outputDir = this._updatePath(project.outputDir);
  369. project.testDir = this._updatePath(project.testDir);
  370. project.snapshotDir = this._updatePath(project.snapshotDir);
  371. project.suites.forEach(suite => this._updateSuite(suite, true));
  372. }
  373. _updateSuite(suite, isFileSuite = false) {
  374. this._updateLocation(suite.location);
  375. if (isFileSuite) suite.title = this._updatePath(suite.title);
  376. for (const child of suite.suites) this._updateSuite(child);
  377. for (const test of suite.tests) this._updateLocation(test.location);
  378. }
  379. _updateLocation(location) {
  380. if (location) location.file = this._updatePath(location.file);
  381. }
  382. _updatePath(text) {
  383. return text.split(this._from).join(this._to);
  384. }
  385. }
  386. class JsonEventPatchers {
  387. constructor() {
  388. this.patchers = [];
  389. }
  390. patchEvents(events) {
  391. for (const event of events) {
  392. for (const patcher of this.patchers) patcher.patchEvent(event);
  393. }
  394. }
  395. }