UNPKG

@progress/kendo-e2e

Version:

Kendo UI end-to-end test utilities.

215 lines 10.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultDownloadPath = void 0; exports.exist = exist; exports.getFileContent = getFileContent; exports.copyFile = copyFile; exports.removeCreationDateMetadataFromFile = removeCreationDateMetadataFromFile; exports.compareTwoFiles = compareTwoFiles; exports.deleteFileIfExists = deleteFileIfExists; const fs = __importStar(require("fs")); const os_1 = __importDefault(require("os")); exports.defaultDownloadPath = os_1.default.homedir() + '/Downloads/'; function exist(filePath) { return __awaiter(this, void 0, void 0, function* () { try { yield fs.promises.access(filePath); return true; } catch (_a) { return false; } }); } /** * Asynchronously reads the content of a file located at the specified file path * and returns it as either a string or a Buffer. * * @param {string} fileName - The name of the file to read. * @param {string} [filePath=defaultDownloadPath] - Default: defaultDownloadPath. The directory path where the file is located. * @param {BufferEncoding} [bufferEncoding] - Optional. The encoding to use when reading the file content. * @param {number} [maxWaitMs=20000] - Default: 20sec. The maximum time, in milliseconds, to wait for the * file to become available. If the file is not found within this timeout, an error will be thrown. */ function getFileContent(fileName_1) { return __awaiter(this, arguments, void 0, function* (fileName, filePath = exports.defaultDownloadPath, bufferEncoding, maxWaitMs = 20000) { const fullFilePathName = filePath + fileName; const startTime = Date.now(); let fileBuffer = null; /** * Wait for the file to become available or until a timeout occurs. */ const waitForFile = () => __awaiter(this, void 0, void 0, function* () { while ((Date.now() - startTime) < maxWaitMs) { try { fileBuffer = fs.readFileSync(fullFilePathName); if (bufferEncoding) { return fileBuffer.toString(bufferEncoding); } else { return fileBuffer; } } catch (_error) { console.log(`File '${fileName}' still not found. ` + _error); } // Sleep for a short interval before checking again yield new Promise(resolve => setTimeout(resolve, 1000)); } throw new Error(`File '${fileName}' not found within the specified timeout.`); }); return yield waitForFile(); }); } /** * Copies the content of a source file to a new target file and custom file extension. * * @param {string} sourceFileName - The name of the source file to copy. * @param {string} targetFileName - The name of the target file to create. * @param {string} [filePathSource=defaultDownloadPath] - Default: defaultDownloadPath. The directory path where the file is located. * @param {string} filePathTarget - Default: defaultDownloadPath. The directory path where the file should be saved. * @param {BufferEncoding} [bufferEncoding] - Optional. The encoding to use when reading the file content. */ function copyFile(sourceFileName_1, targetFileName_1) { return __awaiter(this, arguments, void 0, function* (sourceFileName, targetFileName, filePathSource = exports.defaultDownloadPath, filePathTarget = exports.defaultDownloadPath, bufferEncoding) { try { let fileContent; // Read the content of the source file using getFileContent if (bufferEncoding) { fileContent = yield getFileContent(sourceFileName, filePathSource, bufferEncoding); } else { fileContent = yield getFileContent(sourceFileName, filePathSource); } // Write the content to a new file with the specified name fs.writeFileSync(filePathTarget + targetFileName, fileContent, { encoding: bufferEncoding }); console.log(`File '${sourceFileName}' copied to '${targetFileName}' successfully.`); } catch (_error) { throw new Error(`Error copying file: ${_error.message}`); } }); } /** * Removes Creation date metadata from a file content buffer via RegEx. * * @param {Buffer} fileContentBuffer - The file content buffer. * @param {BufferEncoding} [bufferEncoding] - Optional. The encoding to use when reading the file content. * @param {RegExp} [additionalPattern] - Optional. An optional regular expression pattern to match additional metadata. */ function removeCreationDateMetadataFromFile(fileContentBuffer, bufferEncoding, additionalPattern) { return __awaiter(this, void 0, void 0, function* () { // Convert the Buffer to a string const contentString = fileContentBuffer.toString(bufferEncoding); // Define a regular expression pattern to match the metadata (e.g., /CreationDate ...) const metadataPattern = /\/CreationDate\s+\(D:\d{14}/g; // Use replace to remove all occurrences of metadata let cleanedContentString = contentString.replace(metadataPattern, ''); if (additionalPattern) { cleanedContentString = cleanedContentString.replace(additionalPattern, ''); } // Convert the modified string back to a Buffer return Buffer.from(cleanedContentString, bufferEncoding); }); } /** * Compare the content of two files to check if they are identical using Buffer.compare() method. * * @param {string} filePathExpectedResult - The path to the first file. * @param {string} filePathActualResult - The path to the second file. * @param {boolean} removeCreationDate - Default: true. RegEx to remove the creation date metadata. * @param {BufferEncoding} [bufferEncoding] - Optional. The encoding to use when reading the file content. */ function compareTwoFiles(filePathExpectedResult_1, filePathActualResult_1) { return __awaiter(this, arguments, void 0, function* (filePathExpectedResult, filePathActualResult, removeCreationDate = true, bufferEncoding) { try { let fileContentExpected, fileContentActual; // Read the content of both files if (bufferEncoding) { fileContentExpected = fs.readFileSync(filePathExpectedResult, { encoding: bufferEncoding }); fileContentActual = fs.readFileSync(filePathActualResult, { encoding: bufferEncoding }); } else { fileContentExpected = fs.readFileSync(filePathExpectedResult); fileContentActual = fs.readFileSync(filePathActualResult); } let fileContentNoCreationDateActual, fileContentNoCreationDateExpected; if (removeCreationDate) { // Remove the Creation Date metadata via RegEx fileContentNoCreationDateExpected = yield removeCreationDateMetadataFromFile(fileContentExpected); fileContentNoCreationDateActual = yield removeCreationDateMetadataFromFile(fileContentActual); fileContentExpected = fileContentNoCreationDateExpected; fileContentActual = fileContentNoCreationDateActual; } // Compare the content of the files return Buffer.compare(fileContentNoCreationDateExpected, fileContentNoCreationDateActual) === 0; } catch (_error) { throw new Error(`Error comparing files: ${_error.message}`); } }); } /** * Deletes a file from the file system if it exists. If the file is not found - print message and continue with no error. * * @param {string} fileName - The file name to be deleted. * @param {string} [filePath=defaultDownloadPath] - Default: defaultDownloadPath. The directory path where the file is located. */ function deleteFileIfExists(fileName_1) { return __awaiter(this, arguments, void 0, function* (fileName, filePath = exports.defaultDownloadPath) { try { const fullPathToFile = filePath + fileName; yield fs.promises.unlink(fullPathToFile); console.log(`File: '${fullPathToFile}' has been deleted.`); } catch (_error) { console.warn(`Error deleting file: ${_error.message}`); } }); } //# sourceMappingURL=fsUtils.js.map