#![allow(clippy::unwrap_used)]
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
const SIGNALED: &str =
"ERROR cargo_tarpaulin: Failed to run tests: Attempting to handle tarpaulin being signaled";
struct Probe {
dir: TempDir,
bin: PathBuf,
tally: PathBuf,
}
struct Answer {
code: i32,
err: String,
calls: usize,
}
impl Probe {
fn new(body: &str) -> Self {
let dir = tempfile::tempdir().unwrap();
let status = yog::git_env::git()
.current_dir(dir.path())
.args(["init", "-q"])
.status()
.unwrap();
assert!(status.success(), "git init");
let bin = dir.path().join("bin");
fs::create_dir(&bin).unwrap();
let tally = dir.path().join("calls");
fs::write(&tally, "").unwrap();
let (t, make) = (tally.display(), bin.join("make"));
let script =
format!("#!/usr/bin/env bash\necho x >>\"{t}\"\nn=$(wc -l <\"{t}\")\n{body}\n");
fs::write(&make, script).unwrap();
fs::set_permissions(&make, fs::Permissions::from_mode(0o755)).unwrap();
Self { dir, bin, tally }
}
fn run(&self) -> Answer {
let path = format!(
"{}:{}",
self.bin.display(),
std::env::var("PATH").unwrap_or_default()
);
let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts/check-coverage.sh");
let out = yog::git_env::command(&script)
.current_dir(self.dir.path())
.env("PATH", path)
.output()
.unwrap();
Answer {
code: out.status.code().unwrap(),
err: String::from_utf8_lossy(&out.stderr).into_owned(),
calls: fs::read_to_string(&self.tally).unwrap().lines().count(),
}
}
}
#[test]
fn a_signaled_tarpaulin_is_retried_and_answered_with_no_verdict() {
let a = Probe::new(&format!("echo '{SIGNALED}' >&2\nexit 1")).run();
assert_eq!(a.code, 75, "exit 75 is 'no verdict': {}", a.err);
assert_eq!(a.calls, 2, "signaled once is retried exactly once");
assert!(a.err.contains("SIGNALED (attempt 1)"), "{}", a.err);
assert!(a.err.contains("SIGNALED (attempt 2)"), "{}", a.err);
assert!(a.err.contains("bl-673a"), "the diagnosis cites its ball");
}
#[test]
fn a_signal_the_retry_survives_is_a_pass() {
let body = format!("if [ \"$n\" -eq 1 ]; then echo '{SIGNALED}' >&2; exit 1; fi\nexit 0");
let a = Probe::new(&body).run();
assert_eq!(a.code, 0, "the retry earned the verdict: {}", a.err);
assert_eq!(a.calls, 2);
}
#[test]
fn a_real_failure_is_answered_once_and_replayed() {
let a = Probe::new("echo 'test yog::sinks ... FAILED'\nexit 1").run();
assert_eq!(a.code, 1, "a verdict about the tree: {}", a.err);
assert_eq!(a.calls, 1, "a real failure is not retried");
assert!(a.err.contains("test yog::sinks ... FAILED"), "{}", a.err);
}
#[test]
fn a_pass_is_quiet() {
let a = Probe::new("echo '|| yog/src/lib.rs | 100% |'\nexit 0").run();
assert_eq!(a.code, 0, "{}", a.err);
assert_eq!(a.calls, 1);
assert!(
!a.err.contains("100%"),
"held stdout replayed on a pass: {}",
a.err
);
}