use crate::test_support::{fixture, remove};
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
const ISOLATED_FIXTURE_VARIABLE: &str = "CLI_GIT_NATIVE_SCANNER_FIXTURE";
pub(crate) fn run_isolated(test_path: &str, name: &str, body: fn(&Path)) {
if let Some(directory) = std::env::var_os(ISOLATED_FIXTURE_VARIABLE) {
body(Path::new(&directory));
return;
}
let root: PathBuf = fixture(name);
let home: PathBuf = root.join("home");
std::fs::create_dir(&home).expect("disposable home");
let work: PathBuf = root.join("work");
std::fs::create_dir(&work).expect("control directory");
let output: Output = Command::new(crate::test_support::this_test_binary().expect("test executable"))
.args(["--exact", test_path, "--nocapture", "--test-threads=1"])
.env(ISOLATED_FIXTURE_VARIABLE, &work)
.env("HOME", &home)
.env("FORBIDDEN_STRINGS_CACHE_DIR", root.join("cache"))
.env_remove("XDG_CACHE_HOME")
.env_remove("FORBIDDEN_STRINGS_RULES")
.output()
.expect("start the isolated control");
let stdout: String = String::from_utf8_lossy(&output.stdout).into_owned();
assert!(
output.status.success(),
"isolated control {test_path} failed:\n{stdout}\n{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
stdout.contains("test result: ok. 1 passed; 0 failed"),
"isolated control {test_path} did not run exactly one test:\n{stdout}"
);
remove(root.as_path());
}
pub(crate) fn needle() -> String {
return ["PLANTED", "CANDIDATE", "NEEDLE"].join("_");
}
pub(crate) fn rules_file(directory: &Path) -> PathBuf {
let path: PathBuf = directory.join("rules.txt");
std::fs::write(&path, format!("{}\n", needle())).expect("rules file");
return path;
}