Skip to content

Commit

Permalink
refactor: move image processing logic out of test-adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowusr committed Aug 31, 2023
1 parent 9d468cd commit bccefe2
Show file tree
Hide file tree
Showing 24 changed files with 1,422 additions and 1,197 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
2 changes: 1 addition & 1 deletion lib/gui/tool-runner/index.js
Original file line number Diff line number Diff line change
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 bccefe2

Please sign in to comment.