Skip to content

Commit 3b0ac4a

Browse files
Merge pull request #472 from gemini-testing/HERMIONE-969.reused_refs_fs
fix: dont overwrite reused references on file system
2 parents fb4a5d6 + 9df6eae commit 3b0ac4a

File tree

3 files changed

+87
-19
lines changed

3 files changed

+87
-19
lines changed

lib/test-adapter.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,43 @@ module.exports = class TestAdapter {
101101
return [id.toString(), browserId, stateName].join('/');
102102
}
103103

104-
_getExpectedPath(stateName, status) {
104+
_getExpectedPath(stateName, status, cacheExpectedPaths) {
105105
const key = this._getExpectedKey(stateName);
106106

107107
if (status === UPDATED) {
108108
const expectedPath = utils.getReferencePath(this, stateName);
109-
globalCacheExpectedPaths.set(key, expectedPath);
109+
cacheExpectedPaths.set(key, expectedPath);
110110

111-
return expectedPath;
111+
return {path: expectedPath, reused: false};
112112
}
113113

114-
if (globalCacheExpectedPaths.has(key)) {
115-
return globalCacheExpectedPaths.get(key);
114+
if (cacheExpectedPaths.has(key)) {
115+
return {path: cacheExpectedPaths.get(key), reused: true};
116116
}
117117

118118
const imageInfo = this._getLastImageInfoFromDB(stateName);
119-
const expectedPath = imageInfo && imageInfo.expectedImg
120-
? imageInfo.expectedImg.path
121-
: utils.getReferencePath(this, stateName);
122119

123-
globalCacheExpectedPaths.set(key, expectedPath);
120+
if (imageInfo && imageInfo.expectedImg) {
121+
const expectedPath = imageInfo.expectedImg.path;
124122

125-
return expectedPath;
123+
cacheExpectedPaths.set(key, expectedPath);
124+
125+
return {path: expectedPath, reused: true};
126+
}
127+
128+
const expectedPath = utils.getReferencePath(this, stateName);
129+
130+
cacheExpectedPaths.set(key, expectedPath);
131+
132+
return {path: expectedPath, reused: false};
126133
}
127134

128135
getImagesFor(status, stateName) {
129136
const refImg = this.getRefImg(stateName);
130137
const currImg = this.getCurrImg(stateName);
131138
const errImg = this.getErrImg();
132139

133-
const refPath = this._getExpectedPath(stateName, status);
140+
const {path: refPath} = this._getExpectedPath(stateName, status, globalCacheExpectedPaths);
134141
const currPath = utils.getCurrentPath(this, stateName);
135142
const diffPath = utils.getDiffPath(this, stateName);
136143

@@ -373,10 +380,10 @@ module.exports = class TestAdapter {
373380
await fs.writeFile(detailsFilePath, detailsData);
374381
}
375382

376-
async saveTestImages(reportPath, workers) {
383+
async saveTestImages(reportPath, workers, cacheExpectedPaths = globalCacheExpectedPaths) {
377384
const result = await Promise.all(this.assertViewResults.map(async (assertResult) => {
378385
const {stateName} = assertResult;
379-
const destRefPath = this._getExpectedPath(stateName);
386+
const {path: destRefPath, reused: reusedReference} = this._getExpectedPath(stateName, undefined, cacheExpectedPaths);
380387
const srcRefPath = this.getRefImg(stateName).path;
381388

382389
const destCurrPath = utils.getCurrentPath(this, stateName);
@@ -396,8 +403,11 @@ module.exports = class TestAdapter {
396403
actions.push(
397404
this._saveImg(srcCurrPath, destCurrPath, reportPath),
398405
this._saveImg(srcDiffPath, dstCurrPath, reportPath),
399-
this._saveImg(srcRefPath, destRefPath, reportPath)
400406
);
407+
408+
if (!reusedReference) {
409+
actions.push(this._saveImg(srcRefPath, destRefPath, reportPath));
410+
}
401411
}
402412

403413
if (this.isNoRefImageError(assertResult)) {

test/unit/hermione.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const HermioneReporter = require('../../hermione');
66
const PluginAdapter = require('lib/plugin-adapter');
77
const StaticReportBuilder = require('lib/report-builder/static');
88
const SqliteAdapter = require('lib/sqlite-adapter');
9+
const TestAdapter = require('lib/test-adapter');
910
const utils = require('lib/server-utils');
1011
const {logger} = require('lib/common-utils');
1112
const {stubTool} = require('./utils');
@@ -114,6 +115,11 @@ describe('lib/hermione', () => {
114115

115116
sandbox.stub(SqliteAdapter.prototype, 'query');
116117

118+
const saveTestImagesImplementation = TestAdapter.prototype.saveTestImages;
119+
sandbox.stub(TestAdapter.prototype, 'saveTestImages').callsFake(function(...args) {
120+
return saveTestImagesImplementation.call(this, ...args, new Map()); // disable expectedPath cache
121+
});
122+
117123
sandbox.stub(fs, 'readFile').resolves(Buffer.from(''));
118124
});
119125

test/unit/lib/test-adapter.js

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ describe('hermione test adapter', () => {
4545
fullTitle: () => 'default-title'
4646
});
4747

48-
const mkErrStub = (ErrType = ImageDiffError) => {
48+
const mkErrStub = (ErrType = ImageDiffError, {stateName, currImg, refImg} = {}) => {
4949
const err = new ErrType();
5050

51-
err.stateName = 'plain';
52-
err.currImg = {path: 'curr/path'};
53-
err.refImg = {path: 'ref/path'};
51+
err.stateName = stateName || 'plain';
52+
err.currImg = currImg || {path: 'curr/path'};
53+
err.refImg = refImg || {path: 'ref/path'};
5454

5555
return err;
5656
};
@@ -68,6 +68,7 @@ describe('hermione test adapter', () => {
6868
});
6969
sandbox.stub(utils, 'getCurrentPath').returns('');
7070
sandbox.stub(utils, 'getDiffPath').returns('');
71+
sandbox.stub(utils, 'getReferencePath').returns('');
7172
sandbox.stub(fs, 'readFile').resolves(Buffer.from(''));
7273
sandbox.stub(fs, 'copy').resolves();
7374
err = mkErrStub();
@@ -399,6 +400,57 @@ describe('hermione test adapter', () => {
399400
assert.calledWith(imagesSaver.saveImg, sinon.match('dest/path'), {destPath: 'dest/path', reportDir: 'report/path'});
400401
});
401402
});
403+
404+
describe('saving reference image', () => {
405+
it('should save reference, if it is not reused', async () => {
406+
tmp.tmpdir = 'tmp/dir';
407+
const testResult = mkTestResult_({assertViewResults: [err]});
408+
utils.getReferencePath.returns('ref/report/path');
409+
const imagesSaver = {saveImg: sandbox.stub()};
410+
const cacheExpectedPaths = new Map();
411+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult, {
412+
htmlReporter: {
413+
imagesSaver
414+
}
415+
});
416+
417+
await hermioneTestAdapter.saveTestImages(
418+
'html-report/path',
419+
{saveDiffTo: sandbox.stub()},
420+
cacheExpectedPaths
421+
);
422+
423+
assert.calledWith(
424+
imagesSaver.saveImg, 'ref/path',
425+
{destPath: 'ref/report/path', reportDir: 'html-report/path'}
426+
);
427+
});
428+
429+
it('should not save reference, if it is reused', async () => {
430+
tmp.tmpdir = 'tmp/dir';
431+
const error = mkErrStub(ImageDiffError, {stateName: 'plain'});
432+
const testResult = mkTestResult_({assertViewResults: [error], browserId: 'browser-id'});
433+
utils.getReferencePath.returns('ref/report/path');
434+
const imagesSaver = {saveImg: sandbox.stub()};
435+
const cacheExpectedPaths = new Map([['some-id/browser-id/plain', 'ref/report/path']]);
436+
const hermioneTestAdapter = mkHermioneTestResultAdapter(testResult, {
437+
htmlReporter: {
438+
imagesSaver
439+
}
440+
});
441+
442+
await hermioneTestAdapter.saveTestImages(
443+
'html-report/path',
444+
{saveDiffTo: sandbox.stub()},
445+
cacheExpectedPaths
446+
);
447+
448+
assert.neverCalledWith(
449+
imagesSaver.saveImg, 'ref/path',
450+
{destPath: 'ref/report/path', reportDir: 'html-report/path'}
451+
);
452+
});
453+
});
402454
});
403455

404456
describe('hasDiff()', () => {
@@ -510,7 +562,7 @@ describe('hermione test adapter', () => {
510562
describe('getImagesInfo()', () => {
511563
beforeEach(() => {
512564
sandbox.stub(utils, 'copyFileAsync');
513-
sandbox.stub(utils, 'getReferencePath').returns('some/ref.png');
565+
utils.getReferencePath.returns('some/ref.png');
514566
});
515567

516568
it('should not reinit "imagesInfo"', () => {

0 commit comments

Comments
 (0)