Skip to content

Commit

Permalink
feat: save diff from hermione assert view (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
rostik404 authored Feb 16, 2018
1 parent 5c6a5ba commit f4e1c92
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 49 deletions.
31 changes: 4 additions & 27 deletions gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const Promise = require('bluebird');

const utils = require('./utils');
const {saveTestImages} = require('./lib/reporter-helpers');
const ReportBuilderFactory = require('./lib/report-builder-factory');
const parseConfig = require('./lib/config');

Expand Down Expand Up @@ -54,30 +55,6 @@ function prepareImages(gemini, pluginConfig) {
return src && utils.copyImageAsync(src, utils.getCurrentAbsolutePath(result, pluginConfig.path));
}

function handleTestResultEvent(testResult) {
const actions = [
utils.copyImageAsync(
testResult.referencePath,
utils.getReferenceAbsolutePath(testResult, pluginConfig.path)
)
];

if (!testResult.equal) {
actions.push(
utils.copyImageAsync(
testResult.currentPath,
utils.getCurrentAbsolutePath(testResult, pluginConfig.path)
),
utils.saveDiff(
testResult,
utils.getDiffAbsolutePath(testResult, pluginConfig.path)
)
);
}

return Promise.all(actions);
}

return new Promise((resolve, reject) => {
let queue = Promise.resolve();

Expand All @@ -90,13 +67,13 @@ function prepareImages(gemini, pluginConfig) {

queue = queue.then(() => {
return wrapped.isEqual()
? handleTestResultEvent(wrapped)
? saveTestImages(wrapped, pluginConfig)
: handleErrorEvent(wrapped);
});
});

gemini.on(gemini.events.TEST_RESULT, (testResult) => {
queue = queue.then(() => handleTestResultEvent(reportBuilder.format(testResult)));
queue = queue.then(() => saveTestImages(reportBuilder.format(testResult), pluginConfig));
});

gemini.on(gemini.events.UPDATE_RESULT, (testResult) => {
Expand All @@ -105,7 +82,7 @@ function prepareImages(gemini, pluginConfig) {
equal: true
});

queue = queue.then(() => handleTestResultEvent(reportBuilder.format(testResult)));
queue = queue.then(() => saveTestImages(reportBuilder.format(testResult), pluginConfig));
});

gemini.on(gemini.events.END, () => queue.then(resolve, reject));
Expand Down
25 changes: 19 additions & 6 deletions hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Promise = require('bluebird');
const fs = require('fs-extra');

const utils = require('./utils');
const {saveTestImages} = require('./lib/reporter-helpers');
const ReportBuilderFactory = require('./lib/report-builder-factory');
const parseConfig = require('./lib/config');

Expand Down Expand Up @@ -47,16 +48,22 @@ function prepareData(hermione, pluginConfig) {
browserConfig.screenshotOnReject = pluginConfig.screenshotOnReject;
});

hermione.on(hermione.events.TEST_PENDING, (result) => reportBuilder.addSkipped(result));
hermione.on(hermione.events.TEST_PENDING, (testResult) => reportBuilder.addSkipped(testResult));

hermione.on(hermione.events.TEST_PASS, (result) => reportBuilder.addSuccess(result));
hermione.on(hermione.events.TEST_PASS, (testResult) => reportBuilder.addSuccess(testResult));

hermione.on(hermione.events.TEST_FAIL, (result) => reportBuilder.addError(result));
hermione.on(hermione.events.TEST_FAIL, failHandler);

hermione.on(hermione.events.RETRY, (result) => reportBuilder.addRetry(result));
hermione.on(hermione.events.RETRY, failHandler);

hermione.on(hermione.events.RUNNER_END, (stats) => resolve(reportBuilder.setStats(stats)));
});

function failHandler(testResult) {
const wrapped = reportBuilder.format(testResult);

return wrapped.hasDiff ? reportBuilder.addFail(testResult) : reportBuilder.addError(testResult);
}
}

