-
Notifications
You must be signed in to change notification settings - Fork 68
feat: implement API for downloading and reading reports #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import fs from 'fs-extra'; | ||
| import path from 'path'; | ||
|
|
||
| import { | ||
| downloadDatabases, | ||
| makeSqlDatabaseFromFile, | ||
| prepareUrls, | ||
| resolveDatabaseUrlsJsonPath, | ||
| downloadSingleDatabase | ||
| } from './db-utils/server'; | ||
| import {compareDatabaseRowsByTimestamp, selectAllSuitesQuery} from './db-utils/common'; | ||
| import {ReporterTestResult} from './adapters/test-result'; | ||
| import {SqliteTestResultAdapter} from './adapters/test-result/sqlite'; | ||
| import {RawSuitesRow} from './types'; | ||
| import {DB_COLUMN_INDEXES} from './constants'; | ||
| import {isUrl} from './common-utils'; | ||
| import {isDbFile, writeDatabaseUrlsFile} from './server-utils'; | ||
|
|
||
| export type {ReporterTestResult}; | ||
|
|
||
| export type ReportFileToDownload = 'dbFiles'; | ||
|
|
||
| export interface DownloadReportOptions { | ||
| files?: ReportFileToDownload[]; | ||
| } | ||
|
|
||
| export interface DownloadReportResult { | ||
| reportPath: string; | ||
| dbPaths: string[]; | ||
| } | ||
|
|
||
| const DEFAULT_REPORT_FILES_TO_DOWNLOAD: ReportFileToDownload[] = ['dbFiles']; | ||
|
|
||
| const isString = (value: unknown): value is string => typeof value === 'string'; | ||
|
|
||
| const resolveAttempt = (attemptsByBrowser: Map<string, number>, row: RawSuitesRow): number => { | ||
| const testPath: string[] = JSON.parse(row[DB_COLUMN_INDEXES.suitePath] as string); | ||
| const browserName = row[DB_COLUMN_INDEXES.name] as string; | ||
| const browserId = [...testPath, browserName].join(' '); | ||
| const attempt = attemptsByBrowser.has(browserId) ? (attemptsByBrowser.get(browserId) as number) + 1 : 0; | ||
|
|
||
| attemptsByBrowser.set(browserId, attempt); | ||
|
|
||
| return attempt; | ||
| }; | ||
|
|
||
| const validateReportFilesToDownload = (files: ReportFileToDownload[]): void => { | ||
| for (const file of files) { | ||
| if (file !== 'dbFiles') { | ||
| throw new Error(`Unsupported report file type to download: ${file}`); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const validateLocalDatabaseUrlsJson = async (dbJsonPath: string): Promise<void> => { | ||
| const {dbUrls = [], jsonUrls = []} = await fs.readJSON(dbJsonPath); | ||
| const preparedDbUrls = prepareUrls(dbUrls, dbJsonPath); | ||
| const preparedDbJsonUrls = prepareUrls(jsonUrls, dbJsonPath); | ||
|
|
||
| [...preparedDbUrls, ...preparedDbJsonUrls].forEach((filePathOrUrl) => { | ||
| if (isUrl(filePathOrUrl)) { | ||
| throw new Error(`Cannot read remote report file "${filePathOrUrl}". Use downloadReport first.`); | ||
| } | ||
| }); | ||
|
|
||
| await Promise.all(preparedDbJsonUrls.map(validateLocalDatabaseUrlsJson)); | ||
| }; | ||
|
|
||
| const downloadDbFiles = async (reportPathOrUrl: string, destPath: string): Promise<string[]> => { | ||
| if (isDbFile(reportPathOrUrl)) { | ||
| return [await downloadSingleDatabase(reportPathOrUrl, { | ||
| pluginConfig: {path: destPath} | ||
| })]; | ||
| } | ||
|
|
||
| const dbJsonPathOrUrl = await resolveDatabaseUrlsJsonPath(reportPathOrUrl); | ||
| const dbPaths = await downloadDatabases([dbJsonPathOrUrl], { | ||
| pluginConfig: {path: destPath}, | ||
| strict: true | ||
| }); | ||
|
|
||
| return dbPaths.filter(isString); | ||
| }; | ||
|
|
||
| const resolveLocalDbPaths = async (reportPath: string): Promise<string[]> => { | ||
| if (isUrl(reportPath)) { | ||
| throw new Error('readResultsFromReport expects a local report path. Use downloadReport first for remote reports.'); | ||
| } | ||
|
|
||
| if (isDbFile(reportPath)) { | ||
| return [path.resolve(reportPath)]; | ||
| } | ||
|
|
||
| const dbJsonPath = await resolveDatabaseUrlsJsonPath(reportPath); | ||
|
|
||
| await validateLocalDatabaseUrlsJson(dbJsonPath); | ||
|
|
||
| const dbPaths = await downloadDatabases([dbJsonPath], { | ||
| pluginConfig: {path: path.dirname(dbJsonPath)}, | ||
| strict: true | ||
| }); | ||
|
|
||
| return dbPaths.filter(isString); | ||
| }; | ||
|
|
||
| export const downloadReport = async ( | ||
| reportPathOrUrl: string, | ||
| destPath: string, | ||
| options: DownloadReportOptions = {} | ||
| ): Promise<DownloadReportResult> => { | ||
| const files = options.files ?? DEFAULT_REPORT_FILES_TO_DOWNLOAD; | ||
|
|
||
| validateReportFilesToDownload(files); | ||
|
|
||
| if (!isUrl(reportPathOrUrl)) { | ||
| throw new Error('downloadReport expects a remote report path or URL. Use readResultsFromReport directly for local reports.'); | ||
| } | ||
|
|
||
| await fs.ensureDir(destPath); | ||
|
|
||
| const dbPaths = files.includes('dbFiles') ? await downloadDbFiles(reportPathOrUrl, destPath) : []; | ||
|
|
||
| if (files.includes('dbFiles')) { | ||
| await writeDatabaseUrlsFile(destPath, dbPaths.map(dbPath => path.relative(destPath, dbPath))); | ||
| } | ||
|
|
||
| return { | ||
| reportPath: destPath, | ||
| dbPaths | ||
| }; | ||
| }; | ||
|
|
||
| export const readResultsFromReport = async (reportPath: string): Promise<ReporterTestResult[]> => { | ||
| const dbPaths = await resolveLocalDbPaths(reportPath); | ||
| const rows: RawSuitesRow[] = []; | ||
| const attemptsByBrowser = new Map<string, number>(); | ||
| const results: ReporterTestResult[] = []; | ||
|
|
||
| for (const dbPath of dbPaths) { | ||
| const db = await makeSqlDatabaseFromFile(dbPath); | ||
| const statement = db.prepare(selectAllSuitesQuery()); | ||
|
|
||
| while (statement.step()) { | ||
| rows.push(statement.get() as RawSuitesRow); | ||
| } | ||
|
|
||
| statement.free(); | ||
| db.close(); | ||
| } | ||
|
|
||
| for (const row of rows.sort(compareDatabaseRowsByTimestamp)) { | ||
| const attempt = resolveAttempt(attemptsByBrowser, row); | ||
|
|
||
| results.push(new SqliteTestResultAdapter(row, attempt)); | ||
| } | ||
|
|
||
| return results; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.