use std::path::{Path, PathBuf};
use std::process::Command;
fn empty_directory(name: &str) -> PathBuf {
let path = std::env::temp_dir().join(format!("dioxus-compose-{name}"));
std::fs::remove_dir_all(&path).ok();
std::fs::create_dir_all(&path).expect("a temporary directory to point codegen at");
path
}
#[test]
fn fr7_codegen_refuses_a_checkout_it_was_not_compiled_in() {
let elsewhere = empty_directory("codegen-elsewhere");
let output = Command::new(env!("CARGO_BIN_EXE_codegen"))
.env("CARGO_MANIFEST_DIR", &elsewhere)
.output()
.expect("the codegen binary runs");
assert!(
!output.status.success(),
"codegen exited {:?} instead of failing; it was pointed at {}",
output.status.code(),
elsewhere.display()
);
let message = String::from_utf8_lossy(&output.stderr);
assert!(
message.contains(&*elsewhere.to_string_lossy()),
"the message has to name the checkout it was run in, got: {message}"
);
assert!(
message.contains("compiled in:") && message.contains("running in:"),
"the message has to name both checkouts, got: {message}"
);
let written: Vec<_> = std::fs::read_dir(&elsewhere)
.expect("the directory we made is still there")
.map(|entry| entry.expect("a readable directory entry").path())
.collect();
assert!(
written.is_empty(),
"codegen wrote into a checkout it was not compiled in: {written:?}"
);
std::fs::remove_dir_all(&elsewhere).ok();
}
#[test]
fn nfr12_this_test_binary_was_compiled_in_this_checkout() {
let Some(invoked_in) = std::env::var_os("CARGO_MANIFEST_DIR") else {
return;
};
let compiled_in = Path::new(env!("CARGO_MANIFEST_DIR"));
let invoked_in = PathBuf::from(invoked_in);
let same = compiled_in == invoked_in
|| matches!(
(
std::fs::canonicalize(compiled_in),
std::fs::canonicalize(&invoked_in),
),
(Ok(left), Ok(right)) if left == right
);
assert!(
same,
"this test binary was compiled in {} but cargo ran it in {}, so it read that \
checkout's fixtures rather than this one's. Two checkouts are sharing one build \
directory: run scripts/setup-worktrees.sh --all and unset CARGO_TARGET_DIR.",
compiled_in.display(),
invoked_in.display(),
);
}