@flakiness/sdk 0.145.0 → 0.146.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,430 +0,0 @@
1
- // src/playwrightJSONReport.ts
2
- import { FlakinessReport as FK } from "@flakiness/report";
3
- import { ReportUtils as ReportUtils2 } from "@flakiness/report";
4
- import debug from "debug";
5
- import { posix as posixPath2 } from "path";
6
-
7
- // src/utils.ts
8
- import { ReportUtils } from "@flakiness/report";
9
- import assert from "assert";
10
- import { spawnSync } from "child_process";
11
- import crypto from "crypto";
12
- import fs from "fs";
13
- import http from "http";
14
- import https from "https";
15
- import os from "os";
16
- import path, { posix as posixPath, win32 as win32Path } from "path";
17
- async function existsAsync(aPath) {
18
- return fs.promises.stat(aPath).then(() => true).catch((e) => false);
19
- }
20
- function extractEnvConfiguration() {
21
- const ENV_PREFIX = "FK_ENV_";
22
- return Object.fromEntries(
23
- Object.entries(process.env).filter(([key]) => key.toUpperCase().startsWith(ENV_PREFIX.toUpperCase())).map(([key, value]) => [key.substring(ENV_PREFIX.length).toLowerCase(), (value ?? "").trim().toLowerCase()])
24
- );
25
- }
26
- function sha1File(filePath) {
27
- return new Promise((resolve, reject) => {
28
- const hash = crypto.createHash("sha1");
29
- const stream = fs.createReadStream(filePath);
30
- stream.on("data", (chunk) => {
31
- hash.update(chunk);
32
- });
33
- stream.on("end", () => {
34
- resolve(hash.digest("hex"));
35
- });
36
- stream.on("error", (err) => {
37
- reject(err);
38
- });
39
- });
40
- }
41
- var FLAKINESS_DBG = !!process.env.FLAKINESS_DBG;
42
- function errorText(error) {
43
- return FLAKINESS_DBG ? error.stack : error.message;
44
- }
45
- function sha1Buffer(data) {
46
- const hash = crypto.createHash("sha1");
47
- hash.update(data);
48
- return hash.digest("hex");
49
- }
50
- async function retryWithBackoff(job, backoff = []) {
51
- for (const timeout of backoff) {
52
- try {
53
- return await job();
54
- } catch (e) {
55
- if (e instanceof AggregateError)
56
- console.error(`[flakiness.io err]`, errorText(e.errors[0]));
57
- else if (e instanceof Error)
58
- console.error(`[flakiness.io err]`, errorText(e));
59
- else
60
- console.error(`[flakiness.io err]`, e);
61
- await new Promise((x) => setTimeout(x, timeout));
62
- }
63
- }
64
- return await job();
65
- }
66
- var httpUtils;
67
- ((httpUtils2) => {
68
- function createRequest({ url, method = "get", headers = {} }) {
69
- let resolve;
70
- let reject;
71
- const responseDataPromise = new Promise((a, b) => {
72
- resolve = a;
73
- reject = b;
74
- });
75
- const protocol = url.startsWith("https") ? https : http;
76
- headers = Object.fromEntries(Object.entries(headers).filter(([key, value]) => value !== void 0));
77
- const request = protocol.request(url, { method, headers }, (res) => {
78
- const chunks = [];
79
- res.on("data", (chunk) => chunks.push(chunk));
80
- res.on("end", () => {
81
- if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300)
82
- resolve(Buffer.concat(chunks));
83
- else
84
- reject(new Error(`Request to ${url} failed with ${res.statusCode}`));
85
- });
86
- res.on("error", (error) => reject(error));
87
- });
88
- request.on("error", reject);
89
- return { request, responseDataPromise };
90
- }
91
- httpUtils2.createRequest = createRequest;
92
- async function getBuffer(url, backoff) {
93
- return await retryWithBackoff(async () => {
94
- const { request, responseDataPromise } = createRequest({ url });
95
- request.end();
96
- return await responseDataPromise;
97
- }, backoff);
98
- }
99
- httpUtils2.getBuffer = getBuffer;
100
- async function getText(url, backoff) {
101
- const buffer = await getBuffer(url, backoff);
102
- return buffer.toString("utf-8");
103
- }
104
- httpUtils2.getText = getText;
105
- async function getJSON(url) {
106
- return JSON.parse(await getText(url));
107
- }
108
- httpUtils2.getJSON = getJSON;
109
- async function postText(url, text, backoff) {
110
- const headers = {
111
- "Content-Type": "application/json",
112
- "Content-Length": Buffer.byteLength(text) + ""
113
- };
114
- return await retryWithBackoff(async () => {
115
- const { request, responseDataPromise } = createRequest({ url, headers, method: "post" });
116
- request.write(text);
117
- request.end();
118
- return await responseDataPromise;
119
- }, backoff);
120
- }
121
- httpUtils2.postText = postText;
122
- async function postJSON(url, json, backoff) {
123
- const buffer = await postText(url, JSON.stringify(json), backoff);
124
- return JSON.parse(buffer.toString("utf-8"));
125
- }
126
- httpUtils2.postJSON = postJSON;
127
- })(httpUtils || (httpUtils = {}));
128
- var ansiRegex = new RegExp("[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", "g");
129
- function stripAnsi(str) {
130
- return str.replace(ansiRegex, "");
131
- }
132
- function shell(command, args, options) {
133
- try {
134
- const result = spawnSync(command, args, { encoding: "utf-8", ...options });
135
- if (result.status !== 0) {
136
- return void 0;
137
- }
138
- return result.stdout.trim();
139
- } catch (e) {
140
- console.error(e);
141
- return void 0;
142
- }
143
- }
144
- function readLinuxOSRelease() {
145
- const osReleaseText = fs.readFileSync("/etc/os-release", "utf-8");
146
- return new Map(osReleaseText.toLowerCase().split("\n").filter((line) => line.includes("=")).map((line) => {
147
- line = line.trim();
148
- let [key, value] = line.split("=");
149
- if (value.startsWith('"') && value.endsWith('"'))
150
- value = value.substring(1, value.length - 1);
151
- return [key, value];
152
- }));
153
- }
154
- function osLinuxInfo() {
155
- const arch = shell(`uname`, [`-m`]);
156
- const osReleaseMap = readLinuxOSRelease();
157
- const name = osReleaseMap.get("name") ?? shell(`uname`);
158
- const version = osReleaseMap.get("version_id");
159
- return { name, arch, version };
160
- }
161
- function osDarwinInfo() {
162
- const name = "macos";
163
- const arch = shell(`uname`, [`-m`]);
164
- const version = shell(`sw_vers`, [`-productVersion`]);
165
- return { name, arch, version };
166
- }
167
- function osWinInfo() {
168
- const name = "win";
169
- const arch = process.arch;
170
- const version = os.release();
171
- return { name, arch, version };
172
- }
173
- function getOSInfo() {
174
- if (process.platform === "darwin")
175
- return osDarwinInfo();
176
- if (process.platform === "win32")
177
- return osWinInfo();
178
- return osLinuxInfo();
179
- }
180
- function inferRunUrl() {
181
- if (process.env.GITHUB_REPOSITORY && process.env.GITHUB_RUN_ID)
182
- return `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
183
- return void 0;
184
- }
185
- function parseStringDate(dateString) {
186
- return +new Date(dateString);
187
- }
188
- function gitCommitInfo(gitRepo) {
189
- const sha = shell(`git`, ["rev-parse", "HEAD"], {
190
- cwd: gitRepo,
191
- encoding: "utf-8"
192
- });
193
- assert(sha, `FAILED: git rev-parse HEAD @ ${gitRepo}`);
194
- return sha.trim();
195
- }
196
- function computeGitRoot(somePathInsideGitRepo) {
197
- const root = shell(`git`, ["rev-parse", "--show-toplevel"], {
198
- cwd: somePathInsideGitRepo,
199
- encoding: "utf-8"
200
- });
201
- assert(root, `FAILED: git rev-parse --show-toplevel HEAD @ ${somePathInsideGitRepo}`);
202
- return normalizePath(root);
203
- }
204
- var IS_WIN32_PATH = new RegExp("^[a-zA-Z]:\\\\", "i");
205
- var IS_ALMOST_POSIX_PATH = new RegExp("^[a-zA-Z]:/", "i");
206
- function normalizePath(aPath) {
207
- if (IS_WIN32_PATH.test(aPath)) {
208
- aPath = aPath.split(win32Path.sep).join(posixPath.sep);
209
- }
210
- if (IS_ALMOST_POSIX_PATH.test(aPath))
211
- return "/" + aPath[0] + aPath.substring(2);
212
- return aPath;
213
- }
214
- function gitFilePath(gitRoot, absolutePath) {
215
- return posixPath.relative(gitRoot, absolutePath);
216
- }
217
- function parseDurationMS(value) {
218
- if (isNaN(value))
219
- throw new Error("Duration cannot be NaN");
220
- if (value < 0)
221
- throw new Error(`Duration cannot be less than 0, found ${value}`);
222
- return value | 0;
223
- }
224
- function createEnvironments(projects) {
225
- const envConfiguration = extractEnvConfiguration();
226
- const osInfo = getOSInfo();
227
- let uniqueNames = /* @__PURE__ */ new Set();
228
- const result = /* @__PURE__ */ new Map();
229
- for (const project of projects) {
230
- let defaultName = project.name;
231
- if (!defaultName.trim())
232
- defaultName = "anonymous";
233
- let name = defaultName;
234
- for (let i = 2; uniqueNames.has(name); ++i)
235
- name = `${defaultName}-${i}`;
236
- uniqueNames.add(defaultName);
237
- result.set(project, {
238
- name,
239
- systemData: {
240
- osArch: osInfo.arch,
241
- osName: osInfo.name,
242
- osVersion: osInfo.version
243
- },
244
- userSuppliedData: {
245
- ...envConfiguration,
246
- ...project.metadata
247
- },
248
- opaqueData: {
249
- project
250
- }
251
- });
252
- }
253
- return result;
254
- }
255
-
256
- // src/playwrightJSONReport.ts
257
- var dlog = debug("flakiness:json-report");
258
- var PlaywrightJSONReport;
259
- ((PlaywrightJSONReport2) => {
260
- function collectMetadata(somePathInsideProject = process.cwd()) {
261
- const commitId = gitCommitInfo(somePathInsideProject);
262
- const osInfo = getOSInfo();
263
- const metadata = {
264
- gitRoot: computeGitRoot(somePathInsideProject),
265
- commitId,
266
- osName: osInfo.name,
267
- arch: osInfo.arch,
268
- osVersion: osInfo.version,
269
- runURL: inferRunUrl()
270
- };
271
- dlog(`metadata directory: ${somePathInsideProject}`);
272
- dlog(`metadata: ${JSON.stringify(metadata)}`);
273
- dlog(`commit info: ${JSON.stringify(commitId)}`);
274
- dlog(`os info: ${JSON.stringify(osInfo)}`);
275
- return metadata;
276
- }
277
- PlaywrightJSONReport2.collectMetadata = collectMetadata;
278
- async function parse(metadata, jsonReport, options) {
279
- const context = {
280
- projectId2environmentIdx: /* @__PURE__ */ new Map(),
281
- testBaseDir: normalizePath(jsonReport.config.rootDir),
282
- gitRoot: metadata.gitRoot,
283
- attachments: /* @__PURE__ */ new Map(),
284
- unaccessibleAttachmentPaths: [],
285
- extractAttachments: options.extractAttachments
286
- };
287
- const configPath = jsonReport.config.configFile ? gitFilePath(context.gitRoot, normalizePath(jsonReport.config.configFile)) : void 0;
288
- const report = {
289
- category: FK.CATEGORY_PLAYWRIGHT,
290
- commitId: metadata.commitId,
291
- configPath,
292
- url: metadata.runURL,
293
- environments: [],
294
- suites: [],
295
- opaqueData: jsonReport.config,
296
- unattributedErrors: jsonReport.errors.map((error) => parseJSONError(context, error)),
297
- // The report.stats is a releatively new addition to Playwright's JSONReport,
298
- // so we have to polyfill with some reasonable values when it's missing.
299
- duration: jsonReport.stats?.duration && jsonReport.stats?.duration > 0 ? parseDurationMS(jsonReport.stats.duration) : 0,
300
- startTimestamp: jsonReport.stats && jsonReport.stats.startTime ? parseStringDate(jsonReport.stats.startTime) : Date.now()
301
- };
302
- report.environments = [...createEnvironments(jsonReport.config.projects).values()];
303
- for (let envIdx = 0; envIdx < report.environments.length; ++envIdx)
304
- context.projectId2environmentIdx.set(jsonReport.config.projects[envIdx].id, envIdx);
305
- report.suites = await Promise.all(jsonReport.suites.map((suite) => parseJSONSuite(context, suite)));
306
- return {
307
- report: ReportUtils2.dedupeSuitesTestsEnvironments(report),
308
- attachments: [...context.attachments.values()],
309
- unaccessibleAttachmentPaths: context.unaccessibleAttachmentPaths
310
- };
311
- }
312
- PlaywrightJSONReport2.parse = parse;
313
- })(PlaywrightJSONReport || (PlaywrightJSONReport = {}));
314
- async function parseJSONSuite(context, jsonSuite) {
315
- let type = "suite";
316
- if (jsonSuite.column === 0 && jsonSuite.line === 0)
317
- type = "file";
318
- else if (!jsonSuite.title)
319
- type = "anonymous suite";
320
- const suite = {
321
- type,
322
- title: jsonSuite.title,
323
- location: {
324
- file: gitFilePath(context.gitRoot, normalizePath(jsonSuite.file)),
325
- line: jsonSuite.line,
326
- column: jsonSuite.column
327
- }
328
- };
329
- if (jsonSuite.suites && jsonSuite.suites.length)
330
- suite.suites = await Promise.all(jsonSuite.suites.map((suite2) => parseJSONSuite(context, suite2)));
331
- if (jsonSuite.specs && jsonSuite.specs.length)
332
- suite.tests = await Promise.all(jsonSuite.specs.map((spec) => parseJSONSpec(context, spec)));
333
- return suite;
334
- }
335
- async function parseJSONSpec(context, jsonSpec) {
336
- const test = {
337
- title: jsonSpec.title,
338
- tags: jsonSpec.tags,
339
- location: {
340
- file: gitFilePath(context.gitRoot, normalizePath(posixPath2.join(context.testBaseDir, normalizePath(jsonSpec.file)))),
341
- line: jsonSpec.line,
342
- column: jsonSpec.column
343
- },
344
- attempts: []
345
- };
346
- for (const jsonTest of jsonSpec.tests) {
347
- const environmentIdx = context.projectId2environmentIdx.get(jsonTest.projectId);
348
- if (environmentIdx === void 0)
349
- throw new Error("Inconsistent report - no project for a test found!");
350
- const testResults = jsonTest.results.filter((result) => result.status !== void 0);
351
- if (!testResults.length)
352
- continue;
353
- test.attempts.push(...await Promise.all(testResults.map((jsonTestResult) => parseJSONTestResult(context, jsonTest, environmentIdx, jsonTestResult))));
354
- }
355
- return test;
356
- }
357
- function createLocation(context, location) {
358
- return {
359
- file: gitFilePath(context.gitRoot, normalizePath(location.file)),
360
- line: location.line,
361
- column: location.column
362
- };
363
- }
364
- async function parseJSONTestResult(context, jsonTest, environmentIdx, jsonTestResult) {
365
- const attachments = [];
366
- const attempt = {
367
- timeout: parseDurationMS(jsonTest.timeout),
368
- annotations: jsonTest.annotations.map((annotation) => ({
369
- type: annotation.type,
370
- description: annotation.description,
371
- location: annotation.location ? createLocation(context, annotation.location) : void 0
372
- })),
373
- environmentIdx,
374
- expectedStatus: jsonTest.expectedStatus,
375
- parallelIndex: jsonTestResult.parallelIndex,
376
- status: jsonTestResult.status,
377
- errors: jsonTestResult.errors && jsonTestResult.errors.length ? jsonTestResult.errors.map((error) => parseJSONError(context, error)) : void 0,
378
- stdout: jsonTestResult.stdout && jsonTestResult.stdout.length ? jsonTestResult.stdout : void 0,
379
- stderr: jsonTestResult.stderr && jsonTestResult.stderr.length ? jsonTestResult.stderr : void 0,
380
- steps: jsonTestResult.steps ? jsonTestResult.steps.map((jsonTestStep) => parseJSONTestStep(context, jsonTestStep)) : void 0,
381
- startTimestamp: parseStringDate(jsonTestResult.startTime),
382
- duration: jsonTestResult.duration && jsonTestResult.duration > 0 ? parseDurationMS(jsonTestResult.duration) : 0,
383
- attachments
384
- };
385
- if (context.extractAttachments) {
386
- await Promise.all((jsonTestResult.attachments ?? []).map(async (jsonAttachment) => {
387
- if (jsonAttachment.path && !await existsAsync(jsonAttachment.path)) {
388
- context.unaccessibleAttachmentPaths.push(jsonAttachment.path);
389
- return;
390
- }
391
- const id = jsonAttachment.path ? await sha1File(jsonAttachment.path) : sha1Buffer(jsonAttachment.body ?? "");
392
- context.attachments.set(id, {
393
- contentType: jsonAttachment.contentType,
394
- id,
395
- body: jsonAttachment.body ? Buffer.from(jsonAttachment.body) : void 0,
396
- path: jsonAttachment.path
397
- });
398
- attachments.push({
399
- id,
400
- name: jsonAttachment.name,
401
- contentType: jsonAttachment.contentType
402
- });
403
- }));
404
- }
405
- return attempt;
406
- }
407
- function parseJSONTestStep(context, jsonStep) {
408
- const step = {
409
- // NOTE: jsonStep.duration was -1 in some playwright versions
410
- duration: parseDurationMS(Math.max(jsonStep.duration, 0)),
411
- title: jsonStep.title
412
- };
413
- if (jsonStep.error)
414
- step.error = parseJSONError(context, jsonStep.error);
415
- if (jsonStep.steps)
416
- step.steps = jsonStep.steps.map((childJSONStep) => parseJSONTestStep(context, childJSONStep));
417
- return step;
418
- }
419
- function parseJSONError(context, error) {
420
- return {
421
- location: error.location ? createLocation(context, error.location) : void 0,
422
- message: error.message ? stripAnsi(error.message).split("\n")[0] : void 0,
423
- stack: error.stack,
424
- value: error.value
425
- };
426
- }
427
- export {
428
- PlaywrightJSONReport
429
- };
430
- //# sourceMappingURL=playwrightJSONReport.js.map