use std::path::{Path, PathBuf};
use std::process::Command;
fn check_consumer(dir: &Path, lib_rs: &str) -> (bool, String) {
let forma_ir = Path::new(env!("CARGO_MANIFEST_DIR"));
std::fs::create_dir_all(dir.join("src")).expect("scratch dir");
std::fs::write(
dir.join("Cargo.toml"),
format!(
"[package]\nname = \"fault-isolation-probe\"\nversion = \"0.0.0\"\nedition = \"2018\"\n\
publish = false\n\n[dependencies]\n\
forma-ir = {{ path = '{}', default-features = false, features = [\"test-helpers\"] }}\n\n\
# Its own workspace, not this one.\n[workspace]\n",
forma_ir.display()
),
)
.expect("scratch manifest");
std::fs::write(dir.join("src/lib.rs"), lib_rs).expect("scratch lib");
let output = Command::new(env!("CARGO"))
.args(["check", "--offline", "--quiet"])
.current_dir(dir)
.env("CARGO_TARGET_DIR", dir.join("target"))
.output()
.expect("cargo runs");
let report = format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
(output.status.success(), report)
}
#[test]
fn test_helpers_alone_does_not_compile_the_fault_trigger_in() {
let dir: PathBuf = Path::new(env!("CARGO_TARGET_TMPDIR")).join("fault-isolation-probe");
let (compiled, report) = check_consumer(
&dir,
"pub fn encoders() -> Vec<u8> { \
forma_ir::parser::test_helpers::build_minimal_ir(&[\"p\"], &[], &[], &[]) }\n",
);
assert!(compiled, "test-helpers' encoders must compile:\n{report}");
let (compiled, report) = check_consumer(
&dir,
"pub fn trigger() -> &'static str { forma_ir::fault_injection::FAULT_TRIGGER }\n",
);
assert!(
!compiled,
"test-helpers must not compile in the fault trigger, yet this built:\n{report}"
);
assert!(
report.contains("fault_injection"),
"it failed for another reason:\n{report}"
);
}