I have some code that exits the process depending on condition.
fn main() -> Result<(), Box<dyn Error>> {
do_something();
if gone_wrong {
sys::process::exit(1);
}
if already_fine {
sys::process::exit(0);
}
// otherwise
do_something_else_with_side_effects();
Ok(())
}
In debug/release, this works perfectly. But in cfg(test)
we must hide the ::exit
call, because it breaks the entire test run -- you'll see this:
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
(no, there are 34 tests in the crate, and the 33 other succeeded.)
So I put it in a block:
#[cfg(not(test))] {
sys::process::exit(0);
}
But now do_something_else_with_side_effects
gets executed, making side effects, and things work incorrectly.
How do you guys handle this?
To complicate the matters, I need to handle gone_wrong
and already_fine
conditions in a macro/function, so I'd need to do extra work and wrap the otherwise
section in some kind of conditional, although I'd like not to do this.
In theory, I could do this
...
#[cfg(test)] {
return Ok(())
}
// otherwise
...But it will can be done only in a macro, and imposes a strict limit on the caller's return type.
Any recepies?