Skip to content

Commit

Permalink
Merge pull request #483 from gemini-testing/HERMIONE-1088.html-report…
Browse files Browse the repository at this point in the history
…er_diff_buffer

perf: use diffBuffer, provided from hermione
  • Loading branch information
KuznetsovRoman authored Aug 21, 2023
2 parents b19fab3 + cab7ecb commit 76b8fe3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
20 changes: 14 additions & 6 deletions lib/test-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,16 @@ export class TestAdapter {
return this._errorDetails;
}

getRefImg(stateName?: string): ImageData | undefined {
return _.get(_.find(this.assertViewResults, {stateName}), 'refImg');
getRefImg(stateName?: string): ImageData | Record<string, never> {
return _.get(_.find(this.assertViewResults, {stateName}), 'refImg', {});
}

getCurrImg(stateName?: string): ImageData | undefined {
return _.get(_.find(this.assertViewResults, {stateName}), 'currImg');
getCurrImg(stateName?: string): ImageData | Record<string, never> {
return _.get(_.find(this.assertViewResults, {stateName}), 'currImg', {});
}

getErrImg(): ImageBase64 | undefined {
return this.screenshot;
getErrImg(): ImageBase64 | Record<string, never> {
return this.screenshot || {};
}

prepareTestResult(): PrepareTestResultData {
Expand Down Expand Up @@ -501,6 +501,14 @@ export class TestAdapter {
protected async _saveDiffInWorker(imageDiffError: ImageDiffError, destPath: string, workers: typeof Workers, cacheDiffImages = globalCacheDiffImages): Promise<void> {
await utils.makeDirFor(destPath);

if (imageDiffError.diffBuffer) { // new versions of hermione provide `diffBuffer`
const pngBuffer = Buffer.from(imageDiffError.diffBuffer);

await fs.writeFile(destPath, pngBuffer);

return;
}

const currPath = imageDiffError.currImg.path;
const refPath = imageDiffError.refImg.path;

Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface ImageDiffError {
currImg: ImageData;
refImg: ImageData;
diffClusters: CoordBounds[];
diffBuffer?: ArrayBuffer;
}

export type AssertViewResult = ImageDiffError;
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"inquirer": "^8.2.0",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.4",
"looks-same": "^8.1.0",
"looks-same": "^8.2.1",
"nested-error-stacks": "^2.1.0",
"opener": "^1.4.3",
"ora": "^5.4.1",
Expand Down
30 changes: 21 additions & 9 deletions test/unit/lib/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ describe('hermione test adapter', () => {
fullTitle: () => 'default-title'
});

const mkErrStub = (ErrType = ImageDiffError, {stateName, currImg, refImg} = {}) => {
const mkErrStub = (ErrType = ImageDiffError, {stateName, currImg, refImg, diffBuffer} = {}) => {
const err = new ErrType();

err.stateName = stateName || 'plain';
err.currImg = currImg || {path: 'curr/path'};
err.refImg = refImg || {path: 'ref/path'};
err.diffBuffer = diffBuffer;

return err;
};
Expand All @@ -76,8 +77,11 @@ describe('hermione test adapter', () => {
sandbox.stub(utils, 'getCurrentPath').returns('');
sandbox.stub(utils, 'getDiffPath').returns('');
sandbox.stub(utils, 'getReferencePath').returns('');

fs.readFile.resolves(Buffer.from(''));
fs.writeFile.resolves();
fs.copy.resolves();

err = mkErrStub();
});

Expand Down Expand Up @@ -231,21 +235,17 @@ describe('hermione test adapter', () => {
});

describe('saveErrorDetails', () => {
let fsWriteFile;

beforeEach(() => {
sandbox.stub(utils, 'makeDirFor').resolves();
sandbox.stub(utils, 'getDetailsFileName').returns('md5-bro-n-time');

fsWriteFile = fs.writeFile.resolves();
});

it('should do nothing if no error details are available', async () => {
const hermioneTestAdapter = mkHermioneTestResultAdapter(mkTestResult_({err: {}}));

await hermioneTestAdapter.saveErrorDetails();

assert.notCalled(fsWriteFile);
assert.notCalled(fs.writeFile);
});

it('should save error details to correct path', async () => {
Expand All @@ -257,7 +257,7 @@ describe('hermione test adapter', () => {

await hermioneTestAdapter.saveErrorDetails('report-path');

assert.calledWithMatch(fsWriteFile, `report-path/${filePath}`, sinon.match.any);
assert.calledWithMatch(fs.writeFile, `report-path/${filePath}`, sinon.match.any);
});

it('should create directory for error details', async () => {
Expand All @@ -276,7 +276,7 @@ describe('hermione test adapter', () => {

await hermioneTestAdapter.saveErrorDetails('');

assert.calledWith(fsWriteFile, sinon.match.any, JSON.stringify(data, null, 2));
assert.calledWith(fs.writeFile, sinon.match.any, JSON.stringify(data, null, 2));
});
});

Expand Down Expand Up @@ -347,7 +347,6 @@ describe('hermione test adapter', () => {
beforeEach(() => {
sandbox.stub(logger, 'warn');
sandbox.stub(utils, 'makeDirFor').resolves();
fs.writeFile.resolves();
sandbox.stub(utils, 'copyFileAsync');
});

Expand Down Expand Up @@ -457,6 +456,19 @@ describe('hermione test adapter', () => {
{destPath: 'ref/report/path', reportDir: 'html-report/path'}
);
});

it('should save png buffer, if it is passed', async () => {
const error = mkErrStub(ImageDiffError, {stateName: 'plain', diffBuffer: 'foo'});
const testResult = mkTestResult_({assertViewResults: [error]});
utils.getDiffPath.returns('diff/report/path');

const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult);
const workers = {saveDiffTo: sandbox.stub()};
await hermioneTestAdapter.saveTestImages('', workers);

assert.calledOnceWith(fs.writeFile, sinon.match('diff/report/path'), Buffer.from('foo'));
assert.notCalled(workers.saveDiffTo);
});
});
});

Expand Down

0 comments on commit 76b8fe3

Please sign in to comment.