From 57033fa764d541fc1fdc5a019ab32e3aa4746589 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Mon, 9 Sep 2019 18:29:52 -0700 Subject: [PATCH] Add --config and --help command options (#390) --- dist/truffle.plugin.js | 16 +++++++++----- lib/ui.js | 7 +++++- test/units/app.js | 38 +++++++++++++++++++++++++++++++- test/util/integration.truffle.js | 7 ++++-- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/dist/truffle.plugin.js b/dist/truffle.plugin.js index eb6d57a..d7b762b 100644 --- a/dist/truffle.plugin.js +++ b/dist/truffle.plugin.js @@ -41,20 +41,26 @@ async function plugin(truffleConfig){ let truffle; let testsErrored = false; let coverageConfig; - let coverageConfigPath; + let solcoverjs; // Load truffle lib, .solcover.js & launch app try { - truffle = loadTruffleLibrary(); - - coverageConfigPath = path.join(truffleConfig.working_directory, '.solcover.js'); - coverageConfig = req.silent(coverageConfigPath) || {}; + (truffleConfig.solcoverjs) + ? solcoverjs = path.join(truffleConfig.working_directory, truffleConfig.solcoverjs) + : solcoverjs = path.join(truffleConfig.working_directory, '.solcover.js'); + coverageConfig = req.silent(solcoverjs) || {}; coverageConfig.cwd = truffleConfig.working_directory; coverageConfig.originalContractsDir = truffleConfig.contracts_directory; app = new App(coverageConfig); + if (truffleConfig.help){ + return app.ui.report('truffle-help') + } + + truffle = loadTruffleLibrary(); + } catch (err) { throw err; } diff --git a/lib/ui.js b/lib/ui.js index ddff715..c396808 100644 --- a/lib/ui.js +++ b/lib/ui.js @@ -23,6 +23,11 @@ class UI { const kinds = { + 'truffle-help': `Usage: truffle run coverage [options]\n\n` + + `Options:\n` + + ` --file: path (or glob) to run subset of JS test files\n` + + ` --solcoverjs: relative path to .solcover.js (ex: ./../.solcover.js)\n`, + 'truffle-version': `${ct} ${c.bold('truffle')}: v${args[0]}`, 'ganache-version': `${ct} ${c.bold('ganache-core')}: ${args[0]}`, @@ -33,7 +38,7 @@ class UI { `\n${c.bold('=====================')}\n`, 'instr-item': `${ct} ${args[0]}`, - 'instr-skipped': `${ds} ${c.grey(args[0])}` + 'instr-skipped': `${ds} ${c.grey(args[0])}`, } this.log(emoji.emojify(kinds[kind])); diff --git a/test/units/app.js b/test/units/app.js index 9970999..3ebaaa3 100644 --- a/test/units/app.js +++ b/test/units/app.js @@ -54,11 +54,13 @@ function getOutput(truffleConfig){ describe('app', function() { let truffleConfig; let solcoverConfig; + let collector; beforeEach(() => { mock.clean(); - truffleConfig = mock.getDefaultTruffleConfig(); + solcoverConfig = {}; + truffleConfig = mock.getDefaultTruffleConfig(); if (process.env.SILENT) solcoverConfig.silent = true; @@ -145,6 +147,40 @@ describe('app', function() { await plugin(truffleConfig); }); + it('truffle run coverage --config ../.solcover.js', async function() { + assertCleanInitialState(); + + solcoverConfig = { + silent: process.env.SILENT ? true : false, + istanbulReporter: ['json-summary', 'text'] + }; + fs.writeFileSync('.solcover.js', `module.exports=${JSON.stringify(solcoverConfig)}`); + + // This relative path has to be ./ prefixed + // (because it's path.joined to truffle's working_directory) + truffleConfig.solcoverjs = './../.solcover.js'; + + mock.install('Simple', 'simple.js'); + await plugin(truffleConfig); + + // The relative solcoverjs uses the json-summary reporter which + // this assertion requires + const expected = [{ + file: pathToContract(truffleConfig, 'Simple.sol'), + pct: 100 + }]; + + assertLineCoverage(expected); + shell.rm('.solcover.js'); + }); + + it('truffle run coverage --help', async function(){ + assertCleanInitialState(); + truffleConfig.help = "true"; + mock.install('Simple', 'simple.js', solcoverConfig); + await plugin(truffleConfig); + }) + it('truffle run coverage --file test/', async function() { assertCleanInitialState(); diff --git a/test/util/integration.truffle.js b/test/util/integration.truffle.js index b136050..c119890 100644 --- a/test/util/integration.truffle.js +++ b/test/util/integration.truffle.js @@ -97,7 +97,9 @@ function install( noMigrations ) { - const configjs = getSolcoverJS(config); + let configjs; + if(config) configjs = getSolcoverJS(config); + const trufflejs = getTruffleConfigJS(_truffleConfig); const migration = deploySingle(contract); @@ -116,7 +118,7 @@ function install( // Configs fs.writeFileSync(`${temp}/${truffleConfigName}`, trufflejs); - fs.writeFileSync(configPath, configjs); + if(config) fs.writeFileSync(configPath, configjs); decacheConfigs(); @@ -169,6 +171,7 @@ function clean() { shell.rm('-Rf', temp); shell.rm('-Rf', 'coverage'); shell.rm('coverage.json'); + shell.rm('.solcover.js'); shell.config.silent = false; };