use assert_cmd::cargo;
use std::path::PathBuf;
use std::process::Command;
use std::sync::OnceLock;
use tempfile::TempDir;
use walkdir::WalkDir;
static BENCHMARK_DIR: OnceLock<PathBuf> = OnceLock::new();
#[allow(unused)]
pub struct TestEnv {
_tmp: TempDir,
eval_out: PathBuf,
agent_dir: PathBuf,
}
#[allow(unused)]
impl TestEnv {
pub fn new() -> Self {
let agent_dir: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.join("test_agent");
let tmp = tempfile::tempdir()
.expect("Failed to create temp dir for test env");
let eval_out = tmp.path().join("test_eval_out");
std::fs::create_dir_all(&eval_out)
.expect("Failed to create eval_out dir");
Self { _tmp: tmp, eval_out, agent_dir }
}
pub fn eval_out(&self) -> &PathBuf {
&self.eval_out
}
pub fn agent_dir(&self) -> &PathBuf {
&self.agent_dir
}
}
#[allow(unused)]
pub fn scarf_command() -> Command {
Command::new(cargo::cargo_bin!("scarf"))
}
#[allow(unused)]
pub fn benchmark_dir() -> &'static PathBuf {
BENCHMARK_DIR.get_or_init(|| {
let dir = std::env::temp_dir()
.join("scarfbench-tests-benchmark")
.join("benchmark");
if !(dir.exists()
&& dir
.read_dir()
.ok()
.map(|mut it| it.next().is_some())
.unwrap_or(false))
{
let output = scarf_command()
.arg("bench")
.arg("pull")
.arg("--dest")
.arg(dir.to_str().unwrap())
.output()
.expect("Run scarf bench pull --dest ...");
assert!(
output.status.success(),
"bench pull failed.\nstderr: {}\nstdout: {}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout),
);
}
dir
})
}
#[allow(unused)]
pub fn bench_pull_save_dest() -> PathBuf {
tempfile::tempdir()
.expect("Failed to create temp dir for saving the pulled benchmark")
.path()
.join("bench_pull_save_dest")
.to_path_buf()
}
#[allow(unused)]
pub fn find_first_app(bench_dir: &PathBuf) -> (String, String, String) {
let (layer, app, framework) = WalkDir::new(bench_dir)
.min_depth(3)
.max_depth(3)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_dir())
.find_map(|entry| {
let rel = entry
.path()
.strip_prefix(bench_dir)
.expect("Failed to get relative path");
let mut it = rel.iter().map(|p| p.to_string_lossy().into_owned());
match (it.next(), it.next(), it.next()) {
(Some(layer), Some(app), Some(framework)) => {
Some((layer, app, framework))
},
_ => None,
}
})
.expect("No application found in benchmark");
(layer, app, framework)
}