Skip to content

Commit 82cac37

Browse files
authored
Merge pull request #44 from sc-forks/copy-all-folders
Copy all directories when setting up coverageEnv
2 parents 3d95e31 + 5bf8f93 commit 82cac37

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

README.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
### Code coverage for Solidity testing
88
![coverage example](https://cdn-images-1.medium.com/max/800/1*uum8t-31bUaa6dTRVVhj6w.png)
99

10-
For more details about what this is, how it works and potential limitations, see
10+
For more details about what this is, how it works and potential limitations, see
1111
[the accompanying article](https://blog.colony.io/code-coverage-for-solidity-eecfa88668c2).
1212

1313
**solidity-coverage** is a stand-alone fork of [Solcover](https://github.com/JoinColony/solcover)
@@ -17,25 +17,25 @@ For more details about what this is, how it works and potential limitations, see
1717
$ npm install --save-dev solidity-coverage
1818
```
1919

20-
### Run
20+
### Run
2121
```
2222
$ ./node_modules/.bin/solidity-coverage
2323
```
2424

25-
Tests run signficantly slower while coverage is being generated. A 1 to 2 minute delay
25+
Tests run signficantly slower while coverage is being generated. A 1 to 2 minute delay
2626
between the end of Truffle compilation and the beginning of test execution is possible if your
2727
test suite is large. Large solidity files can also take a while to instrument.
2828

2929
### Configuration
3030

31-
By default, solidity-coverage generates a stub `truffle.js` that accomodates its special gas needs and
31+
By default, solidity-coverage generates a stub `truffle.js` that accomodates its special gas needs and
3232
connects to a modified version of testrpc on port 8555. If your tests will run on the development network
33-
using a standard `truffle.js` and a testrpc instance with no special options, you shouldn't have to
34-
do any configuration. If your tests depend on logic added to `truffle.js` - for example:
35-
[zeppelin-solidity](https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/truffle.js)
36-
uses the file to expose a babel polyfill that its suite requires - you can override the
33+
using a standard `truffle.js` and a testrpc instance with no special options, you shouldn't have to
34+
do any configuration. If your tests depend on logic added to `truffle.js` - for example:
35+
[zeppelin-solidity](https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/truffle.js)
36+
uses the file to expose a babel polyfill that its suite requires - you can override the
3737
default behavior by declaring a coverage network in `truffle.js`. solidity-coverage will use your 'truffle.js'
38-
instead of a dynamically generated one.
38+
instead of a dynamically generated one.
3939

4040
**Example coverage network config**
4141
```javascript
@@ -48,7 +48,7 @@ module.exports = {
4848
},
4949
coverage: {
5050
host: "localhost",
51-
network_id: "*",
51+
network_id: "*",
5252
port: 8555, // <-- Use port 8555
5353
gas: 0xfffffffffff, // <-- Use this high gas value
5454
gasPrice: 0x01 // <-- Use this low gas price
@@ -57,24 +57,25 @@ module.exports = {
5757
};
5858
```
5959

60-
You can also create a `.solcover.js` config file in the root directory of your project and specify
60+
You can also create a `.solcover.js` config file in the root directory of your project and specify
6161
some additional options:
6262

6363

6464
+ **port**: *{ Number }* Port to run testrpc on / have truffle connect to. (Default: 8555)
6565
+ **accounts**: *{ Number }* Number of accounts to launch testrpc with. (Default: 35)
66-
+ **testrpcOptions**: *{ String }* options to append to a command line invocation of testrpc.
67-
+ ex: `--secure --unlock "0x1234..." --unlock "0xabcd..."`.
66+
+ **testrpcOptions**: *{ String }* options to append to a command line invocation of testrpc.
67+
+ ex: `--secure --unlock "0x1234..." --unlock "0xabcd..."`.
6868
+ NB: if you specify the port in your rpc options string, also declare it as a `port` option.
69-
+ **testCommand**: *{ String }* By default solidity-coverage runs `truffle test`. This option lets
70-
you run an arbitrary test command instead, like: `mocha --timeout 5000`.
71-
+ remember to set the config's port option to whatever port your tests use (probably 8545).
72-
+ make sure you don't have another instance of testrpc running on that port (web3 will error if you do).
69+
+ **testCommand**: *{ String }* By default solidity-coverage runs `truffle test`. This option lets
70+
you run an arbitrary test command instead, like: `mocha --timeout 5000`.
71+
+ remember to set the config's port option to whatever port your tests use (probably 8545).
72+
+ make sure you don't have another instance of testrpc running on that port (web3 will error if you do).
7373
+ **norpc**: *{ Boolean }* When true, solidity-coverage will not launch its own testrpc instance. This
7474
can be useful if you are using a different vm like the [sc-forks version of pyethereum](https://github.com/sc-forks/pyethereum).
7575
+ **dir**: *{ String }* : Solidity-coverage usually looks for `contracts` and `test` folders in your root
76-
directory. `dir` allows you to define a relative path from the root directory to those assets.
76+
directory. `dir` allows you to define a relative path from the root directory to those assets.
7777
`dir: "./<dirname>"` would tell solidity-coverage to look for `./<dirname>/contracts/` and `./<dirname>/test/`
78+
+ **copyNodeModules**: *{ Boolean }* : When true, will copy `node_modules` into the coverage environment. False by default, and may significantly increase the time for coverage to complete if enabled. Only enable if required.
7879

7980
**Example .solcover.js config file**
8081
```javascript
@@ -89,28 +90,28 @@ module.exports = {
8990

9091
### Known Issues
9192

92-
**Hardcoded gas costs**: If you have hardcoded gas costs into your tests some of them may fail when using solidity-coverage.
93-
This is because the instrumentation process increases the gas costs for using the contracts, due to
94-
the extra events. If this is the case, then the coverage may be incomplete. To avoid this, using
93+
**Hardcoded gas costs**: If you have hardcoded gas costs into your tests some of them may fail when using solidity-coverage.
94+
This is because the instrumentation process increases the gas costs for using the contracts, due to
95+
the extra events. If this is the case, then the coverage may be incomplete. To avoid this, using
9596
`estimateGas` to estimate your gas costs should be more resilient in most cases.
9697

9798
**Using `require` in `migrations.js` files**: Truffle overloads Node's `require` function but
98-
implements a simplified search algorithm for node_modules packages
99+
implements a simplified search algorithm for node_modules packages
99100
([see Truffle issue #383](https://github.com/trufflesuite/truffle/issues/383)).
100-
Because solidity-coverage copies an instrumented version of your project into a temporary folder, `require`
101+
Because solidity-coverage copies an instrumented version of your project into a temporary folder, `require`
101102
statements handled by Truffle internally won't resolve correctly.
102103

103104
**Using HDWalletProvider in `truffle.js`**: [See Truffle issue #348](https://github.com/trufflesuite/truffle/issues/348).
104-
HDWalletProvider crashes solidity-coverage, so its constructor shouldn't be invoked while running this tool.
105-
A workaround can be found at the zeppelin-solidity project
106-
[here](https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/truffle.js#L8-L10), where a
107-
shell script is used to set an environment variable which `truffle.js` checks before instantiating the wallet.
105+
HDWalletProvider crashes solidity-coverage, so its constructor shouldn't be invoked while running this tool.
106+
A workaround can be found at the zeppelin-solidity project
107+
[here](https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/truffle.js#L8-L10), where a
108+
shell script is used to set an environment variable which `truffle.js` checks before instantiating the wallet.
108109

109110
### Examples
110111

111-
**WARNING**: This utility is in development and its accuracy is unknown. If you
112-
find discrepancies between the coverage report and your suite's behavior, please open an
113-
[issue](https://github.com/sc-forks/solidity-coverage/issues).
112+
**WARNING**: This utility is in development and its accuracy is unknown. If you
113+
find discrepancies between the coverage report and your suite's behavior, please open an
114+
[issue](https://github.com/sc-forks/solidity-coverage/issues).
114115

115116
+ **metacoin**: The default truffle project
116117
+ [HTML reports](https://sc-forks.github.io/metacoin/)
@@ -125,8 +126,8 @@ find discrepancies between the coverage report and your suite's behavior, please
125126
### Contribution Guidelines
126127

127128
Contributions are welcome! If you're opening a PR that adds features please consider writing some
128-
[unit tests](https://github.com/sc-forks/solidity-coverage/tree/master/test) for them. You could
129-
also lint your submission with `npm run lint`. Bugs can be reported in the
129+
[unit tests](https://github.com/sc-forks/solidity-coverage/tree/master/test) for them. You could
130+
also lint your submission with `npm run lint`. Bugs can be reported in the
130131
[issues](https://github.com/sc-forks/solidity-coverage/issues).
131132

132133
### Contributors

bin/exec.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const config = reqCwd.silent('./.solcover.js') || {};
6161
const workingDir = config.dir || '.'; // Relative path to contracts folder
6262
let port = config.port || 8555; // Port testrpc listens on
6363
const accounts = config.accounts || 35; // Number of accounts to testrpc launches with
64+
const copyNodeModules = config.copyNodeModules || false; // Whether we copy node_modules when making coverage environment
6465

6566
// Silence shell and script logging (for solcover's unit tests / CI)
6667
if (config.silent) {
@@ -78,10 +79,16 @@ try {
7879
// migrations/
7980
// truffle.js
8081

82+
let files = shell.ls(`${workingDir}`);
83+
const nmIndex = files.indexOf('node_modules');
84+
85+
if (!config.copyNodeModules && nmIndex > -1) {
86+
files.splice(nmIndex, 1); // Removes node_modules from array.
87+
}
88+
89+
files = files.map(file => `${workingDir}/` + file);
8190
shell.mkdir(`${coverageDir}`);
82-
shell.cp('-R', `${workingDir}/contracts`, `${coverageDir}`);
83-
shell.cp('-R', `${workingDir}/test`, `${coverageDir}`);
84-
shell.cp('-R', `${workingDir}/migrations`, `${coverageDir}`);
91+
shell.cp('-R', files, `${coverageDir}`);
8592

8693
const truffleConfig = reqCwd.silent(`${workingDir}/truffle.js`);
8794

@@ -97,7 +104,7 @@ try {
97104
module.exports = {
98105
networks: {
99106
development: {
100-
host: "localhost",
107+
host: "localhost",
101108
network_id: "*",
102109
port: ${port},
103110
gas: ${gasLimitHex},
@@ -148,7 +155,7 @@ if (!config.norpc) {
148155
const defaultRpcOptions = `--gasLimit ${gasLimitString} --accounts ${accounts} --port ${port}`;
149156
const testrpcOptions = config.testrpcOptions || defaultRpcOptions;
150157
const command = './node_modules/.bin/testrpc-sc ';
151-
158+
152159
testrpcProcess = childprocess.exec(command + testrpcOptions, null, err => {
153160
if (err) cleanUp('testRpc errored after launching as a childprocess.');
154161
});
@@ -178,9 +185,9 @@ try {
178185
const msg =
179186
`
180187
There was an error generating coverage. Possible reasons include:
181-
1. Another application is using port ${port}
188+
1. Another application is using port ${port}
182189
2. Truffle crashed because your tests errored
183-
190+
184191
`;
185192
cleanUp(msg + err);
186193
}

test/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ describe('cli', () => {
118118
`module.exports = {
119119
networks: {
120120
development: {
121-
host: "localhost",
121+
host: "localhost",
122122
port: 8545,
123123
network_id: "*"
124124
},
125125
coverage: {
126-
host: "localhost",
126+
host: "localhost",
127127
port: 8999,
128128
network_id: "*"
129129
}

0 commit comments

Comments
 (0)