2021-06-06 17:09:53 -07: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.
|
|
|
|
*/
|
|
|
|
|
2021-08-07 22:08:56 -07:00
|
|
|
import type { TestInfoImpl } from './types';
|
2021-06-06 17:09:53 -07:00
|
|
|
import util from 'util';
|
2021-07-19 12:20:24 -05:00
|
|
|
import path from 'path';
|
|
|
|
import type { TestError, Location } from './types';
|
2021-06-06 17:09:53 -07:00
|
|
|
import { default as minimatch } from 'minimatch';
|
2021-07-28 15:44:44 -07:00
|
|
|
import { errors } from '../..';
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-08-07 22:08:56 -07:00
|
|
|
export async function pollUntilDeadline(testInfo: TestInfoImpl, func: (remainingTime: number) => Promise<boolean>, pollTime: number | undefined, deadlinePromise: Promise<void>): Promise<void> {
|
|
|
|
let defaultExpectTimeout = testInfo.project.expect?.timeout;
|
|
|
|
if (typeof defaultExpectTimeout === 'undefined')
|
|
|
|
defaultExpectTimeout = 5000;
|
|
|
|
pollTime = pollTime === 0 ? 0 : pollTime || defaultExpectTimeout;
|
2021-07-29 21:03:50 -07:00
|
|
|
const deadline = pollTime ? monotonicTime() + pollTime : 0;
|
|
|
|
|
2021-07-30 13:12:49 -07:00
|
|
|
let aborted = false;
|
|
|
|
const abortedPromise = deadlinePromise.then(() => {
|
|
|
|
aborted = true;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2021-08-05 10:55:37 -07:00
|
|
|
const pollIntervals = [100, 250, 500];
|
|
|
|
let attempts = 0;
|
2021-07-30 13:12:49 -07:00
|
|
|
while (!aborted) {
|
2021-07-29 21:03:50 -07:00
|
|
|
const remainingTime = deadline ? deadline - monotonicTime() : 1000 * 3600 * 24;
|
|
|
|
if (remainingTime <= 0)
|
2021-07-28 15:44:44 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
try {
|
2021-07-30 13:12:49 -07:00
|
|
|
// Either aborted, or func() returned truthy.
|
|
|
|
const result = await Promise.race([
|
|
|
|
func(remainingTime),
|
|
|
|
abortedPromise,
|
|
|
|
]);
|
|
|
|
if (result)
|
2021-07-28 15:44:44 -07:00
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof errors.TimeoutError)
|
|
|
|
return;
|
|
|
|
throw e;
|
|
|
|
}
|
2021-07-30 13:12:49 -07:00
|
|
|
|
|
|
|
let timer: NodeJS.Timer;
|
2021-08-05 10:55:37 -07:00
|
|
|
const timeoutPromise = new Promise(f => timer = setTimeout(f, pollIntervals[attempts++] || 1000));
|
2021-07-30 13:12:49 -07:00
|
|
|
await Promise.race([abortedPromise, timeoutPromise]);
|
|
|
|
clearTimeout(timer!);
|
2021-07-27 20:26:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
export function serializeError(error: Error | any): TestError {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
return {
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
value: util.inspect(error)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function monotonicTime(): number {
|
|
|
|
const [seconds, nanoseconds] = process.hrtime();
|
|
|
|
return seconds * 1000 + (nanoseconds / 1000000 | 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isRegExp(e: any): e is RegExp {
|
|
|
|
return e && typeof e === 'object' && (e instanceof RegExp || Object.prototype.toString.call(e) === '[object RegExp]');
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Matcher = (value: string) => boolean;
|
|
|
|
|
2021-06-24 10:02:34 +02:00
|
|
|
export type FilePatternFilter = {
|
|
|
|
re: RegExp;
|
|
|
|
line: number | null;
|
|
|
|
};
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
export function createMatcher(patterns: string | RegExp | (string | RegExp)[]): Matcher {
|
|
|
|
const reList: RegExp[] = [];
|
|
|
|
const filePatterns: string[] = [];
|
|
|
|
for (const pattern of Array.isArray(patterns) ? patterns : [patterns]) {
|
|
|
|
if (isRegExp(pattern)) {
|
|
|
|
reList.push(pattern);
|
|
|
|
} else {
|
|
|
|
if (!pattern.startsWith('**/') && !pattern.startsWith('**/'))
|
|
|
|
filePatterns.push('**/' + pattern);
|
|
|
|
else
|
|
|
|
filePatterns.push(pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (value: string) => {
|
|
|
|
for (const re of reList) {
|
|
|
|
re.lastIndex = 0;
|
|
|
|
if (re.test(value))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (const pattern of filePatterns) {
|
2021-06-15 17:27:52 -07:00
|
|
|
if (minimatch(value, pattern, {
|
|
|
|
nocase: true,
|
|
|
|
}))
|
2021-06-06 17:09:53 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mergeObjects<A extends object, B extends object>(a: A | undefined | void, b: B | undefined | void): A & B {
|
|
|
|
const result = { ...a } as any;
|
|
|
|
if (!Object.is(b, undefined)) {
|
|
|
|
for (const [name, value] of Object.entries(b as B)) {
|
|
|
|
if (!Object.is(value, undefined))
|
|
|
|
result[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result as any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function wrapInPromise(value: any) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function forceRegExp(pattern: string): RegExp {
|
|
|
|
const match = pattern.match(/^\/(.*)\/([gi]*)$/);
|
|
|
|
if (match)
|
|
|
|
return new RegExp(match[1], match[2]);
|
|
|
|
return new RegExp(pattern, 'g');
|
|
|
|
}
|
2021-07-19 12:20:24 -05: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 12:34:37 -07:00
|
|
|
|
|
|
|
export function errorWithLocation(location: Location, message: string) {
|
|
|
|
return new Error(`${formatLocation(location)}: ${message}`);
|
|
|
|
}
|
2021-07-28 15:44:44 -07:00
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function expectType(receiver: any, type: string, matcherName: string) {
|
|
|
|
if (typeof receiver !== 'object' || receiver.constructor.name !== type)
|
|
|
|
throw new Error(`${matcherName} can be only used with ${type} object`);
|
2021-07-28 15:44:44 -07:00
|
|
|
}
|
2021-08-11 00:24:35 -04:00
|
|
|
|
|
|
|
export function sanitizeForFilePath(s: string) {
|
|
|
|
return s.replace(/[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
|
|
|
}
|