Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a vm.mockCallRevert cheatcode #2740

Closed
fubhy opened this issue Aug 12, 2022 · 13 comments · Fixed by #4343
Closed

Add a vm.mockCallRevert cheatcode #2740

fubhy opened this issue Aug 12, 2022 · 13 comments · Fixed by #4343
Assignees
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test T-feature Type: feature

Comments

@fubhy
Copy link
Contributor

fubhy commented Aug 12, 2022

Component

Forge

Describe the feature you would like

I might be missing something but it seems like it's currently only possible to mock return values, but not reverts. For now, in my case I can simply create a Mock contract that does that for me but I'd imagine it could be handy to allow this via a cheatcode for convenience.

vm.mockCallRevert("revert reason / error")

Additional context

No response

@fubhy fubhy added the T-feature Type: feature label Aug 12, 2022
@gakonst gakonst added this to Foundry Aug 12, 2022
@gakonst gakonst moved this to Todo in Foundry Aug 12, 2022
@onbjerg
Copy link
Member

onbjerg commented Aug 12, 2022

Makes sense

@onbjerg onbjerg added Cmd-forge-test Command: forge test C-forge Command: forge A-cheatcodes Area: cheatcodes labels Aug 12, 2022
@mds1
Copy link
Collaborator

mds1 commented Aug 12, 2022

IMO if this is added we can also close foundry-rs/forge-std#46, as cheatcodes would then cover the majority of mocking use cases

@tynes
Copy link
Contributor

tynes commented Aug 12, 2022

Could you mock a revert by returning abi.encodeWithSignature("Error(string)", abi.encode("my error message")) or if its a custom revert just abi encoding the custom error?

@mds1
Copy link
Collaborator

mds1 commented Aug 12, 2022

I don't think so, because the call's success output would still be true instead of false

@tynes
Copy link
Contributor

tynes commented Aug 12, 2022

I suppose you could make vm.mockCall aware of the data passed in and check to see if the 4byte selector matches Error(string) or any of the custom errors and have it return false, but that adds complexity to the implementation of mock call. I'd be happy as long as whatever solution could work with custom errors. Also wondering if something like this could be used to test exceptional errors like evm out of gas in a subcall

@onbjerg
Copy link
Member

onbjerg commented Aug 12, 2022

Also wondering if something like this could be used to test exceptional errors like evm out of gas in a subcall

No, that would make the EVM unwind entirely. No way to stop that.

@tynes
Copy link
Contributor

tynes commented Aug 12, 2022

Also wondering if something like this could be used to test exceptional errors like evm out of gas in a subcall

No, that would make the EVM unwind entirely. No way to stop that.

I mean something like:

vm.ExpectRevert(RevertReasons.OUT_OF_GAS);
(bool success, ) = target.call{ gas: 1 }(...);

Where you pass a defined amount of gas to a subcall and it reverts there but there is still gas left in the higher level call to continue. This is probably a more fringe usecase so not super important

@onbjerg
Copy link
Member

onbjerg commented Aug 15, 2022

This would still cause the EVM to unwind 😄

@adhusson
Copy link
Contributor

This would be very useful.

@sendra
Copy link

sendra commented Jan 25, 2023

How is the state of this? It would be very useful

@reubenr0d
Copy link
Contributor

I have a working mvp here: #4343

Shall I take things forward?

@mds1
Copy link
Collaborator

mds1 commented Mar 8, 2023

That would be awesome, thanks @reubenr0d. I'll assign this to you. I think we need two functions, one that requires value, and one that doesn't:

function mockCallRevert(address, bytes calldata, bytes calldata) external;
function mockCallRevert(address, uint256, bytes calldata, bytes calldata) external;

@reubenr0d
Copy link
Contributor

Thanks @mds1, I've made an attempt here: #4343
I'm still learning the ropes with rust and would appreciate a little help with this. The tests that mockCallRevert on an already mockCalled call and vice versa fail right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test T-feature Type: feature
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

7 participants