Skip to content

Commit

Permalink
feat: allow to reuse reports of different formats
Browse files Browse the repository at this point in the history
  • Loading branch information
rmdm committed Mar 31, 2020
1 parent 6aa68db commit 274efdd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
23 changes: 12 additions & 11 deletions lib/gui/tool-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ module.exports = class ToolRunner {
this._eventSource = new EventSource();
this._reportBuilder = null;

this.saveResultsToDb = isSqlite(pluginConfig.saveFormat);
this._tests = {};
}

Expand All @@ -59,13 +58,17 @@ module.exports = class ToolRunner {
}

async initialize() {
// TODO: remove creating 'report-builder-json' when making saving to DB the default save format
this._reuseDataJs = this._readReuseDataJs();
const reuseFormat = this._reuseDataJs ? this._reuseDataJs.saveFormat : this._pluginConfig.saveFormat;
this.saveResultsToDb = isSqlite(reuseFormat);

if (this.saveResultsToDb) {
await mergeDatabasesForReuse(this._reportPath);
this._reportBuilder = ReportBuilderSqlite.create(this._hermione, this._pluginConfig, {reuse: true});
await this._reportBuilder.init();
await this._reportBuilder.saveStaticFiles();
} else {
// TODO: remove creating 'report-builder-json' when making saving to DB the default save format
this._reportBuilder = ReportBuilderJson.create(this._hermione, this._pluginConfig);
}

Expand Down Expand Up @@ -139,24 +142,22 @@ module.exports = class ToolRunner {
return testSuites.map((suite) => applyReuse(reuseData)(suite));
}

async _loadReuseData() {
let data = {};

_readReuseDataJs() {
// TODO: remove loading "data.js" with format save "js"
try {
data = utils.require(path.resolve(this._reportPath, 'data'));
return utils.require(path.resolve(this._reportPath, 'data'));
} catch (e) {
logger.warn(chalk.yellow(`Nothing to reuse in ${this._reportPath}: can not load data.js`));
return {};
return null;
}
}

if (data.saveFormat !== this._pluginConfig.saveFormat) {
logger.warn(chalk.yellow(`Nothing to reuse in ${this._reportPath}: saveFormat from data.js does not equal saveFormat from plugin options.`));
async _loadReuseData() {
if (!this._reuseDataJs) {
return {};
}

if (!this.saveResultsToDb) {
return data;
return this._reuseDataJs;
}

const dbPath = path.resolve(this._reportPath, constantFileNames.LOCAL_DATABASE_NAME);
Expand Down
49 changes: 33 additions & 16 deletions test/unit/lib/gui/tool-runner/base-tool-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const path = require('path');
const _ = require('lodash');
const proxyquire = require('proxyquire');
const ReportBuilder = require('lib/report-builder/report-builder-json');
const ReportBuilderJson = require('lib/report-builder/report-builder-json');
const ReportBuilderSqlite = require('lib/report-builder/report-builder-sqlite');
const {stubTool, stubConfig, mkSuite, mkState, mkBrowserResult, mkSuiteTree} = require('../../../utils');
const serverUtils = require('lib/server-utils');

Expand Down Expand Up @@ -35,14 +36,20 @@ describe('lib/gui/tool-runner/base-tool-runner', () => {
return ToolGuiReporter.create(opts.paths, tool, configs);
};

const createReportBuilder = (ReportBuilder) => {
const builder = sinon.createStubInstance(ReportBuilder);
sandbox.stub(ReportBuilder, 'create').returns(builder);
builder.getResult.returns({});
builder.finalize.returns({});
return builder;
};

beforeEach(() => {
sandbox.stub(serverUtils.logger, 'warn');
sandbox.stub(serverUtils, 'require').returns({});

reportBuilder = sinon.createStubInstance(ReportBuilder);
sandbox.stub(ReportBuilder, 'create').returns(reportBuilder);
reportBuilder.getResult.returns({});
reportBuilder.finalize.returns({});
reportBuilder = createReportBuilder(ReportBuilderJson);
createReportBuilder(ReportBuilderSqlite);

tool = mkTool_();
tool.readTests.resolves(mkHermioneTestCollection_());
Expand Down Expand Up @@ -103,20 +110,10 @@ describe('lib/gui/tool-runner/base-tool-runner', () => {
});

describe(`reuse hermione data`, () => {
it('should not try load data for reuse if suites are empty', () => {
const gui = initGuiReporter({configs: mkPluginConfig_({path: 'report_path'})});

return gui.initialize()
.then(() => assert.notCalled(serverUtils.require));
});

it('should try to load data for reuse', () => {
it('should load data for reuse', () => {
const gui = initGuiReporter({configs: mkPluginConfig_({path: 'report_path'})});
const reusePath = path.resolve(process.cwd(), 'report_path/data');

const suites = [mkSuiteTree()];
reportBuilder.getResult.returns({suites});

return gui.initialize()
.then(() => assert.calledOnceWith(serverUtils.require, reusePath));
});
Expand All @@ -142,6 +139,26 @@ describe('lib/gui/tool-runner/base-tool-runner', () => {
.then(() => assert.calledWithMatch(serverUtils.logger.warn, 'Nothing to reuse'));
});

it('should use config saveFormat option when no actual data found', async () => {
const gui = initGuiReporter({configs: mkPluginConfig_({path: 'report_path', saveFormat: 'js'})});
serverUtils.require.returns(null);

await gui.initialize();

assert.calledOnce(ReportBuilderJson.create);
assert.notCalled(ReportBuilderSqlite.create);
});

it('should prefer existing report saveFormat over the config one', async () => {
const gui = initGuiReporter({configs: mkPluginConfig_({path: 'report_path', saveFormat: 'js'})});
serverUtils.require.returns({saveFormat: 'sqlite'});

await gui.initialize();

assert.calledOnce(ReportBuilderSqlite.create);
assert.notCalled(ReportBuilderJson.create);
});

describe('should not apply reuse data if', () => {
it('it does not exist', () => {
const gui = initGuiReporter();
Expand Down

0 comments on commit 274efdd

Please sign in to comment.