Skip to content

Commit abea00b

Browse files
authored
Throw error when mocha parallel is set to true (#833)
1 parent 658dc37 commit abea00b

File tree

8 files changed

+71
-0
lines changed

8 files changed

+71
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Common problems & questions:
122122
+ [Running out of time][6]
123123
+ [Running out of stack][1002] (Stack too deep)
124124
+ [Running out of memory][5]
125+
+ [Running in parallel (in CI)][1003]
125126

126127
## Example reports
127128
+ [metacoin][9] (Istanbul HTML)
@@ -216,4 +217,5 @@ $ yarn
216217
[39]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/advanced.md#generating-a-test-matrix
217218
[1001]: https://docs.soliditylang.org/en/v0.8.0/using-the-compiler.html#input-description
218219
[1002]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/faq.md#running-out-of-stack
220+
[1003]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/advanced.md#parallelization-in-ci
219221

docs/advanced.md

+16
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,24 @@ In combination these data sets can be passed to Joran Honig's [tarantula][29] to
125125
a fault localization algorithm to generate 'suspiciousness' ratings for each line of
126126
Solidity code in your project.
127127

128+
## Parallelization in CI
129+
130+
Coverage does not work with the Hardhat's mocha parallel mode. However, it *is* possible to parallelize coverage in CI environments that support complex workflows. The core idea is to
131+
132+
+ partition the set of test files passed to the coverage task
133+
+ split coverage into several concurrent jobs, passing test file targets as arguments using the `--testfiles` command line flag
134+
+ cache the coverage results in shared storage as each job completes
135+
+ combine results in a final step (using the [instanbul-combine-updated][30] tool)
136+
137+
There's a nice example of this being done in CircleCI [at Synthetix, here][31].
138+
139+
:bulb: **Pro Tip**: Codecov CI will automatically combine coverage reports sent to them as a batch - if you're using that service you don't need to do this yourself.
140+
141+
128142
[22]: https://github.com/JoranHonig/vertigo#vertigo
129143
[23]: http://spideruci.org/papers/jones05.pdf
130144
[25]: https://github.com/sc-forks/solidity-coverage/blob/master/docs/matrix.md
131145
[27]: https://mochajs.org/api/reporters_json.js.html
132146
[29]: https://github.com/JoranHonig/tarantula
147+
[30]: https://www.npmjs.com/package/istanbul-combine-updated
148+
[31]: https://github.com/Synthetixio/synthetix/blob/bd54f4e9cfd1529d8ea2e5a7b4d0be4c988e1a03/.circleci/src/jobs/job-unit-tests-coverage.yml

plugins/resources/plugin.ui.js

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class PluginUI extends UI {
5656
`${c.red('the path you specified for it is wrong.')}`,
5757

5858
'tests-fail': `${x} ${c.bold(args[0])} ${c.red('test(s) failed under coverage.')}`,
59+
60+
'mocha-parallel-fail': `${c.red('Coverage cannot be run in mocha parallel mode. ')}` +
61+
`${c.red('Set \`mocha: { parallel: false }\` in .solcover.js ')}` +
62+
`${c.red('to disable the option for the coverage task. ')}` +
63+
`${c.red('See the solidity-coverage README FAQ for info on parallelizing coverage in CI.')}`,
5964
}
6065

6166

plugins/resources/plugin.utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ function loadSolcoverJS(config={}){
232232
);
233233
}
234234

235+
// Per fvictorio recommendation in #691
236+
if (config.mocha.parallel) {
237+
const message = ui.generate('mocha-parallel-fail');
238+
throw new Error(message);
239+
}
240+
235241
return coverageConfig;
236242
}
237243

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
"silent": false,
3+
"istanbulReporter": [ "json-summary", "text"],
4+
"mocha": {
5+
parallel: true // manually tested that setting to false overrides HH config
6+
},
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pragma solidity ^0.7.0;
2+
3+
4+
contract ContractA {
5+
uint x;
6+
7+
function set() public {
8+
x = 5;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require(__dirname + "/../plugins/nomiclabs.plugin");
2+
3+
module.exports = {
4+
mocha: {
5+
parallel: true
6+
},
7+
logger: process.env.SILENT ? { log: () => {} } : console,
8+
};

test/units/hardhat/errors.js

+17
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,21 @@ describe('Hardhat Plugin: error cases', function() {
202202

203203
verify.coverageNotGenerated(hardhatConfig);
204204
});
205+
206+
it('mocha parallel option is true', async function(){
207+
mock.installFullProject('parallel');
208+
mock.hardhatSetupEnv(this);
209+
210+
try {
211+
await this.env.run("coverage");
212+
assert.fail()
213+
} catch(err){
214+
assert(
215+
err.message.includes('Coverage cannot be run in mocha parallel mode'),
216+
`Should notify when mocha parallel flag is set:: ${err.message}`
217+
);
218+
}
219+
220+
verify.coverageNotGenerated(hardhatConfig);
221+
})
205222
})

0 commit comments

Comments
 (0)