sc-forks / solidity-coverage

Code coverage for Solidity smart-contracts
MIT License
972 stars 261 forks source link

Excluding lines of code from coverage testing #190

Open aleybovich opened 6 years ago

aleybovich commented 6 years ago

Is there a way to exclude some solidity code from coverage testing, like its done in JS with istanbul directives in comments?

cgewecke commented 6 years ago

@aleybovich Currently there is not. Agree this might be useful in some cases though.

aleybovich commented 6 years ago

@cgewecke Is this something you are planning to do or should I close this issue?

cgewecke commented 6 years ago

@aleybovich I think it should stay open - tagging it as a possible enhancement.

chapati23 commented 6 years ago

maybe just adopt the existing istanbul interface for ignoring code for coverage ?

cgewecke commented 6 years ago

@chapati23 Thanks for that link!

aleybovich commented 6 years ago

@cgewecke - are you planning to implement this in the future? It would be a very useful feature for many, I'm sure. For example, many contracts have trivial setters with no logic besides assigning a variable (like changing a dependent contract address) and those could be excluded from unit tests.

fw-aaron commented 3 years ago

Trivial setters may as well be tested. In my situatian, I have code like

function myFunc(MyEnum memory val) {
    if (val == MyEnum.one) {
        ...
    } else if (val == MyEnum.two) {
        ...
    } else {
        revert("Invalid enum value");
    }
}

As far as I can tell, there is no way to reach the revert in the else clause. I don't want to remove the revert though in case some day a new value is added, but is not explicitly supported in this function.

emretepedev commented 2 years ago

Any update?

sebastiantf commented 2 years ago

Love to have this.

A use-case is having internal function overrides required by Solidity during multiple inheritances, but not exposed with a public function.

6str commented 2 years ago

I have a helper contract that is used just for tests and I want it to be excluded from the coverage results.

This thread https://github.com/sc-forks/solidity-coverage/issues/17 states that ignoring files can be accomplished "using the / istanbul ignore . . . / strategy".

I've not been able to exclude the file or specific code using / istanbul ... / directives.

In the case of a test helper contract, it would be better if the helper contract was excluded from coverage results rather than included and showing as covered (IMO)

6str commented 2 years ago

maybe just adopt the existing istanbul interface for ignoring code for coverage ?

I couldn't get this to work with hardhat/solidity. It the above meaning that it should be achievable this way now, or a suggestion of a way it could be implemented?

apecollector commented 2 years ago

Would like this for stuff like supportsInterface and overrides of openzeppelin contracts that are required but difficult to test by themselves.

    // The following functions are overrides required by Solidity.
    function _beforeTokenTransfer(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal override(ERC1155, ERC1155Supply) {
        super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC1155, AccessControl)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
luiztools commented 1 year ago

I need this too!

VGabriel45 commented 3 months ago

Any updates on this ?

ricp commented 3 months ago

+1, would be very useful.