Merge pull request #6 from sc-forks/istruffle-option

Add "isTruffle" option
pull/7/head
c-g-e-w-e-k-e- 8 years ago committed by GitHub
commit f4247c4841
  1. 8
      README.md
  2. 63
      exec.js

@ -68,13 +68,17 @@ This option lets you run tests some other way: ex: `mocha --timeout 5000`. You
will probably also need to make sure the web3 provider for your tests explicitly connects to the port solidity-coverage's will probably also need to make sure the web3 provider for your tests explicitly connects to the port solidity-coverage's
testrpc is set to run on, e.g: testrpc is set to run on, e.g:
+ `var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8555"))` + `var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8555"))`
+ **norpc**: {Boolean} By default, false. When true, solidity-coverage will not launch its own testrpc instance. This + **norpc**: {Boolean} When true, solidity-coverage will not launch its own testrpc instance. This
can be useful if you are running tests using a different vm like the can be useful if you are running tests using a different vm like the
[`sc-forks` version of `pyethereum`](https://github.com/sc-forks/pyethereum) [`sc-forks` version of `pyethereum`](https://github.com/sc-forks/pyethereum). (Default: false).
+ **isTruffle**: {Boolean}: Set to false if your project does not have a migrations folder or a
`truffle.js` config file.
+ **dir**: {String} : By default, solidity-coverage looks for a `contracts` folder in your root + **dir**: {String} : By default, solidity-coverage looks for a `contracts` folder in your root
directory. `dir` allows you to define a relative path from the root directory to the contracts directory. `dir` allows you to define a relative path from the root directory to the contracts
folder. A `dir` of `./secretDirectory` would tell solidity-coverage to look for `./secretDirectory/contracts` folder. A `dir` of `./secretDirectory` would tell solidity-coverage to look for `./secretDirectory/contracts`
**Example .solcover.js config file** **Example .solcover.js config file**
```javascript ```javascript
module.exports = { module.exports = {

@ -53,9 +53,10 @@ function cleanUp(err) {
// --------------------------------------- Script -------------------------------------------------- // --------------------------------------- Script --------------------------------------------------
const config = reqCwd.silent('./.solcover.js') || {}; const config = reqCwd.silent('./.solcover.js') || {};
const workingDir = config.dir || '.'; // Relative path to contracts folder const workingDir = config.dir || '.'; // Relative path to contracts folder
const port = config.port || 8555; // Port testrpc listens on const port = config.port || 8555; // Port testrpc listens on
const accounts = config.accounts || 35; // Number of accounts to testrpc launches with const accounts = config.accounts || 35; // Number of accounts to testrpc launches with
const isTruffle = config.isTruffle || true; // Is target a truffle project?
// Set testrpc options // Set testrpc options
const defaultRpcOptions = `--gasLimit ${gasLimitString} --accounts ${accounts} --port ${port}`; const defaultRpcOptions = `--gasLimit ${gasLimitString} --accounts ${accounts} --port ${port}`;
@ -71,45 +72,49 @@ if (config.silent) {
// (Changes here should be also be added to the before() block of test/run.js). // (Changes here should be also be added to the before() block of test/run.js).
if (!config.norpc) { if (!config.norpc) {
const command = './node_modules/.bin/testrpc-sc '; const command = './node_modules/.bin/testrpc-sc ';
testrpcProcess = childprocess.exec(command + testrpcOptions, null, (err) => { testrpcProcess = childprocess.exec(command + testrpcOptions, null, err => {
if (err) cleanUp('testRpc errored after launching as a childprocess.'); if (err) cleanUp('testRpc errored after launching as a childprocess.');
}); });
log(`Launching testrpc on port ${port}`); log(`Launching testrpc on port ${port}`);
} }
// Generate a copy of the target truffle project configured for solcover and save to the coverage // Generate a copy of the target project configured for solcover and save to the coverage
// environment folder. // environment folder.
log('Generating coverage environment'); log('Generating coverage environment');
try { try {
// Common environment: /contracts/ & /test/
shell.mkdir(`${coverageDir}`); shell.mkdir(`${coverageDir}`);
shell.cp('-R', `${workingDir}/contracts`, `${coverageDir}`); shell.cp('-R', `${workingDir}/contracts`, `${coverageDir}`);
shell.cp('-R', `${workingDir}/migrations`, `${coverageDir}`);
shell.cp('-R', `${workingDir}/test`, `${coverageDir}`); shell.cp('-R', `${workingDir}/test`, `${coverageDir}`);
const truffleConfig = reqCwd(`${workingDir}/truffle.js`); // Truffle environment: + /migrations/, truffle.js
if (isTruffle) {
// Coverage network opts specified: copy truffle.js whole to coverage environment shell.cp('-R', `${workingDir}/migrations`, `${coverageDir}`);
if (truffleConfig.networks.coverage) { const truffleConfig = reqCwd(`${workingDir}/truffle.js`);
shell.cp(`${workingDir}/truffle.js`, `${coverageDir}/truffle.js`);
// Coverage network opts specified: copy truffle.js whole to coverage environment
// Coverage network opts NOT specified: default to the development network w/ modified if (truffleConfig.networks.coverage) {
// port, gasLimit, gasPrice. Export the config object only. shell.cp(`${workingDir}/truffle.js`, `${coverageDir}/truffle.js`);
} else {
const trufflejs = ` // Coverage network opts NOT specified: default to the development network w/ modified
module.exports = { // port, gasLimit, gasPrice. Export the config object only.
networks: { } else {
development: { const trufflejs = `
host: "localhost", module.exports = {
network_id: "*", networks: {
port: ${port}, development: {
gas: ${gasLimitHex}, host: "localhost",
gasPrice: ${gasPriceHex} network_id: "*",
port: ${port},
gas: ${gasLimitHex},
gasPrice: ${gasPriceHex}
}
} }
} };`;
};`;
coverageOption = ''; coverageOption = '';
fs.writeFileSync(`${coverageDir}/truffle.js`, trufflejs); fs.writeFileSync(`${coverageDir}/truffle.js`, trufflejs);
}
} }
} catch (err) { } catch (err) {
const msg = ('There was a problem generating the coverage environment: '); const msg = ('There was a problem generating the coverage environment: ');
@ -144,7 +149,7 @@ try {
cleanUp(msg + err); cleanUp(msg + err);
} }
// Run solcover's fork of truffle over instrumented contracts in the // Run truffle over instrumented contracts in the
// coverage environment folder. Shell cd command needs to be invoked // coverage environment folder. Shell cd command needs to be invoked
// as its own statement for command line options to work, apparently. // as its own statement for command line options to work, apparently.
try { try {

Loading…
Cancel
Save