UNPKG

html-reporter

Version:

Html-reporter and GUI for viewing and managing results of a tests run. Currently supports Testplane and Hermione.

109 lines 5.57 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeReferenceImage = exports.revertReferenceImage = exports.updateReferenceImages = void 0; const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const promises_1 = require("stream/promises"); const lodash_1 = __importDefault(require("lodash")); const common_utils_1 = require("./common-utils"); const utils = __importStar(require("./server-utils")); const server_utils_1 = require("./server-utils"); const utils_1 = require("./adapters/test-result/utils"); const constants_1 = require("./constants"); const mkReferenceHash = (testId, stateName) => (0, common_utils_1.getShortMD5)(`${testId}#${stateName}`); const resolveSourcePath = (actualImgPath, reportPath) => { return (0, common_utils_1.isUrl)(actualImgPath) ? actualImgPath : path_1.default.resolve(reportPath, actualImgPath); }; const copyImageFromUrl = async (imageUrl, destinationPath) => { const { data, status } = await (0, common_utils_1.fetchFile)(imageUrl, { responseType: 'stream' }); if (!data) { throw new Error(`Failed to fetch image by URL "${imageUrl}". Request status: ${status}`); } await utils.makeDirFor(destinationPath); await (0, promises_1.pipeline)(data, fs_extra_1.default.createWriteStream(destinationPath)); }; const updateReferenceImages = async (testResult, reportPath, onReferenceUpdateCb) => { const { default: tmp } = await Promise.resolve().then(() => __importStar(require('tmp'))); const newImagesInfo = await Promise.all(testResult.imagesInfo.map(async (imageInfo) => { const newImageInfo = lodash_1.default.clone(imageInfo); if (!(0, common_utils_1.isImageInfoWithState)(newImageInfo) || newImageInfo.status !== constants_1.UPDATED) { return newImageInfo; } const { stateName } = newImageInfo; const { actualImg } = newImageInfo; const src = actualImg?.path ? resolveSourcePath(actualImg.path, reportPath) : utils.getCurrentAbsolutePath(testResult, reportPath, stateName); const referencePath = newImageInfo.refImg.path; if (utils.fileExists(referencePath)) { const referenceId = mkReferenceHash(testResult.id, stateName); const oldReferencePath = path_1.default.resolve(tmp.tmpdir, referenceId); await utils.copyFileAsync(referencePath, oldReferencePath); } const reportReferencePath = utils.getReferencePath(testResult, stateName); const resolvedReportReferencePath = path_1.default.resolve(reportPath, reportReferencePath); if ((0, common_utils_1.isUrl)(src)) { await copyImageFromUrl(src, referencePath); await utils.copyFileAsync(referencePath, resolvedReportReferencePath); } else { await Promise.all([ utils.copyFileAsync(src, referencePath), utils.copyFileAsync(src, resolvedReportReferencePath) ]); } const { expectedImg } = newImageInfo; expectedImg.path = reportReferencePath; onReferenceUpdateCb(testResult, newImageInfo, stateName); return newImageInfo; })); return (0, utils_1.copyAndUpdate)(testResult, { imagesInfo: newImagesInfo }); }; exports.updateReferenceImages = updateReferenceImages; const revertReferenceImage = async (removedResult, newResult, stateName) => { const { default: tmp } = await Promise.resolve().then(() => __importStar(require('tmp'))); const referenceId = removedResult.id; const referenceHash = mkReferenceHash(referenceId, stateName); const oldReferencePath = path_1.default.resolve(tmp.tmpdir, referenceHash); const referencePath = (0, server_utils_1.getImagesInfoByStateName)(newResult.imagesInfo, stateName)?.refImg?.path; if (!referencePath) { return; } return utils.copyFileAsync(oldReferencePath, referencePath); }; exports.revertReferenceImage = revertReferenceImage; const removeReferenceImage = async (testResult, stateName) => { const imagePath = (0, server_utils_1.getImagesInfoByStateName)(testResult.imagesInfo, stateName)?.refImg?.path; if (!imagePath) { return; } return utils.deleteFile(imagePath); }; exports.removeReferenceImage = removeReferenceImage; //# sourceMappingURL=reporter-helpers.js.map