53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import re
|
|
|
|
MODELS_FILE = "rust/crates/rusty-claude-cli/src/config/models.rs"
|
|
|
|
with open(MODELS_FILE, 'r') as f:
|
|
content = f.read()
|
|
|
|
content = content.replace("use runtime::test_utils::{env_lock, temp_dir, with_current_dir};", """
|
|
use std::sync::{Mutex, MutexGuard, OnceLock};
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
use std::path::{Path, PathBuf};
|
|
use std::fs;
|
|
|
|
fn env_lock() -> MutexGuard<'static, ()> {
|
|
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
|
|
LOCK.get_or_init(|| Mutex::new(()))
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
}
|
|
|
|
fn cwd_lock() -> MutexGuard<'static, ()> {
|
|
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
|
|
LOCK.get_or_init(|| Mutex::new(()))
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
}
|
|
|
|
fn temp_dir() -> PathBuf {
|
|
static COUNTER: AtomicUsize = AtomicUsize::new(0);
|
|
let nanos = std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.subsec_nanos();
|
|
let unique = COUNTER.fetch_add(1, Ordering::Relaxed);
|
|
std::env::temp_dir().join(format!("rusty-claude-cli-{nanos}-{unique}"))
|
|
}
|
|
|
|
fn with_current_dir<T>(cwd: &Path, f: impl FnOnce() -> T) -> T {
|
|
let _guard = cwd_lock();
|
|
let previous = std::env::current_dir().expect("cwd should load");
|
|
std::env::set_current_dir(cwd).expect("cwd should change");
|
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
|
|
std::env::set_current_dir(previous).expect("cwd should restore");
|
|
match result {
|
|
Ok(value) => value,
|
|
Err(payload) => std::panic::resume_unwind(payload),
|
|
}
|
|
}
|
|
""")
|
|
|
|
with open(MODELS_FILE, 'w') as f:
|
|
f.write(content)
|