UNPKG

testplane

Version:

Tests framework based on mocha and wdio

127 lines 6.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readTestFileWithTestplaneDependenciesCollecting = exports.addTestplaneSelectivityPngDependency = exports.runWithTestplaneDependenciesCollecting = exports.getCollectedTestplanePngDependencies = exports.getCollectedTestplaneJsDependencies = exports.enableCollectingTestplaneDependencies = exports.disableCollectingTestplaneDependencies = void 0; const path_1 = __importDefault(require("path")); const module_1 = require("module"); const async_hooks_1 = require("async_hooks"); const fs_cache_1 = require("./fs-cache"); const debug_1 = require("./debug"); // eslint-disable-next-line @typescript-eslint/no-explicit-any const TypedModule = module_1.Module; const testDependenciesStorage = new async_hooks_1.AsyncLocalStorage(); const testFileDependenciesRamCache = new Map(); const testFileLocks = {}; let disableCollectingDependenciesCb = null; const disableCollectingTestplaneDependencies = () => { if (disableCollectingDependenciesCb) { disableCollectingDependenciesCb(); disableCollectingDependenciesCb = null; } }; exports.disableCollectingTestplaneDependencies = disableCollectingTestplaneDependencies; const enableCollectingTestplaneDependencies = () => { if (disableCollectingDependenciesCb) { return; } const originalResolveFileName = TypedModule._resolveFilename; disableCollectingDependenciesCb = () => { TypedModule._resolveFilename = originalResolveFileName; }; TypedModule._resolveFilename = function () { // eslint-disable-next-line prefer-rest-params const result = originalResolveFileName.apply(this, arguments); try { const store = disableCollectingDependenciesCb ? testDependenciesStorage.getStore() : null; const relPath = result && path_1.default.isAbsolute(result) ? path_1.default.relative(process.cwd(), result) : null; if (store && relPath) { const posixRelPath = path_1.default.sep === path_1.default.posix.sep ? relPath : relPath.replaceAll(path_1.default.sep, path_1.default.posix.sep); store.jsTestplaneDeps?.add(posixRelPath); } } catch { } // eslint-disable-line no-empty return result; }; }; exports.enableCollectingTestplaneDependencies = enableCollectingTestplaneDependencies; const getCollectedTestplaneJsDependencies = () => { const store = testDependenciesStorage.getStore(); return store && store.jsTestplaneDeps ? store.jsTestplaneDeps : null; }; exports.getCollectedTestplaneJsDependencies = getCollectedTestplaneJsDependencies; const getCollectedTestplanePngDependencies = () => { const store = testDependenciesStorage.getStore(); return store && store.pngTestplaneDeps ? store.pngTestplaneDeps : null; }; exports.getCollectedTestplanePngDependencies = getCollectedTestplanePngDependencies; const runWithTestplaneDependenciesCollecting = (fn) => { (0, exports.enableCollectingTestplaneDependencies)(); const store = { jsTestplaneDeps: new Set(), pngTestplaneDeps: new Set() }; return testDependenciesStorage.run(store, fn).finally(() => { // After "fn" completion, "store" is reachable in CDP ping interval callback, so it never GC-removed // Thats why we do it manually. It is enough, and set remains unchanged, if used delete store.jsTestplaneDeps; delete store.pngTestplaneDeps; }); }; exports.runWithTestplaneDependenciesCollecting = runWithTestplaneDependenciesCollecting; const addTestplaneSelectivityPngDependency = (pngPath) => { const store = testDependenciesStorage.getStore(); if (store && store.pngTestplaneDeps) { store.pngTestplaneDeps.add(pngPath); } }; exports.addTestplaneSelectivityPngDependency = addTestplaneSelectivityPngDependency; const readTestFileWithTestplaneDependenciesCollecting = async (file, fn) => { if (!disableCollectingDependenciesCb) { return fn(); } const store = testDependenciesStorage.getStore(); const jsTestplaneDeps = store && store.jsTestplaneDeps; if (!jsTestplaneDeps) { return fn(); } if (file in testFileLocks) { await testFileLocks[file]; } let releaseLock = () => { }; testFileLocks[file] = new Promise(resolve => { releaseLock = resolve; }).finally(() => { delete testFileLocks[file]; }); const ramCachedDependencies = testFileDependenciesRamCache.get(file); if (ramCachedDependencies) { releaseLock(); ramCachedDependencies.forEach(dependency => jsTestplaneDeps.add(dependency)); return fn(); } const fsCachedDependencies = await (0, fs_cache_1.getCachedSelectivityFile)(fs_cache_1.CacheType.TestFile, file); if (fsCachedDependencies) { releaseLock(); let parsedDependencies; try { parsedDependencies = JSON.parse(fsCachedDependencies); } catch (cause) { throw new Error(`Selectivity: It looks like fs-cache for "${file}" dependencies is broken: contents of cache file is invalid JSON`, { cause }); } parsedDependencies.forEach(dependency => jsTestplaneDeps.add(dependency)); return fn(); } try { return await fn(); } finally { const cacheValue = Array.from(jsTestplaneDeps).sort((a, b) => a.localeCompare(b)); await (0, fs_cache_1.setCachedSelectivityFile)(fs_cache_1.CacheType.TestFile, file, JSON.stringify(cacheValue)).catch(err => { testFileDependenciesRamCache.set(file, cacheValue); (0, debug_1.debugSelectivity)(`Couldn't offload test dependencies from "${file}" to fs-cache: %O`, err); }); releaseLock(); } }; exports.readTestFileWithTestplaneDependenciesCollecting = readTestFileWithTestplaneDependenciesCollecting; //# sourceMappingURL=testplane-selectivity.js.map