Skip to content

Commit f91532e

Browse files
authored
Add command option to specify the source files to run the coverage on (#806)
1 parent 8da877e commit f91532e

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

plugins/hardhat.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ task("coverage", "Generates a code coverage report for tests")
9797
.addOptionalParam("testfiles", ui.flags.file, "", types.string)
9898
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string)
9999
.addOptionalParam('temp', ui.flags.temp, "", types.string)
100+
.addOptionalParam('sources', ui.flags.sources, "", types.string)
100101
.addFlag('matrix', ui.flags.testMatrix)
101102
.addFlag('abi', ui.flags.abi)
102103
.setAction(async function(args, env){

plugins/resources/nomiclabs.utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function getTestFilePaths(files){
3131
*/
3232
function normalizeConfig(config, args={}){
3333
config.workingDir = config.paths.root;
34-
config.contractsDir = config.paths.sources;
34+
config.contractsDir = args.sources? args.sources : config.paths.sources;
3535
config.testDir = config.paths.tests;
3636
config.artifactsDir = config.paths.artifacts;
3737
config.logger = config.logger ? config.logger : {log: null};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pragma solidity ^0.7.0;
2+
3+
4+
contract OtherContractA {
5+
uint x;
6+
constructor() public {
7+
}
8+
9+
function sendFn() public {
10+
x = 5;
11+
}
12+
13+
function callFn() public pure returns (uint){
14+
uint y = 5;
15+
return y;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const OtherContractA = artifacts.require("OtherContractA");
2+
3+
contract("otherContractA", function(accounts) {
4+
let instance;
5+
6+
before(async () => instance = await OtherContractA.new())
7+
8+
it('sends', async function(){
9+
await instance.sendFn();
10+
});
11+
12+
it('calls', async function(){
13+
await instance.callFn();
14+
})
15+
});

test/units/hardhat/flags.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,33 @@ describe('Hardhat Plugin: command line options', function() {
230230
const output = require(outputPath);
231231
assert.deepEqual(output, expected);
232232
})
233+
234+
it('--sources contract/<fileName>', async function() {
235+
236+
const taskArgs = {
237+
testfiles: path.join(
238+
hardhatConfig.paths.root,
239+
'test/other_contract_a.js'
240+
),
241+
sources: path.join(
242+
hardhatConfig.paths.root,
243+
'contracts/otherContracts'
244+
)
245+
};
246+
mock.installFullProject('test-files');
247+
mock.hardhatSetupEnv(this);
248+
249+
await this.env.run("coverage", taskArgs);
250+
251+
const expected = [
252+
{
253+
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
254+
pct: 100
255+
}
256+
];
257+
258+
verify.lineCoverage(expected);
259+
});
260+
233261
});
234262

0 commit comments

Comments
 (0)