Skip to content

Commit 86e5a31

Browse files
authored
[ty] Introduce and use System::env_var for better test isolation (astral-sh#18538)
1 parent 0c20010 commit 86e5a31

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

crates/ruff_db/src/system.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ pub trait System: Debug {
171171
PatternError,
172172
>;
173173

174+
/// Fetches the environment variable `key` from the current process.
175+
///
176+
/// # Errors
177+
///
178+
/// Returns [`std::env::VarError::NotPresent`] if:
179+
/// - The variable is not set.
180+
/// - The variable's name contains an equal sign or NUL (`'='` or `'\0'`).
181+
///
182+
/// Returns [`std::env::VarError::NotUnicode`] if the variable's value is not valid
183+
/// Unicode.
184+
fn env_var(&self, name: &str) -> std::result::Result<String, std::env::VarError> {
185+
let _ = name;
186+
Err(std::env::VarError::NotPresent)
187+
}
188+
174189
fn as_any(&self) -> &dyn std::any::Any;
175190

176191
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;

crates/ruff_db/src/system/os.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ impl System for OsSystem {
214214
})
215215
})))
216216
}
217+
218+
fn env_var(&self, name: &str) -> std::result::Result<String, std::env::VarError> {
219+
std::env::var(name)
220+
}
217221
}
218222

219223
impl OsSystem {

crates/ty_project/src/metadata/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ impl Options {
192192
PythonPath::sys_prefix(python_path.absolute(project_root, system), origin)
193193
})
194194
.or_else(|| {
195-
std::env::var("VIRTUAL_ENV").ok().map(|virtual_env| {
195+
system.env_var("VIRTUAL_ENV").ok().map(|virtual_env| {
196196
PythonPath::sys_prefix(virtual_env, SysPrefixPathOrigin::VirtualEnvVar)
197197
})
198198
})
199199
.or_else(|| {
200-
std::env::var("CONDA_PREFIX").ok().map(|path| {
200+
system.env_var("CONDA_PREFIX").ok().map(|path| {
201201
PythonPath::sys_prefix(path, SysPrefixPathOrigin::CondaPrefixVar)
202202
})
203203
})

crates/ty_server/src/system.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ impl System for LSPSystem {
247247
fn case_sensitivity(&self) -> CaseSensitivity {
248248
self.os_system.case_sensitivity()
249249
}
250+
251+
fn env_var(&self, name: &str) -> std::result::Result<String, std::env::VarError> {
252+
self.os_system.env_var(name)
253+
}
250254
}
251255

252256
fn not_a_text_document(path: impl Display) -> std::io::Error {

0 commit comments

Comments
 (0)