From 43a96402972a84d1cdeb8f25a1e689ebb3102af1 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 13 Sep 2018 14:30:15 -0400 Subject: [PATCH] add option to generate the coverage --- cmd/cmd.js | 4 +++- lib/core/fs.js | 15 +++++++++++++++ lib/tests/run_tests.js | 20 +++++++++++++++++++- package.json | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 1e1cb9a97..63faa63be 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -266,13 +266,15 @@ class Cmd { .command('test [file]') .option('-n , --node [node]', __('Node to connect to (default: vm)')) .option('-d , --gasDetails', __('When set, will print the gas cost for each contract deploy')) + .option('-c , --coverage', __('When set, will generate the coverage after the tests')) .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') .description(__('run tests')) .action(function(file, options) { checkDeps(); i18n.setOrDetectLocale(options.locale); - embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails, node: options.node}); + embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails, + node: options.node, coverage: options.coverage}); }); } diff --git a/lib/core/fs.js b/lib/core/fs.js index 5e340e966..d88ee4352 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -11,6 +11,18 @@ function mkdirp() { return fs.mkdirp.apply(fs.mkdirp, arguments); } +function readdir() { + return fs.readdir.apply(fs.readdir, arguments); +} + +function stat() { + return fs.stat.apply(fs.stat, arguments); +} + +function remove() { + return fs.remove.apply(fs.remove, arguments); +} + function copy() { return fs.copy.apply(fs.copy, arguments); } @@ -106,6 +118,9 @@ function tmpDir() { module.exports = { mkdirpSync, mkdirp, + readdir, + stat, + remove, copy, copySync, move, diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index febb06be1..c13a3b296 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -1,7 +1,8 @@ const async = require('async'); -const fs = require('fs-extra'); const Mocha = require('mocha'); const path = require('path'); +const {runCmd} = require('../utils/utils'); +const fs = require('../core/fs'); const assert = require('assert'); const Test = require('./test'); const EmbarkSpec = require('./reporter'); @@ -123,6 +124,23 @@ module.exports = { eachCb(); }); }, next); + }, + function runCoverage(next) { + if (!options.coverage) { + return next(); + } + + runCmd(`${fs.embarkPath('node_modules/.bin/istanbul')} report --root .embark --format html`, + {silent: false, exitOnError: false}, (err) => { + if (err) { + return next(err); + } + console.log(`Coverage report created. You can find it here: ${fs.dappPath('coverage/index.html')}\n`); + const opn = require('opn'); + opn(fs.dappPath('coverage/index.html')); + + setTimeout(next, 1000); // Slight delay to enable the browser to open. opn's promise calls too fast + }); } ], (err) => { if (err) { diff --git a/package.json b/package.json index a66c905f2..263f186de 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "i18n": "^0.8.3", "ipfs-api": "17.2.4", "is-valid-domain": "0.0.5", + "istanbul": "^0.4.5", "live-plugin-manager-git-fix": "^0.12.1", "lodash.clonedeep": "^4.5.0", "merge": "^1.2.0",