//! Common utilities for integration tests. use std::fs; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; use rand; /// Test fixture for integration tests. pub struct TestFixture { pub scripts_dir: PathBuf, } impl TestFixture { /// Create a new test fixture. pub fn new() -> Self { let scripts_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("tests") .join("rhai_scripts"); Self { scripts_dir, } } /// Get the path to a test script. pub fn script_path(&self, name: &str) -> PathBuf { self.scripts_dir.join(name) } } /// Create a temporary test directory with a unique name. pub fn setup_test_dir(prefix: &str) -> PathBuf { let timestamp = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_millis(); let test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("tests") .join("temp") .join(format!("{}_{}_{}", prefix, timestamp, rand::random::())); fs::create_dir_all(&test_dir).unwrap(); test_dir } /// Clean up a temporary test directory. pub fn cleanup_test_dir(dir: PathBuf) { if dir.exists() && dir.starts_with(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("temp")) { let _ = fs::remove_dir_all(dir); } }