2021-06-07 02:09:53 +02:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
import fs from 'fs';
|
2022-04-19 05:20:49 +02:00
|
|
|
import { mime } from 'playwright-core/lib/utilsBundle';
|
2023-02-25 03:36:15 +01:00
|
|
|
import type { StackFrame } from '@protocol/channels';
|
2022-04-08 22:22:14 +02:00
|
|
|
import util from 'util';
|
2021-07-19 19:20:24 +02:00
|
|
|
import path from 'path';
|
2021-09-13 18:09:38 +02:00
|
|
|
import url from 'url';
|
2023-02-22 04:24:17 +01:00
|
|
|
import { colors, debug, minimatch, parseStackTraceLine } from 'playwright-core/lib/utilsBundle';
|
2023-04-07 18:54:01 +02:00
|
|
|
import type { TestInfoError } from './../types/test';
|
|
|
|
|
import type { Location } from './../types/testReporter';
|
2023-08-03 22:23:30 +02:00
|
|
|
import { calculateSha1, isRegExp, isString, sanitizeForFilePath } from 'playwright-core/lib/utils';
|
2023-02-24 21:17:03 +01:00
|
|
|
import type { RawStack } from 'playwright-core/lib/utils';
|
2022-02-02 03:40:44 +01:00
|
|
|
|
2023-01-27 21:44:15 +01:00
|
|
|
const PLAYWRIGHT_TEST_PATH = path.join(__dirname, '..');
|
2023-02-22 04:24:17 +01:00
|
|
|
const PLAYWRIGHT_CORE_PATH = path.dirname(require.resolve('playwright-core/package.json'));
|
2022-02-02 03:40:44 +01:00
|
|
|
|
2023-05-12 23:23:22 +02:00
|
|
|
export function filterStackTrace(e: Error): { message: string, stack: string } {
|
2022-08-01 22:44:59 +02:00
|
|
|
if (process.env.PWDEBUGIMPL)
|
2023-05-12 23:23:22 +02:00
|
|
|
return { message: e.message, stack: e.stack || '' };
|
|
|
|
|
|
2023-02-24 21:17:03 +01:00
|
|
|
const stackLines = stringifyStackFrames(filteredStackTrace(e.stack?.split('\n') || []));
|
2023-05-12 23:23:22 +02:00
|
|
|
return {
|
|
|
|
|
message: e.message,
|
|
|
|
|
stack: `${e.name}: ${e.message}\n${stackLines.join('\n')}`
|
|
|
|
|
};
|
2022-03-15 02:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-13 04:15:31 +02:00
|
|
|
export function filterStackFile(file: string) {
|
|
|
|
|
if (!process.env.PWDEBUGIMPL && file.startsWith(PLAYWRIGHT_TEST_PATH))
|
|
|
|
|
return false;
|
|
|
|
|
if (!process.env.PWDEBUGIMPL && file.startsWith(PLAYWRIGHT_CORE_PATH))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:36:15 +01:00
|
|
|
export function filteredStackTrace(rawStack: RawStack): StackFrame[] {
|
|
|
|
|
const frames: StackFrame[] = [];
|
2023-02-24 21:17:03 +01:00
|
|
|
for (const line of rawStack) {
|
2023-02-22 04:24:17 +01:00
|
|
|
const frame = parseStackTraceLine(line);
|
2023-02-25 03:36:15 +01:00
|
|
|
if (!frame || !frame.file)
|
2023-02-22 04:24:17 +01:00
|
|
|
continue;
|
2023-05-13 04:15:31 +02:00
|
|
|
if (!filterStackFile(frame.file))
|
2023-02-22 04:24:17 +01:00
|
|
|
continue;
|
|
|
|
|
frames.push(frame);
|
|
|
|
|
}
|
|
|
|
|
return frames;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 03:36:15 +01:00
|
|
|
export function stringifyStackFrames(frames: StackFrame[]): string[] {
|
2023-02-22 04:24:17 +01:00
|
|
|
const stackLines: string[] = [];
|
|
|
|
|
for (const frame of frames) {
|
|
|
|
|
if (frame.function)
|
2023-02-25 03:36:15 +01:00
|
|
|
stackLines.push(` at ${frame.function} (${frame.file}:${frame.line}:${frame.column})`);
|
2023-02-22 04:24:17 +01:00
|
|
|
else
|
2023-02-25 03:36:15 +01:00
|
|
|
stackLines.push(` at ${frame.file}:${frame.line}:${frame.column}`);
|
2023-02-22 04:24:17 +01:00
|
|
|
}
|
|
|
|
|
return stackLines;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 18:36:59 +01:00
|
|
|
export function serializeError(error: Error | any): TestInfoError {
|
2023-05-12 23:23:22 +02:00
|
|
|
if (error instanceof Error)
|
|
|
|
|
return filterStackTrace(error);
|
2021-06-07 02:09:53 +02:00
|
|
|
return {
|
|
|
|
|
value: util.inspect(error)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Matcher = (value: string) => boolean;
|
|
|
|
|
|
2022-08-04 17:09:54 +02:00
|
|
|
export type TestFileFilter = {
|
|
|
|
|
re?: RegExp;
|
|
|
|
|
exact?: string;
|
2021-06-24 10:02:34 +02:00
|
|
|
line: number | null;
|
2022-04-07 22:45:45 +02:00
|
|
|
column: number | null;
|
2021-06-24 10:02:34 +02:00
|
|
|
};
|
|
|
|
|
|
2023-02-07 18:48:46 +01:00
|
|
|
export function createFileFiltersFromArguments(args: string[]): TestFileFilter[] {
|
|
|
|
|
return args.map(arg => {
|
|
|
|
|
const match = /^(.*?):(\d+):?(\d+)?$/.exec(arg);
|
|
|
|
|
return {
|
|
|
|
|
re: forceRegExp(match ? match[1] : arg),
|
|
|
|
|
line: match ? parseInt(match[2], 10) : null,
|
|
|
|
|
column: match?.[3] ? parseInt(match[3], 10) : null,
|
|
|
|
|
};
|
|
|
|
|
});
|
2023-02-07 00:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-07 18:48:46 +01:00
|
|
|
export function createFileMatcherFromArguments(args: string[]): Matcher {
|
|
|
|
|
const filters = createFileFiltersFromArguments(args);
|
2022-10-20 00:05:59 +02:00
|
|
|
return createFileMatcher(filters.map(filter => filter.re || filter.exact || ''));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 18:09:38 +02:00
|
|
|
export function createFileMatcher(patterns: string | RegExp | (string | RegExp)[]): Matcher {
|
2021-06-07 02:09:53 +02:00
|
|
|
const reList: RegExp[] = [];
|
|
|
|
|
const filePatterns: string[] = [];
|
|
|
|
|
for (const pattern of Array.isArray(patterns) ? patterns : [patterns]) {
|
|
|
|
|
if (isRegExp(pattern)) {
|
|
|
|
|
reList.push(pattern);
|
|
|
|
|
} else {
|
2022-09-29 03:45:01 +02:00
|
|
|
if (!pattern.startsWith('**/'))
|
2021-06-07 02:09:53 +02:00
|
|
|
filePatterns.push('**/' + pattern);
|
|
|
|
|
else
|
|
|
|
|
filePatterns.push(pattern);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-13 18:09:38 +02:00
|
|
|
return (filePath: string) => {
|
2021-06-07 02:09:53 +02:00
|
|
|
for (const re of reList) {
|
|
|
|
|
re.lastIndex = 0;
|
2021-09-13 18:09:38 +02:00
|
|
|
if (re.test(filePath))
|
2021-06-07 02:09:53 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2022-02-01 20:09:41 +01:00
|
|
|
// Windows might still receive unix style paths from Cygwin or Git Bash.
|
2021-09-13 18:09:38 +02:00
|
|
|
// Check against the file url as well.
|
|
|
|
|
if (path.sep === '\\') {
|
|
|
|
|
const fileURL = url.pathToFileURL(filePath).href;
|
|
|
|
|
for (const re of reList) {
|
|
|
|
|
re.lastIndex = 0;
|
|
|
|
|
if (re.test(fileURL))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-07 02:09:53 +02:00
|
|
|
for (const pattern of filePatterns) {
|
2021-09-13 18:09:38 +02:00
|
|
|
if (minimatch(filePath, pattern, { nocase: true, dot: true }))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 19:56:03 +01:00
|
|
|
export function createTitleMatcher(patterns: RegExp | RegExp[]): Matcher {
|
2021-09-13 18:09:38 +02:00
|
|
|
const reList = Array.isArray(patterns) ? patterns : [patterns];
|
|
|
|
|
return (value: string) => {
|
|
|
|
|
for (const re of reList) {
|
|
|
|
|
re.lastIndex = 0;
|
|
|
|
|
if (re.test(value))
|
2021-06-07 02:09:53 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 02:46:47 +02:00
|
|
|
export function mergeObjects<A extends object, B extends object, C extends object>(a: A | undefined | void, b: B | undefined | void, c: B | undefined | void): A & B & C {
|
2022-07-27 17:51:45 +02:00
|
|
|
const result = { ...a } as any;
|
2023-04-08 02:46:47 +02:00
|
|
|
for (const x of [b, c].filter(Boolean)) {
|
|
|
|
|
for (const [name, value] of Object.entries(x as any)) {
|
2022-07-27 17:51:45 +02:00
|
|
|
if (!Object.is(value, undefined))
|
|
|
|
|
result[name] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result as any;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function forceRegExp(pattern: string): RegExp {
|
|
|
|
|
const match = pattern.match(/^\/(.*)\/([gi]*)$/);
|
|
|
|
|
if (match)
|
|
|
|
|
return new RegExp(match[1], match[2]);
|
2023-02-07 00:52:14 +01:00
|
|
|
return new RegExp(pattern, 'gi');
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
2021-07-19 19:20:24 +02:00
|
|
|
|
|
|
|
|
export function relativeFilePath(file: string): string {
|
|
|
|
|
if (!path.isAbsolute(file))
|
|
|
|
|
return file;
|
|
|
|
|
return path.relative(process.cwd(), file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatLocation(location: Location) {
|
|
|
|
|
return relativeFilePath(location.file) + ':' + location.line + ':' + location.column;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function errorWithFile(file: string, message: string) {
|
|
|
|
|
return new Error(`${relativeFilePath(file)}: ${message}`);
|
|
|
|
|
}
|
2021-07-22 21:34:37 +02:00
|
|
|
|
2022-03-12 07:40:28 +01:00
|
|
|
export function expectTypes(receiver: any, types: string[], matcherName: string) {
|
|
|
|
|
if (typeof receiver !== 'object' || !types.includes(receiver.constructor.name)) {
|
|
|
|
|
const commaSeparated = types.slice();
|
|
|
|
|
const lastType = commaSeparated.pop();
|
|
|
|
|
const typesString = commaSeparated.length ? commaSeparated.join(', ') + ' or ' + lastType : lastType;
|
|
|
|
|
throw new Error(`${matcherName} can be only used with ${typesString} object${types.length > 1 ? 's' : ''}`);
|
|
|
|
|
}
|
2021-07-29 00:44:44 +02:00
|
|
|
}
|
2021-08-11 06:24:35 +02:00
|
|
|
|
2021-12-13 19:56:03 +01:00
|
|
|
export function trimLongString(s: string, length = 100) {
|
|
|
|
|
if (s.length <= length)
|
|
|
|
|
return s;
|
|
|
|
|
const hash = calculateSha1(s);
|
|
|
|
|
const middle = `-${hash.substring(0, 5)}-`;
|
|
|
|
|
const start = Math.floor((length - middle.length) / 2);
|
|
|
|
|
const end = length - middle.length - start;
|
|
|
|
|
return s.substring(0, start) + middle + s.slice(-end);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 20:03:02 +02:00
|
|
|
export function addSuffixToFilePath(filePath: string, suffix: string, customExtension?: string, sanitize = false): string {
|
|
|
|
|
const dirname = path.dirname(filePath);
|
|
|
|
|
const ext = path.extname(filePath);
|
|
|
|
|
const name = path.basename(filePath, ext);
|
|
|
|
|
const base = path.join(dirname, name);
|
|
|
|
|
return (sanitize ? sanitizeForFilePath(base) : base) + suffix + (customExtension || ext);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 18:15:44 +02:00
|
|
|
/**
|
|
|
|
|
* Returns absolute path contained within parent directory.
|
|
|
|
|
*/
|
|
|
|
|
export function getContainedPath(parentPath: string, subPath: string = ''): string | null {
|
|
|
|
|
const resolvedPath = path.resolve(parentPath, subPath);
|
|
|
|
|
if (resolvedPath === parentPath || resolvedPath.startsWith(parentPath + path.sep)) return resolvedPath;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 17:56:39 +02:00
|
|
|
export const debugTest = debug('pw:test');
|
2022-02-23 22:17:37 +01:00
|
|
|
|
|
|
|
|
export function callLogText(log: string[] | undefined): string {
|
|
|
|
|
if (!log)
|
|
|
|
|
return '';
|
|
|
|
|
return `
|
|
|
|
|
Call log:
|
|
|
|
|
${colors.dim('- ' + (log || []).join('\n - '))}
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
const folderToPackageJsonPath = new Map<string, string>();
|
|
|
|
|
|
|
|
|
|
export function getPackageJsonPath(folderPath: string): string {
|
|
|
|
|
const cached = folderToPackageJsonPath.get(folderPath);
|
|
|
|
|
if (cached !== undefined)
|
|
|
|
|
return cached;
|
|
|
|
|
|
|
|
|
|
const packageJsonPath = path.join(folderPath, 'package.json');
|
|
|
|
|
if (fs.existsSync(packageJsonPath)) {
|
|
|
|
|
folderToPackageJsonPath.set(folderPath, packageJsonPath);
|
|
|
|
|
return packageJsonPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parentFolder = path.dirname(folderPath);
|
|
|
|
|
if (folderPath === parentFolder) {
|
|
|
|
|
folderToPackageJsonPath.set(folderPath, '');
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = getPackageJsonPath(parentFolder);
|
|
|
|
|
folderToPackageJsonPath.set(folderPath, result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-04-08 22:22:14 +02:00
|
|
|
|
2023-07-29 00:49:31 +02:00
|
|
|
export function resolveReporterOutputPath(defaultValue: string, configDir: string, configValue: string | undefined) {
|
|
|
|
|
if (configValue)
|
|
|
|
|
return path.resolve(configDir, configValue);
|
|
|
|
|
let basePath = getPackageJsonPath(configDir);
|
|
|
|
|
basePath = basePath ? path.dirname(basePath) : process.cwd();
|
|
|
|
|
return path.resolve(basePath, defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 05:29:32 +02:00
|
|
|
export async function normalizeAndSaveAttachment(outputPath: string, name: string, options: { path?: string, body?: string | Buffer, contentType?: string } = {}): Promise<{ name: string; path?: string | undefined; body?: Buffer | undefined; contentType: string; }> {
|
2022-04-08 22:22:14 +02:00
|
|
|
if ((options.path !== undefined ? 1 : 0) + (options.body !== undefined ? 1 : 0) !== 1)
|
|
|
|
|
throw new Error(`Exactly one of "path" and "body" must be specified`);
|
|
|
|
|
if (options.path !== undefined) {
|
|
|
|
|
const hash = calculateSha1(options.path);
|
2022-11-03 21:54:51 +01:00
|
|
|
|
|
|
|
|
if (!isString(name))
|
|
|
|
|
throw new Error('"name" should be string.');
|
|
|
|
|
|
|
|
|
|
const sanitizedNamePrefix = sanitizeForFilePath(name) + '-';
|
|
|
|
|
const dest = path.join(outputPath, 'attachments', sanitizedNamePrefix + hash + path.extname(options.path));
|
2022-04-08 22:22:14 +02:00
|
|
|
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
|
|
|
|
|
await fs.promises.copyFile(options.path, dest);
|
|
|
|
|
const contentType = options.contentType ?? (mime.getType(path.basename(options.path)) || 'application/octet-stream');
|
|
|
|
|
return { name, contentType, path: dest };
|
|
|
|
|
} else {
|
|
|
|
|
const contentType = options.contentType ?? (typeof options.body === 'string' ? 'text/plain' : 'application/octet-stream');
|
|
|
|
|
return { name, contentType, body: typeof options.body === 'string' ? Buffer.from(options.body) : options.body };
|
|
|
|
|
}
|
2023-01-18 02:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fileIsModule(file: string): boolean {
|
2023-05-12 00:41:50 +02:00
|
|
|
if (file.endsWith('.mjs') || file.endsWith('.mts'))
|
2023-01-18 02:16:36 +01:00
|
|
|
return true;
|
2023-05-12 00:41:50 +02:00
|
|
|
if (file.endsWith('.cjs') || file.endsWith('.cts'))
|
|
|
|
|
return false;
|
2023-01-18 02:16:36 +01:00
|
|
|
const folder = path.dirname(file);
|
|
|
|
|
return folderIsModule(folder);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 00:12:18 +02:00
|
|
|
function folderIsModule(folder: string): boolean {
|
2023-01-18 02:16:36 +01:00
|
|
|
const packageJsonPath = getPackageJsonPath(folder);
|
|
|
|
|
if (!packageJsonPath)
|
|
|
|
|
return false;
|
|
|
|
|
// Rely on `require` internal caching logic.
|
|
|
|
|
return require(packageJsonPath).type === 'module';
|
|
|
|
|
}
|
2023-01-27 02:26:47 +01:00
|
|
|
|
2023-08-11 18:37:14 +02:00
|
|
|
const kExperimentalLoaderOptions = [
|
|
|
|
|
'--no-warnings',
|
|
|
|
|
`--experimental-loader=${url.pathToFileURL(require.resolve('@playwright/test/lib/transform/esmLoader')).toString()}`,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export function execArgvWithExperimentalLoaderOptions() {
|
|
|
|
|
return [
|
|
|
|
|
...process.execArgv,
|
|
|
|
|
...kExperimentalLoaderOptions,
|
|
|
|
|
];
|
2023-01-27 02:26:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-11 18:37:14 +02:00
|
|
|
export function execArgvWithoutExperimentalLoaderOptions() {
|
|
|
|
|
return process.execArgv.filter(arg => !kExperimentalLoaderOptions.includes(arg));
|
2023-01-27 02:26:47 +01:00
|
|
|
}
|
2023-04-19 23:20:53 +02:00
|
|
|
|
2023-05-10 01:26:29 +02:00
|
|
|
// This follows the --moduleResolution=bundler strategy from tsc.
|
|
|
|
|
// https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#moduleresolution-bundler
|
|
|
|
|
const kExtLookups = new Map([
|
|
|
|
|
['.js', ['.jsx', '.ts', '.tsx']],
|
|
|
|
|
['.jsx', ['.tsx']],
|
|
|
|
|
['.cjs', ['.cts']],
|
|
|
|
|
['.mjs', ['.mts']],
|
|
|
|
|
['', ['.js', '.ts', '.jsx', '.tsx', '.cjs', '.mjs', '.cts', '.mts']],
|
|
|
|
|
]);
|
|
|
|
|
export function resolveImportSpecifierExtension(resolved: string): string | undefined {
|
2023-06-05 17:58:25 +02:00
|
|
|
if (fileExists(resolved))
|
2023-05-10 01:26:29 +02:00
|
|
|
return resolved;
|
2023-06-05 17:58:25 +02:00
|
|
|
|
2023-05-10 01:26:29 +02:00
|
|
|
for (const [ext, others] of kExtLookups) {
|
|
|
|
|
if (!resolved.endsWith(ext))
|
|
|
|
|
continue;
|
|
|
|
|
for (const other of others) {
|
|
|
|
|
const modified = resolved.substring(0, resolved.length - ext.length) + other;
|
2023-06-05 17:58:25 +02:00
|
|
|
if (fileExists(modified))
|
2023-05-10 01:26:29 +02:00
|
|
|
return modified;
|
|
|
|
|
}
|
|
|
|
|
break; // Do not try '' when a more specific extesion like '.jsx' matched.
|
|
|
|
|
}
|
2023-06-05 17:58:25 +02:00
|
|
|
// try directory imports last
|
|
|
|
|
if (dirExists(resolved)) {
|
|
|
|
|
const dirImport = path.join(resolved, 'index');
|
|
|
|
|
return resolveImportSpecifierExtension(dirImport);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fileExists(resolved: string) {
|
|
|
|
|
return fs.statSync(resolved, { throwIfNoEntry: false })?.isFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dirExists(resolved: string) {
|
|
|
|
|
return fs.statSync(resolved, { throwIfNoEntry: false })?.isDirectory();
|
2023-04-19 23:20:53 +02:00
|
|
|
}
|