foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(`cheatcodes`): add ability to exclude certain custom errors and revert reason strings from failing tests

Open PaulRBerg opened this issue 2 years ago • 2 comments

Component

Forge

Describe the feature you would like

The use case is fork + fuzz tests - certain tokens like USDC have blacklists that prevent the transfer of assets between certain accounts (see lines 837 and 838 here). When Foundry fuzzes a blacklisted account, the Forge test will fail because of the following error:

Blacklistable: account is blacklisted

However, this is a false negative. The user's test should not have failed.

It would be great if there were two config options for filtering revert reason strings and custom errors.

PaulRBerg avatar Feb 04 '23 20:02 PaulRBerg

Related:

  • #4090

0xPhaze avatar Feb 05 '23 20:02 0xPhaze

For the time being, PSA: forge-std has a number of modifiers to help filter out these specific cases (I think added by OP): https://github.com/foundry-rs/forge-std/blob/75b3fcf052cc7886327e4c2eac3d1a1f36942b41/src/StdCheats.sol#L208-L278

The general request is still relevant so leaving the ticket open

zerosnacks avatar Jun 28 '24 14:06 zerosnacks

Interested in take a stab at this!

emo-eth avatar Oct 21 '24 17:10 emo-eth

Interested in take a stab at this!

assigned!

jenpaff avatar Oct 21 '24 18:10 jenpaff

@PaulRBerg have you run into the scenario where you might want to toss-out multiple kinds of reverts for the same external call (while surfacing others)? Considering adding that functionality.

emo-eth avatar Oct 23 '24 03:10 emo-eth

@emo-eth I have. I think this is what I was trying to get at with the OP — reverts due to USDC blacklisted addresses should be ignored.

PaulRBerg avatar Oct 23 '24 11:10 PaulRBerg

@PaulRBerg Sorry, to be more specific, I mean multiple kinds of errors with different data, to cover a set of anticipated & acceptable revert reasons that might arise from one specific call. Think I have that working either way, but curious if you've run into that scenario.

emo-eth avatar Oct 23 '24 15:10 emo-eth

I don't think I have.

PaulRBerg avatar Oct 23 '24 16:10 PaulRBerg

@PaulRBerg @mds1 as proposed in PR https://github.com/foundry-rs/foundry/pull/9179 here are the cheatcodes planned to be added, mind to review / share thoughts? thanks

    /// Discard this run's fuzz inputs and generate new ones if next call reverted with data starting with the given revert data. Call more than once to add multiple reasons.
    #[cheatcode(group = Testing, safety = Safe)]
    function assumeNoRevert(bytes4 revertData) external pure;

    /// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons.
    #[cheatcode(group = Testing, safety = Safe)]
    function assumeNoRevert(bytes calldata revertData) external pure;

    /// Discard this run's fuzz inputs and generate new ones if next call reverted with data starting with the given revert data. Call more than once to add multiple reasons.
    #[cheatcode(group = Testing, safety = Safe)]
    function assumeNoRevert(bytes4 revertData, address reverter) external pure;

    /// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons.
    #[cheatcode(group = Testing, safety = Safe)]
    function assumeNoRevert(bytes calldata revertData, address reverter) external pure;

grandizzy avatar Nov 06 '24 07:11 grandizzy

looks good for me, tagging for @andreivladbrg and @smol-ninja for additional feedback

PaulRBerg avatar Nov 06 '24 12:11 PaulRBerg

Looks good to me as well.

smol-ninja avatar Nov 09 '24 23:11 smol-ninja