use slotgate::execution::job_outcome::JobOutcome;
use slotgate::execution::job_status::JobStatus;
use slotgate::execution::run_summary::RunSummary;
use std::path::PathBuf;
use std::process::ExitCode;
use std::time::Duration;
fn outcome(name: &str, status: JobStatus) -> JobOutcome {
JobOutcome {
job_name: String::from(name),
status,
duration: Duration::from_millis(1),
stdout_path: PathBuf::from("out.log"),
stderr_path: PathBuf::from("err.log"),
}
}
#[test]
fn exit_code_is_success_only_when_the_run_succeeded() {
let passed = RunSummary::from_outcomes(&[outcome("a", JobStatus::Passed)]);
let failed = RunSummary::from_outcomes(&[outcome("a", JobStatus::Failed)]);
let timed_out = RunSummary::from_outcomes(&[outcome("a", JobStatus::TimedOut)]);
assert_eq!(
format!("{:?}", passed.exit_code()),
format!("{:?}", ExitCode::SUCCESS)
);
assert_eq!(
format!("{:?}", failed.exit_code()),
format!("{:?}", ExitCode::FAILURE)
);
assert_eq!(
format!("{:?}", timed_out.exit_code()),
format!("{:?}", ExitCode::FAILURE)
);
}
#[test]
fn from_outcomes_counts_each_status_into_its_own_bucket() {
let outcomes = vec![
outcome("a", JobStatus::Passed),
outcome("b", JobStatus::Failed),
outcome("c", JobStatus::Passed),
outcome("d", JobStatus::TimedOut),
outcome("e", JobStatus::Passed),
];
let summary = RunSummary::from_outcomes(&outcomes);
assert_eq!(summary.passed, 3);
assert_eq!(summary.failed, 1);
assert_eq!(summary.timed_out, 1);
}
#[test]
fn from_outcomes_of_an_empty_run_counts_nothing() {
let outcomes: Vec<JobOutcome> = Vec::new();
let summary = RunSummary::from_outcomes(&outcomes);
assert_eq!(summary.passed, 0);
assert_eq!(summary.failed, 0);
assert_eq!(summary.timed_out, 0);
assert_eq!(summary.total(), 0);
}
#[test]
fn is_success_for_an_empty_run_because_nothing_went_wrong() {
let summary = RunSummary::from_outcomes(&[]);
assert!(summary.is_success());
}
#[test]
fn is_success_only_when_nothing_failed_and_nothing_timed_out() {
let all_passed = RunSummary::from_outcomes(&[
outcome("a", JobStatus::Passed),
outcome("b", JobStatus::Passed),
]);
let with_failure = RunSummary::from_outcomes(&[
outcome("a", JobStatus::Passed),
outcome("b", JobStatus::Failed),
]);
let with_timeout = RunSummary::from_outcomes(&[
outcome("a", JobStatus::Passed),
outcome("b", JobStatus::TimedOut),
]);
assert!(all_passed.is_success());
assert!(!with_failure.is_success());
assert!(!with_timeout.is_success());
}
#[test]
fn render_reports_every_count_and_the_total() {
let summary = RunSummary::from_outcomes(&[
outcome("a", JobStatus::Passed),
outcome("b", JobStatus::Passed),
outcome("c", JobStatus::Failed),
outcome("d", JobStatus::TimedOut),
]);
let rendered = summary.render();
assert_eq!(
rendered,
"SLOTGATE — SUMMARY: 2 passed, 1 failed, 1 timed out (of 4)"
);
}
#[test]
fn total_accounts_for_every_outcome_handed_in() {
let outcomes = vec![
outcome("a", JobStatus::Passed),
outcome("b", JobStatus::Failed),
outcome("c", JobStatus::TimedOut),
];
let summary = RunSummary::from_outcomes(&outcomes);
assert_eq!(summary.total(), outcomes.len());
}