Skip to content

Commit

Permalink
Add Conclusion::exit_code and note about cleanup to docs of exit
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
LukasKalbertodt committed Apr 9, 2024
1 parent d68ce73 commit e938e53
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e938e53

Please sign in to comment.