function prepareImages(hermione, pluginConfig) {
Expand All @@ -68,15 +75,21 @@ function prepareImages(hermione, pluginConfig) {
: saveScreenshot(testResult.screenshot, utils.getCurrentAbsolutePath(testResult, pluginConfig.path));
}

function failHandler(testResult) {
const wrapped = reportBuilder.format(testResult);

return wrapped.hasDiff ? saveTestImages(wrapped, pluginConfig) : handleErrorEvent(wrapped);
}

return new Promise((resolve, reject) => {
let queue = Promise.resolve();

hermione.on(hermione.events.RETRY, (testResult) => {
queue = queue.then(() => handleErrorEvent(reportBuilder.format(testResult)));
queue = queue.then(() => failHandler(testResult));
});

hermione.on(hermione.events.TEST_FAIL, (testResult) => {
queue = queue.then(() => handleErrorEvent(reportBuilder.format(testResult)));
queue = queue.then(() => failHandler(testResult));
});

hermione.on(hermione.events.RUNNER_END, () => queue.then(resolve, reject));
Expand Down
27 changes: 27 additions & 0 deletions lib/reporter-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const utils = require('../utils');

exports.saveTestImages = function(testResult, pluginConfig) {
const actions = [
utils.copyImageAsync(
testResult.referencePath,
utils.getReferenceAbsolutePath(testResult, pluginConfig.path)
)
];

if (!testResult.equal) {
actions.push(
utils.copyImageAsync(
testResult.currentPath,
utils.getCurrentAbsolutePath(testResult, pluginConfig.path)
),
utils.saveDiff(
testResult,
utils.getDiffAbsolutePath(testResult, pluginConfig.path)
)
);
}

return Promise.all(actions);
};
10 changes: 10 additions & 0 deletions lib/test-adapter/hermione-test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const _ = require('lodash');
const TestAdapter = require('./test-adapter');
const HermioneSuiteAdapter = require('../suite-adapter/hermione-suite-adapter.js');

const IMAGE_DIFF_ERROR = 'ImageDiffError';

module.exports = class HermioneTestResultAdapter extends TestAdapter {
constructor(testResult, config = {}) {
super(testResult);
Expand All @@ -12,6 +14,14 @@ module.exports = class HermioneTestResultAdapter extends TestAdapter {
this._suite = HermioneSuiteAdapter.create(this._testResult);
}

saveDiffTo(...args) {
return this._testResult.err.saveDiffTo(...args);
}

get hasDiff() {
return this._testResult.err.type === IMAGE_DIFF_ERROR;
}

get error() {
return JSON.stringify(_.pick(this._testResult.err, 'message', 'stack', 'stateName'), null, 2);
}
Expand Down
72 changes: 56 additions & 16 deletions test/hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const proxyquire = require('proxyquire');
const utils = require('../utils');
const ReportBuilder = require('../lib/report-builder-factory/report-builder');
const logger = utils.logger;
const HermioneTestAdapter = require('../lib/test-adapter/hermione-test-adapter');

describe('Hermione Reporter', () => {
const sandbox = sinon.sandbox.create();
Expand Down Expand Up @@ -42,8 +43,8 @@ describe('Hermione Reporter', () => {
HermioneReporter(hermione, opts);
}

function mkStubResult_(options) {
return _.defaultsDeep(options, {err: {}});
function mkStubResult_(options = {}) {
return _.defaultsDeep(options, {err: {type: options.diff && 'ImageDiffError'}});
}

beforeEach(() => {
Expand All @@ -57,16 +58,18 @@ describe('Hermione Reporter', () => {
sandbox.stub(fs, 'writeFileAsync').returns(Promise.resolve());
sandbox.stub(utils, 'copyImageAsync');
sandbox.stub(utils, 'getCurrentAbsolutePath');
sandbox.stub(utils, 'getReferenceAbsolutePath');
sandbox.stub(utils, 'saveDiff');
sandbox.stub(utils, 'getDiffAbsolutePath');

sandbox.stub(ReportBuilder.prototype, 'addSkipped');
sandbox.stub(ReportBuilder.prototype, 'addSuccess');
sandbox.stub(ReportBuilder.prototype, 'addError');
sandbox.stub(ReportBuilder.prototype, 'addFail');
sandbox.stub(ReportBuilder.prototype, 'addRetry');
sandbox.stub(ReportBuilder.prototype, 'save').resolves({});

sandbox.spy(ReportBuilder.prototype, 'setStats');

initReporter_();
});

afterEach(() => sandbox.restore());
Expand All @@ -93,20 +96,24 @@ describe('Hermione Reporter', () => {
assert.calledOnceWith(ReportBuilder.prototype.addSuccess, {title: 'some-title'});
});

it('should add failed test to result', () => {
initReporter_();
['TEST_FAIL', 'RETRY'].forEach((event) => {
describe('should add', () => {
it(`errored test to result on ${event} event`, () => {
initReporter_();

hermione.emit(events.TEST_FAIL, mkStubResult_({title: 'some-title'}));
hermione.emit(events[event], mkStubResult_({title: 'some-title'}));

assert.calledOnceWith(ReportBuilder.prototype.addError, sinon.match({title: 'some-title'}));
});
assert.calledOnceWith(ReportBuilder.prototype.addError, sinon.match({title: 'some-title'}));
});

it('should add retried test to result', () => {
initReporter_();
it('failed test to result on ${event} event', () => {
initReporter_();

hermione.emit(events.RETRY, mkStubResult_({title: 'some-title'}));
hermione.emit(events[event], mkStubResult_({title: 'some-title', diff: true}));

assert.calledOnceWith(ReportBuilder.prototype.addRetry, sinon.match({title: 'some-title'}));
assert.calledOnceWith(ReportBuilder.prototype.addFail, sinon.match({title: 'some-title'}));
});
});
});

it('should save statistic', () => {
Expand Down Expand Up @@ -134,13 +141,46 @@ describe('Hermione Reporter', () => {
});

it('should save image from error', () => {
utils.getCurrentAbsolutePath.returns('/absolute/path');
utils.getCurrentAbsolutePath.returns('/absolute/report');

initReporter_();
hermione.emit(events.RETRY, {err: {currentImagePath: 'current/path'}});

return hermione.emitAndWait(events.RUNNER_END).then(() => {
assert.calledOnceWith(utils.copyImageAsync, 'current/path', '/absolute/report');
});
});

it('should save reference image from fail', () => {
utils.getReferenceAbsolutePath.returns('/absolute/report');

initReporter_();
hermione.emit(events.TEST_FAIL, mkStubResult_({err: {refImagePath: 'reference/path'}, diff: true}));

return hermione.emitAndWait(events.RUNNER_END).then(() => {
assert.calledWith(utils.copyImageAsync, 'reference/path', '/absolute/report');
});
});

it('should save current image from fail', () => {
utils.getCurrentAbsolutePath.returns('/absolute/report');

initReporter_();
hermione.emit(events.TEST_FAIL, mkStubResult_({err: {currentImagePath: 'current/path'}, diff: true}));

return hermione.emitAndWait(events.RUNNER_END).then(() => {
assert.calledWith(utils.copyImageAsync, 'current/path', '/absolute/report');
});
});

it('should save current diff image from fail', () => {
utils.getDiffAbsolutePath.returns('/absolute/report');

initReporter_();
hermione.emit(events.RETRY, {err: {currentImagePath: 'image/path'}});
hermione.emit(events.TEST_FAIL, mkStubResult_({diff: true}));

return hermione.emitAndWait(events.RUNNER_END).then(() => {
assert.calledOnceWith(utils.copyImageAsync, 'image/path', '/absolute/path');
assert.calledWith(utils.saveDiff, sinon.match.instanceOf(HermioneTestAdapter), '/absolute/report');
});
});

Expand Down

0 comments on commit f4e1c92

Please sign in to comment.