testplane
Version:
Tests framework based on mocha and wdio
83 lines • 3.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHashReader = exports.HashReader = void 0;
const lodash_1 = require("lodash");
const node_path_1 = __importDefault(require("node:path"));
const hash_provider_1 = require("./hash-provider");
const utils_1 = require("./utils");
const debug_1 = require("./debug");
class HashReader {
constructor(selectivityRootPath, compression) {
this._hashProvider = new hash_provider_1.HashProvider();
this._hashFileContents = null;
this._fileStateCache = new Map();
this._selectivityHashesPath = (0, utils_1.getSelectivityHashesPath)(selectivityRootPath);
this._compresion = compression;
}
_getHashFileContents() {
if (this._hashFileContents) {
return this._hashFileContents;
}
return (this._hashFileContents = (0, utils_1.readHashFileContents)(this._selectivityHashesPath, this._compresion));
}
async patternHasChanged(pattern) {
const fileContents = await this._getHashFileContents();
const cachedPatternHash = fileContents.patterns[pattern];
const calculatedPatternHash = await this._hashProvider.calculateForPattern(pattern);
return cachedPatternHash !== calculatedPatternHash;
}
/** @returns changed deps or null, if nothing changed */
async getTestChangedDeps(testDeps) {
const depFileTypes = ["css", "js", "modules", "png"];
const fileContents = await this._getHashFileContents();
let result = null;
const checkForDepFileType = async (depFileType) => {
// Old selectivity dependency files did not have "png" property
if (!testDeps[depFileType]) {
return;
}
for (const filePath of testDeps[depFileType]) {
const isChanged = this._fileStateCache.get(filePath);
if (isChanged === false) {
continue;
}
else if (isChanged === true) {
result ||= { css: [], js: [], modules: [], png: [] };
result[depFileType].push(filePath);
continue;
}
const adjustedFilePath = depFileType === "modules" ? node_path_1.default.join(filePath, "package.json") : filePath;
const cachedFileHash = depFileType === "modules" ? fileContents.modules[filePath] : fileContents.files[filePath];
const calculatedFileHash = await this._hashProvider
.calculateForFile(adjustedFilePath)
.catch((err) => err);
if (calculatedFileHash instanceof Error) {
(0, debug_1.debugSelectivity)(`${calculatedFileHash.message}: ${calculatedFileHash.cause}`);
}
if (cachedFileHash !== calculatedFileHash) {
result ||= { css: [], js: [], modules: [], png: [] };
result[depFileType].push(filePath);
this._fileStateCache.set(filePath, true);
}
else {
this._fileStateCache.set(filePath, false);
}
}
};
for (const depFileType of depFileTypes) {
await checkForDepFileType(depFileType);
}
return result;
}
clearCache() {
this._fileStateCache.clear();
}
}
exports.HashReader = HashReader;
exports.getHashReader = (0, lodash_1.memoize)((selectivityRootPath, compression) => {
return new HashReader(selectivityRootPath, compression);
}, (selectivityRootPath, compression) => `${selectivityRootPath}#${compression}`);
//# sourceMappingURL=hash-reader.js.map