Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Support single file targets, add README docs
  • Loading branch information
cgewecke committed Feb 2, 2024
commit 75f88bf65c70c255ad2db1a8c5fe9fcc0bbc6359
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
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 = args.sources? args.sources : 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
54 changes: 45 additions & 9 deletions test/units/hardhat/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,53 @@ describe('Hardhat Plugin: command line options', function() {
assert.deepEqual(output, expected);
})

it('--sources contract/<fileName>', async function() {
it('--sources folder', async function() {

const taskArgs = {
testfiles: path.join(
hardhatConfig.paths.root,
'test/other_contract_a.js'
),
sources: path.join(
hardhatConfig.paths.root,
'contracts/otherContracts'
)
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);
Expand Down