Skip to content

Commit 2552eba

Browse files
committed
Add 'onCompileComplete' option (#413)
* Also small UI fixes & document network cli flag
1 parent dc618ba commit 2552eba

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ $ npm install --save-dev solidity-coverage@beta
2222
### Usage notes:
2323
+ Coverage runs tests a little more slowly.
2424
+ Coverage distorts gas consumption. Tests that check exact gas consumption should be skipped.
25-
+ Coverage launches its own in-process ganache server.
26-
+ You can set [ganache options](https://github.com/trufflesuite/ganache-core#options) using the `providerOptions` key in your `.solcover.js` config.
25+
+ Coverage launches its own in-process ganache server.
26+
+ You can set [ganache options](https://github.com/trufflesuite/ganache-core#options) using the `providerOptions` key in your `.solcover.js` config.
2727

2828
### Truffle V5
2929

@@ -45,6 +45,7 @@ truffle run coverage [options]
4545
|--------------|------------------------------------|--------------------------------|
4646
| file | `--file="test/registry/*.js"` | Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
4747
| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) |
48+
| network | `--network development` | Use network settings defined in the Truffle config |
4849
| version | | Version info |
4950
| help | | Usage notes |
5051

@@ -71,6 +72,7 @@ module.exports = {
7172
| mocha | *Object* | `{ }` | [Mocha options](https://mochajs.org/api/mocha) to merge into existing mocha config. `grep` and `invert` are useful for skipping certain tests under coverage using tags in the test descriptions.|
7273
| onServerReady | *Function* | | Hook run *after* server is launched, *before* the tests execute. Useful if you need to use the Oraclize bridge or have setup scripts which rely on the server's availability |
7374
| onTestsComplete | *Function* | | Hook run *after* the tests complete, *before* Istanbul reports are generated. |
75+
| onCompileComplete | *Function* | | Hook run *after* compilation completes, *before* tests are run. Useful if you have secondary compilation steps or need to modify built artifacts. |
7476
| onIstanbulComplete | *Function* | | Hook run *after* the Istanbul reports are generated, *before* the ganache server is shut down. Useful if you need to clean resources up. |
7577

7678

dist/plugin-assets/truffle.ui.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class PluginUI extends UI {
5454

5555

5656
'versions': `${ct} ${c.bold('truffle')}: v${args[0]}\n` +
57-
`${ct} ${c.bold('ganache-core')}: ${args[0]}\n` +
58-
`${ct} ${c.bold('solidity-coverage')}: v${args[0]}`,
57+
`${ct} ${c.bold('ganache-core')}: ${args[1]}\n` +
58+
`${ct} ${c.bold('solidity-coverage')}: v${args[2]}`,
5959

6060
'network': `\n${c.bold('Network Info')}` +
6161
`\n${c.bold('============')}\n` +
@@ -78,6 +78,7 @@ class PluginUI extends UI {
7878
*/
7979
generate(kind, args=[]){
8080
const c = this.chalk;
81+
const x = ":x:";
8182

8283
const kinds = {
8384

@@ -91,6 +92,8 @@ class PluginUI extends UI {
9192
`${c.red('This can happen if it has a syntax error or ')}` +
9293
`${c.red('the path you specified for it is wrong.')}`,
9394

95+
'tests-fail': `${x} ${c.bold(args[0])} ${c.red('test(s) failed under coverage.')}`,
96+
9497
'no-network': `${c.red('Network: ')} ${args[0]} ` +
9598
`${c.red(' is not defined in your truffle-config networks. ')}`,
9699

dist/truffle.plugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,17 @@ async function plugin(config){
9393

9494
// Compile Instrumented Contracts
9595
await truffle.contracts.compile(config);
96+
await api.onCompileComplete(config);
9697

9798
// Run tests
9899
try {
99100
failures = await truffle.test.run(config)
100101
} catch (e) {
101102
error = e.stack;
102103
}
103-
104104
await api.onTestsComplete(config);
105+
106+
// Run Istanbul
105107
await api.report();
106108
await api.onIstanbulComplete(config);
107109

@@ -113,7 +115,7 @@ async function plugin(config){
113115
await utils.finish(config, api);
114116

115117
if (error !== undefined) throw error;
116-
if (failures > 0) throw new Error(`${failures} test(s) failed under coverage.`)
118+
if (failures > 0) throw new Error(ui.generate('tests-fail', [failures]));
117119
}
118120

119121
module.exports = plugin;

lib/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class API {
3535
this.defaultHook = () => {};
3636
this.onServerReady = config.onServerReady || this.defaultHook;
3737
this.onTestsComplete = config.onTestsComplete || this.defaultHook;
38+
this.onCompileComplete = config.onCompileComplete || this.defaultHook;
3839
this.onIstanbulComplete = config.onIstanbulComplete || this.defaultHook;
3940

4041
this.server = null;

test/integration/projects/test-files/.solcover.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
istanbulReporter: ['json-summary', 'text'],
77
onServerReady: fn.bind(null, 'running onServerReady'),
88
onTestsComplete: fn.bind(null, 'running onTestsComplete'),
9+
onCompileComplete: fn.bind(null, 'running onCompileComplete'),
910
onIstanbulComplete: fn.bind(null, 'running onIstanbulComplete')
1011
}

test/units/truffle/standard.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ describe('Truffle Plugin: standard use cases', function() {
285285
assert(
286286
mock.loggerOutput.val.includes('running onServerReady') &&
287287
mock.loggerOutput.val.includes('running onTestsComplete') &&
288+
mock.loggerOutput.val.includes('running onCompileComplete') &&
288289
mock.loggerOutput.val.includes('running onIstanbulComplete'),
289290

290291
`Should run "on" hooks : ${mock.loggerOutput.val}`

0 commit comments

Comments
 (0)