From: Ian Jackson Date: Mon, 31 May 2021 13:37:41 +0000 (+0100) Subject: childio: tests: Provide warning capture machinery X-Git-Tag: otter-0.7.0~197 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e68092b98e5e4a322d90f0f43520384ff39871b9;p=otter.git childio: tests: Provide warning capture machinery Signed-off-by: Ian Jackson --- diff --git a/src/childio.rs b/src/childio.rs index 22059296..3e663c0e 100644 --- a/src/childio.rs +++ b/src/childio.rs @@ -114,10 +114,50 @@ impl Write for ChildIo where W: Write { #[cfg(test)] #[cfg(not(miri))] -mod test { +use crate::capture_warns_warn as warn; + +#[cfg(test)] +#[cfg(not(miri))] +pub mod test { use crate::prelude::*; use super::*; +pub mod capture_warns { + use crate::prelude::*; + use std::cell::RefCell; + + thread_local! { + pub static WARNINGS: RefCell>> = RefCell::new(None) + } + + #[macro_export] + macro_rules! capture_warns_warn { + { $fmt:literal $($rhs:tt)* } => { + $crate::childio::test::capture_warns::WARNINGS.with(|w| { + let mut w = w.borrow_mut(); + if let Some(ref mut msgs) = *w { + let s = format!($fmt $($rhs)*); + msgs.push(s); + } else { + crate::prelude::warn!($fmt $($rhs)*); + } + }); + } + } + + pub fn run(f: &dyn Fn()) -> Vec { + WARNINGS.with(|w| { + let mut w =w.borrow_mut(); + *w = Some(vec![]); + }); + f(); + WARNINGS.with(|w| { + let mut w =w.borrow_mut(); + mem::take(&mut *w).unwrap() + }) + } +} + #[test] fn t_cat() { let setup = ||{