Skip to content

Commit

Permalink
Always drop static methods' Expectations during Drop
Browse files Browse the repository at this point in the history
Even if a panic is in-progress.  Originally we didn't drop them during
panic, for fear of double-panicing.  But there is already protection
from double-panic in Common::drop.  Not clearing expectations during a
panic can cause surprising failures in one test case after a different
test case panics.

Fixes #442
  • Loading branch information
asomers committed Mar 26, 2023
1 parent f1396bd commit 779e316
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Static methods' expectations will now be cleared during a panic.
([#443](https://github.com/asomers/mockall/pull/443))

- Methods with a `where Self: ...` clause will now be mocked like concrete
methods, not generic ones. Among other effects, this prevents "unused method
expect" warnings from the latest nightly compiler.
Expand Down
33 changes: 33 additions & 0 deletions mockall/tests/clear_expectations_on_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// vim: tw=80
//! Static methods' expectations should be dropped during panic.
//!
//! https://github.com/asomers/mockall/issues/442
#![deny(warnings)]

use std::panic;

use mockall::*;

#[automock]
pub trait Foo {
fn foo() -> i32;
}

#[test]
fn drop_expectations_on_panic() {
panic::catch_unwind(|| {
let ctx = MockFoo::foo_context();
ctx.expect()
.times(1)
.return_const(42);
panic!("Panicking!");
}).unwrap_err();

// The previously set expectation should've been cleared during the panic,
// so we must set a new one.
let ctx = MockFoo::foo_context();
ctx.expect()
.times(1)
.return_const(42);
assert_eq!(42, MockFoo::foo());
}
4 changes: 1 addition & 3 deletions mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,9 +1524,7 @@ impl<'a> ToTokens for Context<'a> {
}
impl #ty_ig Drop for Context #ty_tg #ty_wc {
fn drop(&mut self) {
if !std::thread::panicking() {
Self::do_checkpoint()
}
Self::do_checkpoint()
}
}
).to_tokens(tokens);
Expand Down

0 comments on commit 779e316

Please sign in to comment.