forge coverage should allow excluding contracts
Component
Forge
Describe the feature you would like
Running forge coverage automatically includes scripts and test utils that are contracts, which adds noise to the coverage report. It would be helpful if there was a way to exclude these from the reports
Additional context
No response
+1 on this feature, it would be helpful to be able to exclude certain folders too, given that the library coverage is not supported yet, it will make it easier for teams to build robust CICD pipelines if we can avoid the known issues 🙏
+1, absolutely necessary for custom directory layouts and for CICD scripts to rely on forge coverage
Looks like @onbjerg suggested that this might be done natively in #1576, but coverage always seems to include .t.sol files.
The build command supports a --skip test option, which excludes .t.sol files, but coverage kicks off a full rebuild. It would be great if that skip flag were able to be used with the coverage command.
Is there any updates on an option to exclude certain contracts from coverage?
A workaround for this is to retroactively remove unwanted files from the lcov report using the lcov package.
Example CI:
forge coverage --report lcov(this generates a ./lcov.info file)sudo apt-get install lcovlcov --remove ./lcov.info -o ./lcov.info.pruned 'exclusion/path/1' 'exclusion/path/2'- Run coverage check on ./lcov.info.pruned
This would definitely be handy
@alexroan how do you re-run the coverage check in step 4 on ./lcov.info.pruned ?
@alexroan how do you re-run the coverage check in step 4 on ./lcov.info.pruned ?
I do not see a way to do this from forge, but you can generate an HTML report using lcov:
genhtml ./lcov.info.pruned -o report --branch-coverage && firefox report/index.html
@alexroan how do you re-run the coverage check in step 4 on ./lcov.info.pruned ?
you pass it to the coverage reporter
- name: Report coverage
uses: romeovs/[email protected]
with:
lcov-file: lcov.info.pruned
github-token: ${{ secrets.GITHUB_TOKEN }}
As discussed in #1961, another workaround for this (if you'd prefer not to use @alexroan's solution) if you're using Codecov to track coverage is to ignore the test directory using a Codecov config file codecov.yml:
ignore:
- "test"
One other way to bypass (get excluded by the report) it is to add a function prefixed with test in the contract. That way forge thinks it's a test contract and don't have it on the list.
// add this to be excluded from coverage report
function test() public {}
Works for me for those mocks or tester contracts I don't really care.
some of you might find this useful, especially if you are working with hardhat hybrid and node_modules deps https://github.com/hyperlane-xyz/hyperlane-monorepo/blob/main/solidity/coverage.sh
Permalink: https://github.com/hyperlane-xyz/hyperlane-monorepo/blob/e91c3100c29d2913d175df4b3d1790d6a057d36e/solidity/coverage.sh
(Links with main in them may get outdated)