#![allow(dead_code)]
#![allow(clippy::expect_used)]
use assert_cmd::Command;
use tempfile::TempDir;
pub const ELF_HEADER: &[u8] = b"\x7fELF\x02\x01\x01\x00";
pub fn rmagic_cmd() -> Command {
Command::new(assert_cmd::cargo::cargo_bin!("rmagic"))
}
pub fn create_data_file(dir: &TempDir, filename: &str, content: &[u8]) -> std::path::PathBuf {
let path = dir.path().join(filename);
std::fs::write(&path, content).expect("Failed to create data file");
path
}
pub fn path_str(path: &std::path::Path) -> &str {
path.to_str().expect("Invalid path")
}
pub fn try_symlink(target: &std::path::Path, link: &std::path::Path) -> std::io::Result<()> {
#[cfg(unix)]
{
std::os::unix::fs::symlink(target, link)
}
#[cfg(windows)]
{
if target.is_dir() {
std::os::windows::fs::symlink_dir(target, link)
} else {
std::os::windows::fs::symlink_file(target, link)
}
}
#[cfg(not(any(unix, windows)))]
{
let _ = (target, link);
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"symlink creation is not supported on this platform",
))
}
}
#[must_use]
pub fn symlink_or_skip(target: &std::path::Path, link: &std::path::Path, test_name: &str) -> bool {
match try_symlink(target, link) {
Ok(()) => true,
Err(e) => {
assert!(
std::env::var_os("RMAGIC_REQUIRE_SYMLINKS").is_none(),
"{test_name}: symlink creation failed ({e}) but \
RMAGIC_REQUIRE_SYMLINKS is set, so skipping is not permitted"
);
eprintln!("Skipping {test_name}: cannot create symlink ({e})");
false
}
}
}