summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordvermd <315743+dvermd@users.noreply.github.com>2024-09-10 06:30:12 +0200
committerThomas Otto <th1000s@posteo.net>2024-09-15 10:42:35 +0200
commitc384eed93732f8049774bde3bfbc536850317a59 (patch)
treeddb9d1e2616ff8528990c7e56619065da154b279
parent7dd279284b2633f8c43525c35e643801160f2c7a (diff)
Do not double panic in FakeParentArgs::drop
Signed-off-by: dvermd <315743+dvermd@users.noreply.github.com>
-rw-r--r--src/utils/process.rs62
1 files changed, 43 insertions, 19 deletions
diff --git a/src/utils/process.rs b/src/utils/process.rs
index d392eb9..6fa5ea9 100644
--- a/src/utils/process.rs
+++ b/src/utils/process.rs
@@ -598,6 +598,7 @@ pub mod tests {
With(usize, Rc<Vec<T>>),
None,
Invalid,
+ ErrorAlreadyHandled,
}
// When calling `FakeParentArgs::get()`, it can return `Some(values)` which were set earlier
@@ -648,24 +649,39 @@ pub mod tests {
TlsState::With(n, args) => TlsState::With(*n + 1, Rc::clone(args)),
TlsState::None => TlsState::None,
TlsState::Invalid => TlsState::Invalid,
+ TlsState::ErrorAlreadyHandled => TlsState::ErrorAlreadyHandled,
});
match old_value {
TlsState::Once(args) | TlsState::Scope(args) => Some(args),
TlsState::With(n, args) if n < args.len() => Some(args[n].clone()),
TlsState::None => None,
- TlsState::Invalid | TlsState::With(_, _) => Self::error("get"),
+ TlsState::Invalid | TlsState::With(_, _) | TlsState::ErrorAlreadyHandled => {
+ Self::error("get");
+ None
+ }
}
})
}
pub fn are_set() -> bool {
- FAKE_ARGS.with(|a| *a.borrow() != TlsState::None)
+ FAKE_ARGS.with(|a| {
+ *a.borrow() != TlsState::None && *a.borrow() != TlsState::ErrorAlreadyHandled
+ })
}
- fn error(where_: &str) -> ! {
- panic!(
- "test logic error (in {}): wrong FakeParentArgs scope?",
- where_
- );
+ fn error(where_: &str) {
+ FAKE_ARGS.with(|a| {
+ let old_value = a.replace(TlsState::ErrorAlreadyHandled);
+
+ match old_value {
+ TlsState::ErrorAlreadyHandled => (),
+ _ => {
+ panic!(
+ "test logic error (in {}): wrong FakeParentArgs scope?",
+ where_
+ );
+ }
+ }
+ });
}
}
impl Drop for FakeParentArgs {
@@ -680,7 +696,7 @@ pub mod tests {
}
}
TlsState::Once(_) | TlsState::None => Self::error("drop"),
- TlsState::Scope(_) | TlsState::Invalid => {}
+ TlsState::Scope(_) | TlsState::Invalid | TlsState::ErrorAlreadyHandled => {}
}
});
}
@@ -838,7 +854,7 @@ pub mod tests {
}
#[test]
- #[should_panic]
+ #[should_panic(expected = "test logic error (in get): wrong FakeParentArgs scope?")]
fn test_process_testing_assert() {
let _args = FakeParentArgs::once("git blame do.not.panic");
assert_eq!(
@@ -850,13 +866,23 @@ pub mod tests {
}
#[test]
- #[should_panic]
- fn test_process_testing_assert_never_used() {
+ #[should_panic(expected = "test logic error (in drop): wrong FakeParentArgs scope?")]
+ fn test_process_testing_assert_once_never_used() {
let _args = FakeParentArgs::once("never used");
+ }
- // causes a panic while panicking, so can't test:
- // let _args = FakeParentArgs::for_scope(&"never used");
- // let _args = FakeParentArgs::once(&"never used");
+ #[test]
+ #[should_panic(expected = "test logic error (in once): wrong FakeParentArgs scope?")]
+ fn test_process_testing_assert_for_scope_never_used() {
+ let _args = FakeParentArgs::for_scope(&"never used");
+ let _args = FakeParentArgs::once(&"never used");
+ }
+
+ #[test]
+ #[should_panic(expected = "test logic error (in for_scope): wrong FakeParentArgs scope?")]
+ fn test_process_testing_assert_once_never_used2() {
+ let _args = FakeParentArgs::once(&"never used");
+ let _args = FakeParentArgs::for_scope(&"never used");
}
#[test]
@@ -879,13 +905,13 @@ pub mod tests {
}
#[test]
- #[should_panic]
+ #[should_panic(expected = "test logic error (in drop with): wrong FakeParentArgs scope?")]
fn test_process_testing_n_times_unused() {
let _args = FakeParentArgs::with(&["git blame once", "git blame twice"]);
}
#[test]
- #[should_panic]
+ #[should_panic(expected = "test logic error (in drop with): wrong FakeParentArgs scope?")]
fn test_process_testing_n_times_underused() {
let _args = FakeParentArgs::with(&["git blame once", "git blame twice"]);
assert_eq!(
@@ -895,15 +921,13 @@ pub mod tests {
}
#[test]
- #[should_panic]
- #[ignore]
+ #[should_panic(expected = "test logic error (in get): wrong FakeParentArgs scope?")]
fn test_process_testing_n_times_overused() {
let _args = FakeParentArgs::with(&["git blame once"]);
assert_eq!(
calling_process_cmdline(ProcInfo::new(), guess_git_blame_filename),
Some("once".into())
);
- // ignored: dropping causes a panic while panicking, so can't test
calling_process_cmdline(ProcInfo::new(), guess_git_blame_filename);
}