add option to show details of gas cost for each contract

This commit is contained in:
Jonathan Rainville 2018-06-28 15:48:57 -04:00 committed by Iuri Matias
parent 5b912262c5
commit 17c5238847
3 changed files with 16 additions and 2 deletions

View File

@ -189,12 +189,13 @@ class Cmd {
test() { test() {
program program
.command('test [file]') .command('test [file]')
.option('-d , --gasDetails', __('When set, will print the gas cost for each contract'))
.option('--locale [locale]', __('language to use (default: en)')) .option('--locale [locale]', __('language to use (default: en)'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'warn') .option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'warn')
.description(__('run tests')) .description(__('run tests'))
.action(function (file, options) { .action(function (file, options) {
i18n.setOrDetectLocale(options.locale); i18n.setOrDetectLocale(options.locale);
embark.runTests({file, loglevel: options.loglevel}); embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails});
}); });
} }

View File

@ -7,12 +7,22 @@ class EmbarkSpec extends Base {
const self = this; const self = this;
self.embarkEvents = options.reporterOptions.events; self.embarkEvents = options.reporterOptions.events;
self.gasDetails = options.reporterOptions.gasDetails;
let indents = 0; let indents = 0;
let n = 0; let n = 0;
self.stats.gasCost = 0; self.stats.gasCost = 0;
function onContractReceipt(receipt) { function onContractReceipt(receipt) {
self.stats.gasCost += receipt.gasUsed; self.stats.gasCost += receipt.gasUsed;
if (self.gasDetails) {
const fmt = color('bright pass', ' ') +
color('green', ' %s') +
' deployed for ' +
color('green', '%s') +
color('light', ' gas');
console.log(fmt, receipt.className, receipt.gasUsed);
}
} }
self.embarkEvents.on("deploy:contract:receipt", onContractReceipt); self.embarkEvents.on("deploy:contract:receipt", onContractReceipt);
@ -27,6 +37,9 @@ class EmbarkSpec extends Base {
runner.on('suite', function (suite) { runner.on('suite', function (suite) {
++indents; ++indents;
if (self.gasDetails) {
console.log();
}
console.log(color('suite', '%s%s'), indent(), suite.title); console.log(color('suite', '%s%s'), indent(), suite.title);
}); });

View File

@ -101,7 +101,7 @@ module.exports = {
function executeForAllFiles(files, next) { function executeForAllFiles(files, next) {
async.eachLimit(files, 1, (file, eachCb) => { async.eachLimit(files, 1, (file, eachCb) => {
const mocha = new Mocha(); const mocha = new Mocha();
mocha.reporter(EmbarkSpec, {events: global.embark.engine.events}); mocha.reporter(EmbarkSpec, {events: global.embark.engine.events, gasDetails: options.gasDetails});
mocha.addFile(file); mocha.addFile(file);
mocha.suite.timeout(0); mocha.suite.timeout(0);