Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command option to specify the source files to run the coverage on (#806) #838

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ npx hardhat coverage [command-options]
| Option <img width=200/> | Example <img width=750/>| Description <img width=1000/> |
|--------------|------------------------------------|--------------------------------|
| testfiles | `--testfiles "test/registry/*.ts"` | Test file(s) to run. (Globs must be enclosed by quotes and use [globby matching patterns][38])|
| 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/`) |
| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) |
| network | `--network development` | Use network settings defined in the Hardhat config |
| 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] |
Expand Down
1 change: 1 addition & 0 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ task("coverage", "Generates a code coverage report for tests")
.addOptionalParam("testfiles", ui.flags.file, "", types.string)
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, "", types.string)
.addOptionalParam('temp', ui.flags.temp, "", types.string)
.addOptionalParam('sources', ui.flags.sources, "", types.string)
.addFlag('matrix', ui.flags.testMatrix)
.addFlag('abi', ui.flags.abi)
.setAction(async function(args, env){
Expand Down
8 changes: 7 additions & 1 deletion plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ function getTestFilePaths(files){
* @return {HardhatConfig} updated config
*/
function normalizeConfig(config, args={}){
let sources;

(args.sources)
? sources = path.join(config.paths.sources, args.sources)
: sources = config.paths.sources;

config.workingDir = config.paths.root;
config.contractsDir = config.paths.sources;
config.contractsDir = sources;
config.testDir = config.paths.tests;
config.artifactsDir = config.paths.artifacts;
config.logger = config.logger ? config.logger : {log: null};
Expand Down
14 changes: 12 additions & 2 deletions plugins/resources/plugin.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,18 @@ function checkContext(config, tempContractsDir, tempArtifactsDir){
// =============================

function assembleFiles(config, skipFiles=[]){
const targetsPath = path.join(config.contractsDir, '**', '*.{sol,vy}');
const targets = shell.ls(targetsPath).map(path.normalize);
let targets;
let targetsPath;

// The targets (contractsDir) could actually be a single named file (OR a folder)
const extName = path.extname(config.contractsDir);

if (extName.length !== 0) {
targets = [ path.normalize(config.contractsDir) ];
} else {
targetsPath = path.join(config.contractsDir, '**', '*.{sol,vy}');
targets = shell.ls(targetsPath).map(path.normalize);
}

skipFiles = assembleSkipped(config, targets, skipFiles);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.7.0;


contract OtherContractA {
uint x;
constructor() public {
}

function sendFn() public {
x = 5;
}

function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}
15 changes: 15 additions & 0 deletions test/integration/projects/test-files/test/other_contract_a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const OtherContractA = artifacts.require("OtherContractA");

contract("otherContractA", function(accounts) {
let instance;

before(async () => instance = await OtherContractA.new())

it('sends', async function(){
await instance.sendFn();
});

it('calls', async function(){
await instance.callFn();
})
});
64 changes: 64 additions & 0 deletions test/units/hardhat/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,69 @@ describe('Hardhat Plugin: command line options', function() {
const output = require(outputPath);
assert.deepEqual(output, expected);
})

it('--sources folder', async function() {

const taskArgs = {
testfiles: path.join(hardhatConfig.paths.root, 'test/other_contract_a.js'),
sources: 'otherContracts'
};
mock.installFullProject('test-files');
mock.hardhatSetupEnv(this);

await this.env.run("coverage", taskArgs);

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
pct: 100
}
];

verify.lineCoverage(expected);
});

it('--sources folder/', async function() {

const taskArgs = {
testfiles: path.join(hardhatConfig.paths.root, 'test/other_contract_a.js'),
sources: 'otherContracts/'
};
mock.installFullProject('test-files');
mock.hardhatSetupEnv(this);

await this.env.run("coverage", taskArgs);

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
pct: 100
}
];

verify.lineCoverage(expected);
});

it('--sources folder/filename.sol', async function() {

const taskArgs = {
testfiles: path.join(hardhatConfig.paths.root, 'test/other_contract_a.js'),
sources: 'otherContracts/OtherContractA.sol'
};
mock.installFullProject('test-files');
mock.hardhatSetupEnv(this);

await this.env.run("coverage", taskArgs);

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'otherContracts/OtherContractA.sol'),
pct: 100
}
];

verify.lineCoverage(expected);
});

});