#[cfg(test)]
pub mod test_utils {
use log::LevelFilter;
use log::info;
use std::fs;
use std::path::Path;
use std::sync::Once;
static INIT: Once = Once::new();
const CLEAN_CACHE_ENV: &str = "CLEAN_TEST_CACHE";
pub fn setup() {
INIT.call_once(|| {
let _ = std::panic::catch_unwind(|| {
env_logger::builder().filter_level(LevelFilter::Info).init();
});
});
if let Ok(clean_cache) = std::env::var(CLEAN_CACHE_ENV)
&& clean_cache == "1"
{
clean_cache_dir();
}
}
fn clean_cache_dir() {
let cache_dir = Path::new("/tmp/code_cache");
if cache_dir.exists() {
info!("正在清理缓存目录: {cache_dir:?}");
match fs::read_dir(cache_dir) {
Ok(entries) => {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
if let Err(e) = fs::remove_file(&path) {
info!("删除文件失败 {path:?}: {e}");
} else {
info!("已删除缓存文件: {path:?}");
}
}
}
info!("缓存目录清理完成");
}
Err(e) => {
info!("读取缓存目录失败: {e}");
}
}
} else {
info!("缓存目录不存在,创建目录: {cache_dir:?}");
if let Err(e) = fs::create_dir_all(cache_dir) {
info!("创建缓存目录失败: {e}");
} else {
info!("缓存目录创建成功");
}
}
if !cache_dir.exists()
&& let Err(e) = fs::create_dir_all(cache_dir)
{
info!("创建缓存目录失败: {e}");
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Err(e) = fs::set_permissions(cache_dir, fs::Permissions::from_mode(0o775)) {
info!("设置缓存目录权限失败: {e}");
} else {
info!("设置缓存目录权限成功");
}
}
}
}
pub mod js_tests;
pub mod python_tests;
pub mod ts_tests;