#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
pub struct Run {
pub code: i32,
pub stdout: String,
pub stderr: String,
}
impl Run {
pub fn json(&self) -> serde_json::Value {
serde_json::from_str(&self.stdout)
.unwrap_or_else(|error| panic!("stdout is not JSON ({error}):\n{}", self.stdout))
}
}
pub fn run<S: AsRef<std::ffi::OsStr>>(args: &[S]) -> Run {
let output = Command::new(env!("CARGO_BIN_EXE_openbim-ifc"))
.args(args)
.current_dir(root())
.env_remove("AUTHORED_DUMP")
.output()
.expect("the binary runs");
Run {
code: output.status.code().expect("exited, not signalled"),
stdout: String::from_utf8(output.stdout).expect("stdout is UTF-8"),
stderr: String::from_utf8(output.stderr).expect("stderr is UTF-8"),
}
}
pub fn root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.canonicalize()
.expect("repository root")
}
pub fn fixture(relative: &str) -> String {
let path = format!("test/fixtures/{relative}");
assert!(root().join(&path).is_file(), "{path} is missing");
path
}
pub fn corpus() -> Vec<String> {
fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
for entry in std::fs::read_dir(dir).expect("fixture directory") {
let path = entry.expect("entry").path();
if path.is_dir() {
walk(&path, out);
} else if path.extension().is_some_and(|extension| extension == "ifc") {
out.push(path);
}
}
}
let root = root();
let mut found = Vec::new();
walk(&root.join("test/fixtures"), &mut found);
let mut relative: Vec<String> = found
.iter()
.map(|path| {
path.strip_prefix(&root)
.expect("under the root")
.to_string_lossy()
.replace('\\', "/")
})
.collect();
relative.sort();
assert!(relative.len() >= 40, "found {} fixtures", relative.len());
relative
}
pub fn scratch(name: &str) -> PathBuf {
let dir = Path::new(env!("CARGO_TARGET_TMPDIR"))
.join("openbim-ifc-cli")
.join(name);
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("scratch directory");
dir
}