use super::support::{FIXTURE_ROOT_VARIABLE, Fixture, fixture, settle_fixture};
use std::path::{Path, PathBuf};
#[test]
fn a_fixture_is_removed_when_its_test_panics() {
let (sender, receiver) = std::sync::mpsc::channel::<PathBuf>();
let handle = std::thread::spawn(move || {
let created: Fixture = fixture("drop-on-panic");
std::fs::write(created.root.join("file"), "content").expect("file");
sender.send(created.root.clone()).expect("send the root");
panic!("the test fails before it removes its fixture");
});
assert!(handle.join().is_err(), "the thread panics on purpose");
let root: PathBuf = receiver.recv().expect("the root");
assert!(!root.exists(), "{} was left behind", root.display());
}
#[test]
fn a_fixture_removed_explicitly_is_dropped_quietly() {
let created: Fixture = fixture("drop-after-remove");
let root: PathBuf = created.root.clone();
super::support::remove(&created);
assert!(!root.exists());
drop(created);
}
#[test]
fn settling_keeps_removes_and_accepts_a_tree_that_is_gone() {
let created: Fixture = fixture("settle");
let root: &Path = created.root.as_path();
std::fs::write(root.join("file"), "content").expect("file");
settle_fixture(root, true);
assert!(root.join("file").exists(), "a kept fixture stays");
settle_fixture(root, false);
assert!(!root.exists(), "a settled fixture goes");
settle_fixture(root, false);
}
#[test]
fn fixtures_are_created_outside_the_system_temporary_directory() {
if std::env::var_os(FIXTURE_ROOT_VARIABLE).is_some() {
return;
}
let created: Fixture = fixture("location");
assert!(
created.root.starts_with(std::fs::canonicalize(env!("CARGO_TARGET_TMPDIR")).expect("target scratch")),
"{} is not under the target scratch directory",
created.root.display()
);
}