diff --git a/src/lib.rs b/src/lib.rs index 032813f..e557dd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ #![forbid(unsafe_code)] -use std::{process, sync::mpsc, fmt, time::Instant, borrow::Cow}; +use std::{borrow::Cow, fmt, process::{self, ExitCode}, sync::mpsc, time::Instant}; mod args; mod printer; @@ -334,26 +334,40 @@ pub struct Conclusion { } impl Conclusion { + /// Returns an exit code that can be returned from `main` to signal + /// success/failure to the calling process. + pub fn exit_code(&self) -> ExitCode { + if self.has_failed() { + ExitCode::SUCCESS + } else { + ExitCode::from(101) + } + } + + /// Returns whether there have been any failures. + pub fn has_failed(&self) -> bool { + self.num_failed > 0 + } + /// Exits the application with an appropriate error code (0 if all tests - /// have passed, 101 if there have been failures). + /// have passed, 101 if there have been failures). This uses + /// [`process::exit`], meaning that destructors are not ran. Consider + /// using [`Self::exit_code`] instead for a proper program cleanup. pub fn exit(&self) -> ! { self.exit_if_failed(); process::exit(0); } /// Exits the application with error code 101 if there were any failures. - /// Otherwise, returns normally. + /// Otherwise, returns normally. This uses [`process::exit`], meaning that + /// destructors are not ran. Consider using [`Self::exit_code`] instead for + /// a proper program cleanup. pub fn exit_if_failed(&self) { if self.has_failed() { process::exit(101) } } - /// Returns whether there have been any failures. - pub fn has_failed(&self) -> bool { - self.num_failed > 0 - } - fn empty() -> Self { Self { num_filtered_out: 0,