Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ truffle run coverage [command-options]
+ Coverage launches its own in-process ganache server.
+ You can set [ganache options][1] using the `providerOptions` key in your `.solcover.js` [config][15].
+ Coverage [distorts gas consumption][13]. Tests that check exact gas consumption should be [skipped][24].
+ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25]
+ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25]

## Command Options
| Option <img width=200/> | Example <img width=750/>| Description <img width=1000/> |
|--------------|------------------------------------|--------------------------------|
| file | `--file="test/registry/*.js"` | Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
| file (Truffle) | `--file="test/registry/*.js"` | Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
| testFiles (Buidler) | `--testFiles test/file.js` | JS test file(s) to run.|
| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) |
| network | `--network development` | Use network settings defined in the Truffle config |
| network | `--network development` | Use network settings defined in the Truffle or Buidler config |
| temp[<sup>*</sup>][14] | `--temp build` | :warning: **Caution** :warning: Path to a *disposable* folder to store compilation artifacts in. Useful when your test setup scripts include hard-coded paths to a build directory. [More...][14] |
| version | | Version info |
| help | | Usage notes |

[<sup>*</sup> Advanced use][14]

Expand Down
22 changes: 15 additions & 7 deletions dist/buidler.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ const {
TASK_COMPILE,
} = require("@nomiclabs/buidler/builtin-tasks/task-names");

ensurePluginLoadedWithUsePlugin();

function plugin() {

// UI for the task flags...
const ui = new PluginUI();

task("coverage", "Generates a code coverage report for tests")

.addOptionalParam("file", ui.flags.file, null, types.string)
.addOptionalParam("testFiles", ui.flags.file, null, types.string)
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, null, types.string)
.addOptionalParam('temp', ui.flags.temp, null, types.string)

