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

feat(forge) _expectRevertCheatcode #6841

Merged
merged 16 commits into from
Jan 22, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Jan 18, 2024

Motivation

Ref #6833

Considering that we need a way to write negative tests for cheatcodes internally but making existing expectRevert cheats interact with cheatcodes as well would break a lot of thing and not really a desired feature, this PR adds _expectRevertCheatcode cheats for internal usage.

Solution

  1. Extends ExpectedRevert context to allow 2 kinds of expectations - Default (expects revert from next non-cheatcode call) and Cheatcode (expects revert from next cheatcode call)
  2. This cheatcode shouldn't end up in forge-std, so I've marked it as Experimental.
  3. Updated cheatcode tests to use _expectRevertCheatcode instead of expectRevert

The logic with pending bool might be a little bit confusing, so maybe there is a way to improve it which I haven't figured out yet. I tried to document it clearly

@klkvr
Copy link
Member Author

klkvr commented Jan 18, 2024

Failing CI is triggered by one of the tests being false-positive earlier and failing on Windows now, not sure how to fix such things :/

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that this behaviour is somewhat required to move forward with converting forge-std checks to rust native cheatcodes, I think we should proceed with this.

having an additional cheatcode for internal use makes sense to me, but we probably don't want to expose this via the forge-std interface. Perhaps there's an easy way to exclude them when we generate the interface, maybe with an additional group (Internal?)

wdyt @DaniPopes

crates/cheatcodes/src/test/expect.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/test/expect.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/test/expect.rs Outdated Show resolved Hide resolved
Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far, and agreed we need to proceed with this to make these assertions native. About naming, maybe expectCheatcodeRevert is clearer?

crates/cheatcodes/src/inspector.rs Show resolved Hide resolved
@klkvr
Copy link
Member Author

klkvr commented Jan 18, 2024

@Evalir agreed that expectCheatcodeRevert is better, renamed it and pushed other fixes

pending guidance on whether we want Internal cheats group here

@@ -680,6 +680,18 @@ interface Vm {
#[cheatcode(group = Testing, safety = Unsafe)]
function expectRevert(bytes calldata revertData) external;

/// Expects an error on next cheatcode call with any revert data.
#[cheatcode(group = Testing, safety = Unsafe, status = Experimental)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think adding an Internal variant would be appropriate

@@ -23,10 +23,10 @@ contract FsTest is DSTest {

assertEq(vm.readFile(path), "hello readable world\nthis is the second line!");

vm.expectRevert(FOUNDRY_READ_ERR);
vm._expectCheatcodeRevert(FOUNDRY_READ_ERR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we still use expectRevert here, or would this no longer be possible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but the test will exit and pass right after the reverted call

That is the current behavior which resulted in some false positives

@klkvr klkvr requested review from mattsse and Evalir January 22, 2024 11:37
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes lgtm,
pendatic naming nit.

Could you please also summarize what the impact of this change is wrt regular expectRevert?

crates/cheatcodes/src/test/expect.rs Outdated Show resolved Hide resolved
@klkvr
Copy link
Member Author

klkvr commented Jan 22, 2024

Could you please also summarize what the impact of this change is wrt regular expectRevert?

This PR will not affect existing expectRevert behavior, it simply changes flow in call_end hook making it a bit more explicit about how cheatcode calls are being treated.

@mattsse
Copy link
Member

mattsse commented Jan 22, 2024

pending @Evalir @DaniPopes

Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this lgtm, so pending @DaniPopes for any nits—ideally we'd def figure out a way to not include this in the vm interface, but we can do so later

crates/cheatcodes/src/inspector.rs Show resolved Hide resolved
@klkvr
Copy link
Member Author

klkvr commented Jan 22, 2024

we'd def figure out a way to not include this in the vm interface

forge-std script to create Vm.sol interface already excludes Experimental cheats, so I think we can just exclude Internal as well

https://github.com/foundry-rs/forge-std/blob/ae570fec082bfe1c1f45b0acca4a2b4f84d345ce/scripts/vm.py#L32-L33

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mattsse mattsse merged commit 27a1e51 into foundry-rs:master Jan 22, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants