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 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readResultsFromReport = exports.downloadReport = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const server_1 = require("./db-utils/server"); const common_1 = require("./db-utils/common"); const sqlite_1 = require("./adapters/test-result/sqlite"); const constants_1 = require("./constants"); const common_utils_1 = require("./common-utils"); const server_utils_1 = require("./server-utils"); const DEFAULT_REPORT_FILES_TO_DOWNLOAD = ['dbFiles']; const isString = (value) => typeof value === 'string'; const resolveAttempt = (attemptsByBrowser, row) => { const testPath = JSON.parse(row[constants_1.DB_COLUMN_INDEXES.suitePath]); const browserName = row[constants_1.DB_COLUMN_INDEXES.name]; const browserId = [...testPath, browserName].join(' '); const attempt = attemptsByBrowser.has(browserId) ? attemptsByBrowser.get(browserId) + 1 : 0; attemptsByBrowser.set(browserId, attempt); return attempt; }; const validateReportFilesToDownload = (files) => { for (const file of files) { if (file !== 'dbFiles') { throw new Error(`Unsupported report file type to download: ${file}`); } } }; const validateLocalDatabaseUrlsJson = async (dbJsonPath) => { const { dbUrls = [], jsonUrls = [] } = await fs_extra_1.default.readJSON(dbJsonPath); const preparedDbUrls = (0, server_1.prepareUrls)(dbUrls, dbJsonPath); const preparedDbJsonUrls = (0, server_1.prepareUrls)(jsonUrls, dbJsonPath); [...preparedDbUrls, ...preparedDbJsonUrls].forEach((filePathOrUrl) => { if ((0, common_utils_1.isUrl)(filePathOrUrl)) { throw new Error(`Cannot read remote report file "${filePathOrUrl}". Use downloadReport first.`); } }); await Promise.all(preparedDbJsonUrls.map(validateLocalDatabaseUrlsJson)); }; const downloadDbFiles = async (reportPathOrUrl, destPath) => { if ((0, server_utils_1.isDbFile)(reportPathOrUrl)) { return [await (0, server_1.downloadSingleDatabase)(reportPathOrUrl, { pluginConfig: { path: destPath } })]; } const dbJsonPathOrUrl = await (0, server_1.resolveDatabaseUrlsJsonPath)(reportPathOrUrl); const dbPaths = await (0, server_1.downloadDatabases)([dbJsonPathOrUrl], { pluginConfig: { path: destPath }, strict: true }); return dbPaths.filter(isString); }; const resolveLocalDbPaths = async (reportPath) => { if ((0, common_utils_1.isUrl)(reportPath)) { throw new Error('readResultsFromReport expects a local report path. Use downloadReport first for remote reports.'); } if ((0, server_utils_1.isDbFile)(reportPath)) { return [path_1.default.resolve(reportPath)]; } const dbJsonPath = await (0, server_1.resolveDatabaseUrlsJsonPath)(reportPath); await validateLocalDatabaseUrlsJson(dbJsonPath); const dbPaths = await (0, server_1.downloadDatabases)([dbJsonPath], { pluginConfig: { path: path_1.default.dirname(dbJsonPath) }, strict: true }); return dbPaths.filter(isString); }; const downloadReport = async (reportPathOrUrl, destPath, options = {}) => { const files = options.files ?? DEFAULT_REPORT_FILES_TO_DOWNLOAD; validateReportFilesToDownload(files); if (!(0, common_utils_1.isUrl)(reportPathOrUrl)) { throw new Error('downloadReport expects a remote report path or URL. Use readResultsFromReport directly for local reports.'); } await fs_extra_1.default.ensureDir(destPath); const dbPaths = files.includes('dbFiles') ? await downloadDbFiles(reportPathOrUrl, destPath) : []; if (files.includes('dbFiles')) { await (0, server_utils_1.writeDatabaseUrlsFile)(destPath, dbPaths.map(dbPath => path_1.default.relative(destPath, dbPath))); } return { reportPath: destPath, dbPaths }; }; exports.downloadReport = downloadReport; const readResultsFromReport = async (reportPath) => { const dbPaths = await resolveLocalDbPaths(reportPath); const rows = []; const attemptsByBrowser = new Map(); const results = []; for (const dbPath of dbPaths) { const db = await (0, server_1.makeSqlDatabaseFromFile)(dbPath); const statement = db.prepare((0, common_1.selectAllSuitesQuery)()); while (statement.step()) { rows.push(statement.get()); } statement.free(); db.close(); } for (const row of rows.sort(common_1.compareDatabaseRowsByTimestamp)) { const attempt = resolveAttempt(attemptsByBrowser, row); results.push(new sqlite_1.SqliteTestResultAdapter(row, attempt)); } return results; }; exports.readResultsFromReport = readResultsFromReport; //# sourceMappingURL=sdk.js.map