.setAction(async function(taskArguments, env){
.setAction(async function(args, env){
let error;
let ui;
let api;
Expand All @@ -37,15 +39,19 @@ function plugin() {
try {
death(buidlerUtils.finish.bind(null, config, api)); // Catch interrupt signals

config = buidlerUtils.normalizeConfig(env.config);
config = buidlerUtils.normalizeConfig(env.config, args);
ui = new PluginUI(config.logger.log);
api = new API(utils.loadSolcoverJS(config));

// ==============
// Server launch
// ==============

const network = buidlerUtils.setupNetwork(env, api);
const network = buidlerUtils.setupNetwork(
env,
api,
args,
ui
);

const address = await api.ganache(ganache);
const web3 = new Web3(address);
Expand Down Expand Up @@ -88,12 +94,14 @@ function plugin() {
// ==============
// Compilation
// ==============
config.temp = args.temp;

const {
tempArtifactsDir,
tempContractsDir
} = utils.getTempLocations(config);

utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
utils.save(targets, config.paths.sources, tempContractsDir);
utils.save(skipped, config.paths.sources, tempContractsDir);

Expand All @@ -109,7 +117,7 @@ function plugin() {
// ======
// Tests
// ======
const testFiles = buidlerUtils.getTestFilePaths(config);
const testFiles = args.testFiles ? [args.testFiles] : [];

try {
await env.run(TASK_TEST, {testFiles: testFiles})
Expand All @@ -128,7 +136,7 @@ function plugin() {
error = e;
}

await utils.finish(config, api);
await buidlerUtils.finish(config, api);

if (error !== undefined ) throw error;
if (process.exitCode > 0) throw new Error(ui.generate('tests-fail', [process.exitCode]));
Expand Down
6 changes: 4 additions & 2 deletions dist/plugin-assets/buidler.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class PluginUI extends UI {
`\n${c.bold('============')}\n` +
`${ct} ${c.bold('port')}: ${args[1]}\n` +
`${ct} ${c.bold('network')}: ${args[0]}\n`,

'port-clash': `${w} ${c.red("The 'port' values in your Buidler url ")}` +
`${c.red("and .solcover.js are different. Using Buidler's: ")} ${c.bold(args[0])}.\n`,

}

this._write(kinds[kind]);
Expand All @@ -70,8 +74,6 @@ class PluginUI extends UI {

'tests-fail': `${x} ${c.bold(args[0])} ${c.red('test(s) failed under coverage.')}`,

'no-network': `${c.red('Network: ')} ${args[0]} ` +
`${c.red(' is not defined in your truffle-config networks. ')}`,

}

Expand Down
46 changes: 21 additions & 25 deletions dist/plugin-assets/buidler.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,44 @@ const { createProvider } = require("@nomiclabs/buidler/internal/core/providers/c
// Buidler Specific Plugin Utils
// =============================

/**
* Returns a list of test files to pass to TASK_TEST.
* @param {BuidlerConfig} config
* @return {String[]} list of files to pass to mocha
*/
function getTestFilePaths(config){
let target;

// Handle --file <path|glob> cli option (subset of tests)
(typeof config.file === 'string')
? target = globby.sync([config.file])
: target = [];

// Return list of test files
const testregex = /.*\.(js|ts|es|es6|jsx)$/;
return target.filter(f => f.match(testregex) != null);
}

/**
* Normalizes buidler paths / logging for use by the plugin utilities and
* attaches them to the config
* @param {BuidlerConfig} config
* @return {BuidlerConfig} updated config
*/
function normalizeConfig(config){
function normalizeConfig(config, args){
config.workingDir = config.paths.root;
config.contractsDir = config.paths.sources;
config.testDir = config.paths.tests;
config.artifactsDir = config.paths.artifacts;
config.logger = config.logger ? config.logger : {log: null};
config.solcoverjs = args.solcoverjs

return config;
}

function setupNetwork(env, api){
const networkConfig = {
url: `http://${api.host}:${api.port}`,
gas: api.gasLimit,
gasPrice: api.gasPrice
function setupNetwork(env, api, taskArgs, ui){
let networkConfig = {};

if (taskArgs.network){
networkConfig = env.config.networks[taskArgs.network];

const configPort = networkConfig.url.split(':')[2];

// Warn: port conflicts
if (api.port !== api.defaultPort && api.port !== configPort){
ui.report('port-clash', [ configPort ])
}

// Prefer network port
api.port = parseInt(configPort);
}

networkConfig.url = `http://${api.host}:${api.port}`;
networkConfig.gas = api.gasLimit;
networkConfig.gasPrice = api.gasPrice;

const provider = createProvider(api.defaultNetworkName, networkConfig);

env.config.networks[api.defaultNetworkName] = networkConfig;
Expand Down Expand Up @@ -102,7 +99,6 @@ module.exports = {
normalizeConfig: normalizeConfig,
finish: finish,
tempCacheDir: tempCacheDir,
getTestFilePaths: getTestFilePaths,
setupNetwork: setupNetwork
}

26 changes: 15 additions & 11 deletions dist/plugin-assets/plugin.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ function loadSource(_path){
return fs.readFileSync(_path).toString();
}

/**
* Sets up temporary folders for instrumented contracts and their compilation artifacts
* @param {PlatformConfig} config
* @param {String} tempContractsDir
* @param {String} tempArtifactsDir
*/
function setupTempFolders(config, tempContractsDir, tempArtifactsDir){
checkContext(config, tempContractsDir, tempArtifactsDir);

shell.mkdir(tempContractsDir);
shell.mkdir(tempArtifactsDir);
}

/**
* Save a set of instrumented files to a temporary directory.
* @param {Object[]} targets array of targets generated by `assembleTargets`
Expand Down Expand Up @@ -122,16 +135,6 @@ function assembleFiles(config, skipFiles=[]){
let skipFolders;
let skipped = [];

const {
tempContractsDir,
tempArtifactsDir
} = getTempLocations(config);

checkContext(config, tempContractsDir, tempArtifactsDir);

shell.mkdir(tempContractsDir);
shell.mkdir(tempArtifactsDir);

targets = shell.ls(`${config.contractsDir}/**/*.sol`);

skipFiles = assembleSkipped(config, targets, skipFiles);
Expand Down Expand Up @@ -268,5 +271,6 @@ module.exports = {
reportSkipped: reportSkipped,
save: save,
checkContext: checkContext,
toRelativePath: toRelativePath
toRelativePath: toRelativePath,
setupTempFolders: setupTempFolders
}
1 change: 1 addition & 0 deletions dist/truffle.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async function plugin(config){
tempContractsDir
} = utils.getTempLocations(config);

utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
utils.save(targets, config.contracts_directory, tempContractsDir);
utils.save(skipped, config.contracts_directory, tempContractsDir);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"nyc": "SILENT=true nyc --exclude '**/sc_temp/**' --exclude '**/test/**'",
"test": "npm run nyc -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:ci": "SILENT=true nyc --reporter=lcov --exclude '**/sc_temp/**' --exclude '**/test/**/' -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:ci": "SILENT=true node --max-old-space-size=3072 ./node_modules/.bin/nyc --reporter=lcov --exclude '**/sc_temp/**' --exclude '**/test/**/' -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:debug": "mocha test/units/* --timeout 100000 --no-warnings --exit"
},
"homepage": "https://github.com/sc-forks/solidity-coverage",
Expand Down
2 changes: 1 addition & 1 deletion test/units/buidler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const path = require('path')
const pify = require('pify')
const shell = require('shelljs');
const ganache = require('ganache-core-sc');
const ganache = require('ganache-cli')

const verify = require('../../util/verifiers')
const mock = require('../../util/integration');
Expand Down
Loading