embark/lib/modules/tests/reporter.js

157 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-06-28 18:01:44 +00:00
const Base = require('mocha/lib/reporters/base');
2018-06-29 14:29:07 +00:00
const ms = require('mocha/lib/ms');
2018-06-28 18:01:44 +00:00
const color = Base.color;
class EmbarkSpec extends Base {
constructor(runner, options) {
super(runner, options);
const self = this;
2018-09-17 13:12:20 +00:00
self.listenForGas = true;
2018-06-28 18:01:44 +00:00
self.embarkEvents = options.reporterOptions.events;
self.gasDetails = options.reporterOptions.gasDetails;
2018-06-28 20:14:34 +00:00
self.gasLimit = options.reporterOptions.gasLimit;
2018-06-28 18:01:44 +00:00
let indents = 0;
let n = 0;
2018-06-29 14:29:07 +00:00
self.stats.totalGasCost = 0;
self.stats.test = {};
self.stats.test.gasUsed = 0;
2018-06-28 18:01:44 +00:00
function onContractReceipt(receipt) {
2018-07-06 19:16:04 +00:00
const fmt = color('bright pass', ' ') +
color('suite', ' %s') +
color('light', ' deployed for ') +
color(self.getGasColor(receipt.gasUsed), '%s') +
color('light', ' gas');
2018-07-06 19:16:04 +00:00
console.log(fmt, receipt.className, receipt.gasUsed);
2018-06-28 18:01:44 +00:00
}
2018-06-29 14:29:07 +00:00
function onBlockHeader(blockHeader) {
self.stats.totalGasCost += blockHeader.gasUsed;
self.stats.test.gasUsed += blockHeader.gasUsed;
}
2018-07-06 19:16:04 +00:00
2018-06-29 14:29:07 +00:00
function onBlockHeader(blockHeader) {
2018-09-17 13:12:20 +00:00
if(!self.listenForGas) {
return;
}
2018-06-29 14:29:07 +00:00
self.stats.totalGasCost += blockHeader.gasUsed;
self.stats.test.gasUsed += blockHeader.gasUsed;
}
2018-06-28 19:18:28 +00:00
2018-07-06 19:16:04 +00:00
if (self.gasDetails) {
self.embarkEvents.on("deploy:contract:receipt", onContractReceipt);
}
2018-06-29 14:29:07 +00:00
self.embarkEvents.on("block:header", onBlockHeader);
2018-09-17 13:12:20 +00:00
self.embarkEvents.setCommandHandler("reporter:toggleGasListener", () => {
self.listenForGas = !self.listenForGas;
});
2018-06-28 18:01:44 +00:00
function indent() {
return Array(indents).join(' ');
}
runner.on('start', function () {
console.log();
});
runner.on('suite', function (suite) {
++indents;
if (self.gasDetails) {
console.log();
}
2018-06-28 18:01:44 +00:00
console.log(color('suite', '%s%s'), indent(), suite.title);
});
runner.on('suite end', function () {
--indents;
if (indents === 1) {
console.log();
}
});
runner.on('pending', function (test) {
const fmt = indent() + color('pending', ' - %s');
console.log(fmt, test.title);
});
2018-06-29 14:29:07 +00:00
runner.on('test', function () {
self.stats.test.gasUsed = 0;
});
2018-06-28 18:01:44 +00:00
runner.on('pass', function (test) {
2018-06-29 14:29:07 +00:00
let fmt =
indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
color(test.speed, ' (%dms)') +
' - ' +
color(self.getGasColor(self.stats.test.gasUsed), '[%d gas]');
console.log(fmt, test.title, test.duration, self.stats.test.gasUsed);
2018-06-28 18:01:44 +00:00
});
runner.on('fail', function (test) {
2018-06-29 14:29:07 +00:00
console.log(indent() + color('fail', ' %d) %s') + ' - ' + color(self.getGasColor(self.stats.test.gasUsed), '[%d gas]'),
++n, test.title, self.stats.test.gasUsed);
2018-06-28 18:01:44 +00:00
});
2018-06-28 19:18:28 +00:00
runner.once('end', function () {
runner.removeAllListeners();
2018-06-28 18:01:44 +00:00
self.embarkEvents.removeListener("deploy:contract:receipt", onContractReceipt);
2018-06-29 14:29:07 +00:00
self.embarkEvents.removeListener("block:header", onBlockHeader);
2018-06-28 18:01:44 +00:00
self.epilogue();
});
}
2018-06-28 19:18:28 +00:00
2018-06-28 20:14:34 +00:00
getGasColor(gasCost) {
2018-07-06 19:16:04 +00:00
if (gasCost <= this.gasLimit / 10) {
2018-06-28 20:14:34 +00:00
return 'fast';
}
2018-07-06 19:16:04 +00:00
if (gasCost <= 3 * (this.gasLimit / 4)) {
2018-06-28 20:14:34 +00:00
return 'medium';
}
return 'slow';
}
2018-06-28 19:18:28 +00:00
epilogue() {
const stats = this.stats;
let fmt;
console.log();
// passes
fmt = color('bright pass', ' ') +
color('green', ' %d passing') +
2018-06-29 14:29:07 +00:00
color('light', ' (%s)') +
color('light', ' - [Total: %s gas]');
2018-06-28 19:18:28 +00:00
console.log(fmt,
stats.passes || 0,
2018-06-29 14:29:07 +00:00
ms(stats.duration),
stats.totalGasCost);
2018-06-28 19:18:28 +00:00
// pending
if (stats.pending) {
fmt = color('pending', ' ') +
color('pending', ' %d pending');
console.log(fmt, stats.pending);
}
// failures
if (stats.failures) {
fmt = color('fail', ' %d failing');
console.log(fmt, stats.failures);
Base.list(this.failures);
console.log();
}
console.log();
}
2018-06-28 18:01:44 +00:00
}
module.exports = EmbarkSpec;