|
1 |
| -#SolCover |
| 1 | +# SolCover |
2 | 2 |
|
3 | 3 | 
|
4 | 4 | [](https://codecov.io/gh/JoinColony/solcover)
|
5 | 5 |
|
6 |
| - |
7 |
| -###Code coverage for Solidity testing |
| 6 | +### Code coverage for Solidity testing |
8 | 7 | 
|
9 | 8 |
|
10 |
| -For more details about what this is, how it work and potential limitations, see |
| 9 | +For more details about what this is, how it works and potential limitations, see |
11 | 10 | [the accompanying article](https://blog.colony.io/code-coverage-for-solidity-eecfa88668c2).
|
12 | 11 |
|
13 |
| -###Installation and preparation |
| 12 | +### Install |
| 13 | +``` |
| 14 | +$ npm install --save-dev https://github.com/JoinColony/solcover.git#truffle3 |
| 15 | +``` |
14 | 16 |
|
15 |
| -From your truffle directory, clone this repo: |
| 17 | +### Run |
16 | 18 | ```
|
17 |
| -git clone http://github.com/JoinColony/solcover.git |
18 |
| -cd solcover |
19 |
| -npm install |
| 19 | +$ ./node_modules/solcover/exec.js |
20 | 20 | ```
|
21 | 21 |
|
22 |
| -Until [Truffle allows the `--network` flag for the `test` command](https://github.com/ConsenSys/truffle/issues/239), in `truffle.js` you have to set a large gas amount for deployment. While this is set, uninstrumented tests likely won't run correctly, so this should only be set when running the coverage tests. An appropriately modified `truffle.js` might look like |
| 22 | +Tests run signficantly slower while coverage is being generated. A 1 to 2 minute delay |
| 23 | +between the end of Truffle compilation and the beginning of test execution is possible if your |
| 24 | +test suite is large. Large solidity files can also take a while to instrument. |
23 | 25 |
|
24 |
| -``` |
| 26 | +### Configuration |
| 27 | + |
| 28 | +By default, Solcover generates a stub `truffle.js` that accomodates its special gas needs and |
| 29 | +connects to a modified version of testrpc on port 8555. If your tests will run on the development network |
| 30 | +using a standard `truffle.js` and a testrpc instance with no special options, you shouldn't have to |
| 31 | +do any configuration. If your tests depend on logic added to `truffle.js` - for example: |
| 32 | +[zeppelin-solidity](https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/truffle.js) |
| 33 | +uses the file to expose a babel polyfill that its suite requires - you can override the |
| 34 | +default behavior by declaring a coverage network in `truffle.js`. Solcover will use your 'truffle.js' |
| 35 | +instead of a dynamically generated one. |
| 36 | + |
| 37 | +**Example coverage network config** |
| 38 | +```javascript |
25 | 39 | module.exports = {
|
26 |
| - rpc: { |
27 |
| - host: 'localhost', |
28 |
| - gasPrice: 20e9, |
29 |
| - gas: 0xfffffff, |
| 40 | + networks: { |
| 41 | + development: { |
| 42 | + host: "localhost", |
| 43 | + port: 8545, |
| 44 | + network_id: "*" // Match any network id |
| 45 | + }, |
| 46 | + coverage: { |
| 47 | + host: "localhost", |
| 48 | + network_id: "*", |
| 49 | + port: 8555, // <-- Use port 8555 |
| 50 | + gas: 0xfffffffffff, // <-- Use this high gas value |
| 51 | + gasPrice: 0x01 // <-- Use this low gas price |
| 52 | + } |
30 | 53 | }
|
31 |
| - }; |
| 54 | +}; |
32 | 55 | ```
|
33 |
| -In the future, hopefully just adding the 'coverage' network to `truffle.js` will be enough. This will look like |
34 | 56 |
|
| 57 | +You can also create a `.solcover.js` config file in the root directory of your project and specify |
| 58 | +some additional options: |
| 59 | ++ **port**: <Number> The port you want Solcover to run testrpc on / have truffle connect to. |
| 60 | ++ **testrpcOptions**: <String> A string of options to be appended to a command line invocation |
| 61 | +of testrpc. |
| 62 | + + Example: `--account="0x89a...b1f',10000" --port 8777`". |
| 63 | + + Note: you should specify the port in your `testrpcOptions` string AND as a `port` option. |
| 64 | ++ **testCommand**: <String> By default Solcover runs `truffle test` or `truffle test --network coverage`. |
| 65 | +This option lets you run tests some other way: ex: `mocha --timeout 5000`. You |
| 66 | +will probably also need to make sure the web3 provider for your tests explicitly connects to the port Solcover's |
| 67 | +testrpc is set to run on, e.g: |
| 68 | + + `var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8555"))` |
| 69 | + |
| 70 | +**Example .solcover.js config file** |
35 | 71 | ```
|
36 | 72 | module.exports = {
|
37 |
| - rpc: { |
38 |
| - host: 'localhost', |
39 |
| - gasPrice: 20e9, |
40 |
| - }, |
41 |
| - networks:{ |
42 |
| - "coverage":{ |
43 |
| - gas: 0xfffffff, |
44 |
| - } |
45 |
| - } |
46 |
| -} |
| 73 | + port: 6545, |
| 74 | + testrpcOptions: '-p 6545 -u 0x54fd80d6ae7584d8e9a19fe1df43f04e5282cc43', |
| 75 | + testCommand: 'mocha --timeout 5000' |
| 76 | +}; |
47 | 77 | ```
|
48 |
| -and will not interfere with normal `truffle test` - or other commands - being run during development. |
49 |
| - |
50 |
| -Note that if you have hardcoded gas costs into your tests, some of them may fail when using SolCover. This is because the instrumentation process increases the gas costs for using the contracts, due to the extra events. If this is the case, then the coverage may be incomplete. To avoid this, using `estimateGas` to estimate your gas costs should be more resilient in most cases. |
51 |
| - |
52 |
| -###Execution |
53 | 78 |
|
54 |
| -Firstly, make sure that your contracts in your truffle directory are saved elsewhere too - this script moves them and modifies them to do the instrumentation and allow `truffle` to run the tests with the instrumented contracts. It returns them after the tests are complete, but if something goes wrong, then `originalContracts` in the truffle directory should contain the unmodified contracts. |
| 79 | +### Known Issues |
55 | 80 |
|
56 |
| -SolCover runs its own (modified) `testrpc` to get the coverage data, so make sure that you've not left a previous instance running on port 8545, otherwise the coverage reported will be.... sparse... |
| 81 | +**Hardcoded gas costs**: If you have hardcoded gas costs into your tests some of them may fail when using SolCover. |
| 82 | +This is because the instrumentation process increases the gas costs for using the contracts, due to |
| 83 | +the extra events. If this is the case, then the coverage may be incomplete. To avoid this, using |
| 84 | +`estimateGas` to estimate your gas costs should be more resilient in most cases. |
57 | 85 |
|
58 |
| -From inside the SolCover directory, run |
| 86 | +**Events testing**: Because Solcover injects events into your contracts to log which lines your tests reach, |
| 87 | +any tests that ask how many events are fired or where the event sits in the logs array |
| 88 | +will probably error while coverage is being generated. |
59 | 89 |
|
60 |
| -```node ./runCoveredTests.js``` |
| 90 | +**Using `require` in `migrations.js` files**: Truffle overloads Node's `require` function but |
| 91 | +implements a simplified search algorithm for node_modules packages |
| 92 | +([see issue #383 at Truffle](https://github.com/trufflesuite/truffle/issues/383)). |
| 93 | +Because Solcover copies an instrumented version of your project into a temporary folder, `require` |
| 94 | +statements handled by Truffle internally won't resolve correctly. |
61 | 95 |
|
62 |
| -Upon completion of the tests, open the `./coverage/lcov-report/index.html` file to browse the HTML coverage report. |
| 96 | +**Coveralls / CodeCov**: These CI services take the Istanbul reports generated by Solcover and display |
| 97 | +line coverage. Istanbul's own html report publishes significantly more detail and can show whether |
| 98 | +your tests actually reach all the conditional branches in your code. It can be found inside the |
| 99 | +`coverage` folder at `index.html` after you run the tool. |
63 | 100 |
|
64 |
| -###A few, uh, provisos, a, a couple of quid pro quos... |
65 |
| -It is very likely that there are valid Solidity statements that this tool won't instrument correctly, as it's only been developed against a small number of contracts. If (and when) you find such cases, please raise an issue. |
| 101 | +### Examples |
66 | 102 |
|
| 103 | ++ **Metacoin**: The default truffle project |
| 104 | + + [HTML reports](https://sc-forks.github.io/metacoin/) |
| 105 | + + [Metacoin with Solcover installed](https://github.com/sc-forks/metacoin) (simple, without configuration) |
| 106 | ++ **zeppelin-solidity** at commit 453a19825013a586751b87c67bebd551a252fb50 |
| 107 | + + [HTML reports]( https://sc-forks.github.io/zeppelin-solidity/) |
| 108 | + + [Zeppelin with Solcover installed](https://github.com/sc-forks/zeppelin-solidity) (declares own coverage network in truffle.js) |
| 109 | ++ **numeraire** at commit 5ac3fa432c6b4192468c95a66e52ca086c804c95 |
| 110 | + + [HTML reports](https://sc-forks.github.io/contract/) |
| 111 | + + [Numeraire with Solcover installed](https://github.com/sc-forks/contract) (uses .solcover.js) |
67 | 112 |
|
68 |
| -###TODO |
| 113 | +### TODO |
69 | 114 |
|
70 |
| -- [ ] **TESTS** |
71 | 115 | - [ ] Turn into a true command line tool, rather than just a hacked-together script
|
72 | 116 | - [ ] Release on NPM
|
73 |
| -- [ ] Do not modify the `../contract/` directory at all during operation (might need changes to truffle) |
74 | 117 | - [ ] Support for arbitrary testing commands
|
75 | 118 | - [ ] [You tell me](http://github.com/JoinColony/solcover/issues)
|
0 commit comments