Skip to content

Commit

Permalink
feat(cheatcodes): expectRevert with specific reverter address (#8770)
Browse files Browse the repository at this point in the history
* feat(cheatcodes): expectRevert with specific reverter address

* Support assert revert of the given contract at any subcall of the next call
  • Loading branch information
grandizzy authored Sep 17, 2024
1 parent 604ce1d commit 2b39094
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 22 deletions.
82 changes: 81 additions & 1 deletion crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,26 @@ interface Vm {
#[cheatcode(group = Testing, safety = Unsafe)]
function expectRevert(bytes calldata revertData) external;

/// Expects an error with any revert data on next call to reverter address.
#[cheatcode(group = Testing, safety = Unsafe)]
function expectRevert(address reverter) external;

/// Expects an error from reverter address on next call, with any revert data.
#[cheatcode(group = Testing, safety = Unsafe)]
function expectRevert(bytes4 revertData, address reverter) external;

/// Expects an error from reverter address on next call, that exactly matches the revert data.
#[cheatcode(group = Testing, safety = Unsafe)]
function expectRevert(bytes calldata revertData, address reverter) external;

/// Expects an error on next call that starts with the revert data.
#[cheatcode(group = Testing, safety = Unsafe)]
function expectPartialRevert(bytes4 revertData) external;

/// Expects an error on next call to reverter address, that starts with the revert data.
#[cheatcode(group = Testing, safety = Unsafe)]
function expectPartialRevert(bytes4 revertData, address reverter) external;

/// Expects an error on next cheatcode call with any revert data.
#[cheatcode(group = Testing, safety = Unsafe, status = Internal)]
function _expectCheatcodeRevert() external;
Expand Down
19 changes: 13 additions & 6 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,7 @@ impl Cheatcodes {
return match expect::handle_expect_revert(
false,
true,
expected_revert.reason.as_deref(),
expected_revert.partial_match,
&expected_revert,
outcome.result.result,
outcome.result.output.clone(),
&self.config.available_artifacts,
Expand Down Expand Up @@ -1234,8 +1233,17 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
}
}

// Handle expected reverts
if let Some(expected_revert) = &self.expected_revert {
// Handle expected reverts.
if let Some(expected_revert) = &mut self.expected_revert {
// Record current reverter address before processing the expect revert if call reverted,
// expect revert is set with expected reverter address and no actual reverter set yet.
if outcome.result.is_revert() &&
expected_revert.reverter.is_some() &&
expected_revert.reverted_by.is_none()
{
expected_revert.reverted_by = Some(call.target_address);
}

if ecx.journaled_state.depth() <= expected_revert.depth {
let needs_processing = match expected_revert.kind {
ExpectedRevertKind::Default => !cheatcode_call,
Expand All @@ -1251,8 +1259,7 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
return match expect::handle_expect_revert(
cheatcode_call,
false,
expected_revert.reason.as_deref(),
expected_revert.partial_match,
&expected_revert,
outcome.result.result,
outcome.result.output.clone(),
&self.config.available_artifacts,
Expand Down
Loading

0 comments on commit 2b39094

Please sign in to comment.