Skip to content

Commit

Permalink
refactor: get rid of tight coupling with hermione and move image proc…
Browse files Browse the repository at this point in the history
…essing logic out of test-adapter (#491)

* refactor: get rid of hermione in sqlite-adapter

* refactor: get rid of hermione in test-adapter

* refactor: get rid of hermione in static report builder

* refactor: use correct types for workers in test-adapter

* refactor: move image processing logic out of test-adapter
  • Loading branch information
shadowusr authored Sep 1, 2023
1 parent d85d189 commit 469387f
Show file tree
Hide file tree
Showing 33 changed files with 1,499 additions and 1,243 deletions.
5 changes: 3 additions & 2 deletions hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ module.exports = (hermione, opts) => {

async function prepare(hermione, reportBuilder, pluginConfig) {
const {path: reportPath} = pluginConfig;
const {imageHandler} = reportBuilder;

const failHandler = async (testResult) => {
const formattedResult = reportBuilder.format(testResult);
const actions = [formattedResult.saveTestImages(reportPath, workers)];
const actions = [imageHandler.saveTestImages(testResult, formattedResult.attempt, workers)];

if (formattedResult.errorDetails) {
actions.push(formattedResult.saveErrorDetails(reportPath));
Expand All @@ -56,7 +57,7 @@ async function prepare(hermione, reportBuilder, pluginConfig) {
hermione.on(hermione.events.TEST_PASS, testResult => {
promises.push(queue.add(async () => {
const formattedResult = reportBuilder.format(testResult);
await formattedResult.saveTestImages(reportPath, workers);
await imageHandler.saveTestImages(testResult, formattedResult.attempt, workers);

return reportBuilder.addSuccess(formattedResult);
}).catch(reject));
Expand Down
22 changes: 22 additions & 0 deletions lib/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import axios, {AxiosRequestConfig} from 'axios';
import {SUCCESS, FAIL, ERROR, SKIPPED, UPDATED, IDLE, RUNNING, QUEUED, TestStatus} from './constants';

import {UNCHECKED, INDETERMINATE, CHECKED} from './constants/checked-statuses';
import {AssertViewResult, TestResult} from './types';
import {ErrorName, ImageDiffError, NoRefImageError} from './errors';
export const getShortMD5 = (str: string): string => {
return crypto.createHash('md5').update(str, 'ascii').digest('hex').substr(0, 7);
};
Expand Down Expand Up @@ -44,6 +46,26 @@ export const determineStatus = (statuses: TestStatus[]): TestStatus | null => {
return null;
};

export const mkTestId = (fullTitle: string, browserId: string): string => {
return fullTitle + '.' + browserId;
};

export const isImageDiffError = (assertResult: AssertViewResult): assertResult is ImageDiffError => {
return assertResult.name === ErrorName.IMAGE_DIFF;
};

export const isNoRefImageError = (assertResult: AssertViewResult): assertResult is NoRefImageError => {
return assertResult.name === ErrorName.NO_REF_IMAGE;
};

export const getError = (testResult: TestResult): undefined | {message?: string; stack?: string; stateName?: string} => {
if (!testResult.err) {
return undefined;
}

return pick(testResult.err, ['message', 'stack', 'stateName']);
};

export const isUrl = (str: string): boolean => {
if (typeof str !== 'string') {
return false;
Expand Down
3 changes: 2 additions & 1 deletion lib/constants/plugin-events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum PluginEvents {
DATABASE_CREATED = 'databaseCreated',
TEST_SCREENSHOTS_SAVED = 'testScreenshotsSaved',
REPORT_SAVED = 'reportSaved'
REPORT_SAVED = 'reportSaved',
IMAGES_SAVER_UPDATED = 'imagesSaverUpdated'
}
28 changes: 28 additions & 0 deletions lib/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {CoordBounds} from 'looks-same';
import {DiffOptions, ImageData} from '../types';

export enum ErrorName {
IMAGE_DIFF = 'ImageDiffError',
NO_REF_IMAGE = 'NoRefImageError'
}

export interface ImageDiffError {
name: ErrorName.IMAGE_DIFF;
message: string;
stack: string;
stateName: string;
diffOpts: DiffOptions;
currImg: ImageData;
refImg: ImageData;
diffClusters: CoordBounds[];
diffBuffer?: ArrayBuffer;
}

export interface NoRefImageError {
name: ErrorName.NO_REF_IMAGE;
stateName: string;
message: string;
stack: string;
currImg: ImageData;
refImg: ImageData;
}
4 changes: 2 additions & 2 deletions lib/gui/tool-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = class ToolRunner {
async initialize() {
await mergeDatabasesForReuse(this._reportPath);

this._reportBuilder = GuiReportBuilder.create(this._hermione, this._pluginConfig, {reuse: true});
this._reportBuilder = GuiReportBuilder.create(this._hermione.htmlReporter, this._pluginConfig, {reuse: true});
this._subscribeOnEvents();

this._collection = await this._readTests();
Expand Down Expand Up @@ -152,7 +152,7 @@ module.exports = class ToolRunner {
}

if (previousExpectedPath) {
formattedResult.updateCacheExpectedPath(stateName, previousExpectedPath);
this._reportBuilder.imageHandler.updateCacheExpectedPath(updateResult, stateName, previousExpectedPath);
}
});
});
Expand Down
13 changes: 7 additions & 6 deletions lib/gui/tool-runner/report-subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ let workers;

module.exports = (hermione, reportBuilder, client, reportPath) => {
const queue = new PQueue({concurrency: os.cpus().length});
const {imageHandler} = reportBuilder;

function failHandler(formattedResult) {
const actions = [formattedResult.saveTestImages(reportPath, workers)];
function failHandler(testResult, formattedResult) {
const actions = [imageHandler.saveTestImages(testResult, formattedResult.attempt, workers)];

if (formattedResult.errorDetails) {
actions.push(formattedResult.saveErrorDetails(reportPath));
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
const formattedResult = reportBuilder.format(testResult, hermione.events.TEST_PASS);
formattedResult.attempt = reportBuilder.getCurrAttempt(formattedResult);

await formattedResult.saveTestImages(reportPath, workers);
await imageHandler.saveTestImages(testResult, formattedResult.attempt, workers);
reportBuilder.addSuccess(formattedResult);

const testBranch = reportBuilder.getTestBranch(formattedResult.id);
Expand All @@ -66,7 +67,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
const formattedResult = reportBuilder.format(testResult, hermione.events.RETRY);
formattedResult.attempt = reportBuilder.getCurrAttempt(formattedResult);

await failHandler(formattedResult);
await failHandler(testResult, formattedResult);
reportBuilder.addRetry(formattedResult);

const testBranch = reportBuilder.getTestBranch(formattedResult.id);
Expand All @@ -79,7 +80,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
const formattedResult = reportBuilder.format(testResult, hermione.events.TEST_FAIL);
formattedResult.attempt = reportBuilder.getCurrAttempt(formattedResult);

await failHandler(formattedResult);
await failHandler(testResult, formattedResult);
formattedResult.hasDiff()
? reportBuilder.addFail(formattedResult)
: reportBuilder.addError(formattedResult);
Expand All @@ -94,7 +95,7 @@ module.exports = (hermione, reportBuilder, client, reportPath) => {
const formattedResult = reportBuilder.format(testResult, hermione.events.TEST_PENDING);
formattedResult.attempt = reportBuilder.getCurrAttempt(formattedResult);

await failHandler(formattedResult);
await failHandler(testResult, formattedResult);
reportBuilder.addSkipped(formattedResult);

const testBranch = reportBuilder.getTestBranch(formattedResult.id);
Expand Down
3 changes: 3 additions & 0 deletions lib/image-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const cacheAllImages = new Map<string, string>();
export const cacheDiffImages = new Map<string, string>();
export const cacheExpectedPaths = new Map<string, string>();
Loading

0 comments on commit 469387f

Please sign in to comment.