foundry icon indicating copy to clipboard operation
foundry copied to clipboard

forge coverage should allow excluding contracts

Open varunsrin opened this issue 3 years ago • 2 comments

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

varunsrin avatar Aug 28 '22 16:08 varunsrin

+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 🙏

antoncoding avatar Sep 07 '22 09:09 antoncoding

+1, absolutely necessary for custom directory layouts and for CICD scripts to rely on forge coverage

flaskr avatar Sep 16 '22 02:09 flaskr

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.

alexroan avatar Oct 13 '22 12:10 alexroan

Is there any updates on an option to exclude certain contracts from coverage?

haythemsellami avatar Oct 31 '22 20:10 haythemsellami

A workaround for this is to retroactively remove unwanted files from the lcov report using the lcov package.

Example CI:

  1. forge coverage --report lcov (this generates a ./lcov.info file)
  2. sudo apt-get install lcov
  3. lcov --remove ./lcov.info -o ./lcov.info.pruned 'exclusion/path/1' 'exclusion/path/2'
  4. Run coverage check on ./lcov.info.pruned

alexroan avatar Nov 01 '22 14:11 alexroan

This would definitely be handy

Rubilmax avatar Nov 07 '22 16:11 Rubilmax

@alexroan how do you re-run the coverage check in step 4 on ./lcov.info.pruned ?

0xTimepunk avatar Nov 24 '22 12:11 0xTimepunk

@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

EdNoepel avatar Dec 11 '22 00:12 EdNoepel

@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 }}

distributedstatemachine avatar Dec 23 '22 15:12 distributedstatemachine

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"

PaulRBerg avatar Feb 07 '23 14:02 PaulRBerg

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.

antoncoding avatar Feb 21 '23 02:02 antoncoding

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

yorhodes avatar Apr 12 '23 16:04 yorhodes

Permalink: https://github.com/hyperlane-xyz/hyperlane-monorepo/blob/e91c3100c29d2913d175df4b3d1790d6a057d36e/solidity/coverage.sh

(Links with main in them may get outdated)

PaulRBerg avatar Apr 12 '23 18:04 PaulRBerg