Skip to content

Commit 3f767d7

Browse files
cgeweckeclauBv23
andauthored
Add command option to specify the source files to run the coverage on (#806) (#838)
Co-authored-by: Claudia Barcelo <[email protected]>
1 parent 8da877e commit 3f767d7

File tree

7 files changed

+117
-3
lines changed

7 files changed

+117
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ npx hardhat coverage [command-options]
4444
| Option <img width=200/> | Example <img width=750/>| Description <img width=1000/> |
4545
|--------------|------------------------------------|--------------------------------|
4646
| testfiles | `--testfiles "test/registry/*.ts"` | Test file(s) to run. (Globs must be enclosed by quotes and use [globby matching patterns][38])|
47+
| sources | `--sources myFolder` or `--sources myFile.sol` | Path to *single* folder or file to target for coverage. Path is relative to Hardhat's `paths.sources` (usually `contracts/`) |
4748
| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) |
4849
| network | `--network development` | Use network settings defined in the Hardhat config |
4950
| temp[<sup>*</sup>][14] | `--temp build` | :warning: **Caution** :warning: Path to a *disposable* folder to store compilation artifacts in. Useful when your test setup scripts include hard-coded paths to a build directory. [More...][14] |

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ function getTestFilePaths(files){
3030
* @return {HardhatConfig} updated config
3131
*/
3232
function normalizeConfig(config, args={}){
33+
let sources;
34+
35+
(args.sources)
36+
? sources = path.join(config.paths.sources, args.sources)
37+
: sources = config.paths.sources;
38+
3339
config.workingDir = config.paths.root;
34-
config.contractsDir = config.paths.sources;
40+
config.contractsDir = sources;
3541
config.testDir = config.paths.tests;
3642
config.artifactsDir = config.paths.artifacts;
3743
config.logger = config.logger ? config.logger : {log: null};

plugins/resources/plugin.utils.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,18 @@ function checkContext(config, tempContractsDir, tempArtifactsDir){
131131
// =============================
132132

133133
function assembleFiles(config, skipFiles=[]){
134-
const targetsPath = path.join(config.contractsDir, '**', '*.{sol,vy}');
135-
const targets = shell.ls(targetsPath).map(path.normalize);
134+
let targets;
135+
let targetsPath;
136+
137+
// The targets (contractsDir) could actually be a single named file (OR a folder)
138+
const extName = path.extname(config.contractsDir);
139+
140+
if (extName.length !== 0) {
141+
targets = [ path.normalize(config.contractsDir) ];
142+
} else {
143+
targetsPath = path.join(config.contractsDir, '**', '*.{sol,vy}');
144+
targets = shell.ls(targetsPath).map(path.normalize);
145+
}
136146

137147
skipFiles = assembleSkipped(config, targets, skipFiles);
138148

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,69 @@ describe('Hardhat Plugin: command line options', function() {
230230
const output = require(outputPath);
231231
assert.deepEqual(output, expected);
232232
})
233+
234+
it('--sources folder', async function() {
235+
236+
const taskArgs = {
237+
testfiles: path.join(hardhatConfig.paths.root, 'test/other_contract_a.js'),
238+
sources: 'otherContracts'
239+
};
240+
mock.installFullProject('test-files');
241+
mock.hardhatSetupEnv(this);
242+
243+
await this.env.run("coverage", taskArgs);
244+
245+
const expected = [
246+
{
247+
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
248+
pct: 100
249+
}
250+
];
251+
252+
verify.lineCoverage(expected);
253+
});
254+
255+
it('--sources folder/', async function() {
256+
257+
const taskArgs = {
258+
testfiles: path.join(hardhatConfig.paths.root, 'test/other_contract_a.js'),
259+
sources: 'otherContracts/'
260+
};
261+
mock.installFullProject('test-files');
262+
mock.hardhatSetupEnv(this);
263+
264+
await this.env.run("coverage", taskArgs);
265+
266+
const expected = [
267+
{
268+
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
269+
pct: 100
270+
}
271+
];
272+
273+
verify.lineCoverage(expected);
274+
});
275+
276+
it('--sources folder/filename.sol', async function() {
277+
278+
const taskArgs = {
279+
testfiles: path.join(hardhatConfig.paths.root, 'test/other_contract_a.js'),
280+
sources: 'otherContracts/OtherContractA.sol'
281+
};
282+
mock.installFullProject('test-files');
283+
mock.hardhatSetupEnv(this);
284+
285+
await this.env.run("coverage", taskArgs);
286+
287+
const expected = [
288+
{
289+
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
290+
pct: 100
291+
}
292+
];
293+
294+
verify.lineCoverage(expected);
295+
});
296+
233297
});
234298

0 commit comments

Comments
 (0)