返回 CodeWhale
test_support.rs
根目录 / crates / runtime / src / config_persistence / test_support.rs
1 use super::*;
2 use std::env;
3 use std::ffi::OsString;
4 use std::fs;
5
6 // The fixture resolves the exact declaration from its destination document.
7 // Persistence still re-reads and validates under the shared write lock.
8 pub fn selection_identity(path: &Path, id: &str) -> crate::config::ProviderIdentity {
9 crate::config::parse_config_base(&fs::read_to_string(path).unwrap())
10 .unwrap()
11 .resolve_provider_pin_identity(id)
12 .unwrap()
13 }
14
15 /// Clears every model override the dispatcher's env layer reads for the
16 /// providers exercised below, so the assertion is about config precedence
17 /// and cannot be flipped by an ambient variable on a developer machine.
18 pub struct ModelEnvGuard {
19 saved: Vec<(&'static str, Option<OsString>)>,
20 }
21
22 impl ModelEnvGuard {
23 const VARS: &'static [&'static str] = &[
24 "CODEWHALE_MODEL",
25 "DEEPSEEK_MODEL",
26 "DEEPSEEK_DEFAULT_TEXT_MODEL",
27 "GLM_MODEL",
28 "BIGMODEL_MODEL",
29 "ZAI_MODEL",
30 "XAI_MODEL",
31 "GROK_MODEL",
32 "OPENROUTER_MODEL",
33 "OLLAMA_MODEL",
34 ];
35
36 pub fn new() -> Self {
37 let saved = Self::VARS
38 .iter()
39 .map(|name| (*name, env::var_os(name)))
40 .collect();
41 // Safety: test-only environment mutation; the caller holds the
42 // process-wide test-env lock via `EnvGuard`.
43 unsafe {
44 for name in Self::VARS {
45 env::remove_var(name);
46 }
47 }
48 Self { saved }
49 }
50 }
51
52 impl Default for ModelEnvGuard {
53 fn default() -> Self {
54 Self::new()
55 }
56 }
57
58 impl Drop for ModelEnvGuard {
59 fn drop(&mut self) {
60 // Safety: test-only environment restoration under the same lock.
61 unsafe {
62 for (name, value) in &self.saved {
63 match value {
64 Some(value) => env::set_var(name, value),
65 None => env::remove_var(name),
66 }
67 }
68 }
69 }
70 }
71
71 lines RUST