Skip to content

Commit 0f7ced8

Browse files
committed
tests: cache managed Python installs
- flip `with_managed_python_dirs()` to set `UV_PYTHON_CACHE_DIR` - add `without_python_download_cache()` for the cold-cache scenarios - keep the offline test pointed at a fresh temp cache so it still fails the right way
1 parent c16a5fd commit 0f7ced8

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

crates/uv/tests/it/common/mod.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,26 @@ impl TestContext {
487487
/// Use a shared global cache for Python downloads.
488488
#[must_use]
489489
pub fn with_python_download_cache(mut self) -> Self {
490-
self.extra_env.push((
491-
EnvVars::UV_PYTHON_CACHE_DIR.into(),
492-
// Respect `UV_PYTHON_CACHE_DIR` if set, or use the default cache directory
493-
env::var_os(EnvVars::UV_PYTHON_CACHE_DIR).unwrap_or_else(|| {
494-
uv_cache::Cache::from_settings(false, None)
495-
.unwrap()
496-
.bucket(CacheBucket::Python)
497-
.into()
498-
}),
499-
));
490+
let key: OsString = EnvVars::UV_PYTHON_CACHE_DIR.into();
491+
self.extra_env.retain(|(existing, _)| existing != &key);
492+
let value = env::var_os(EnvVars::UV_PYTHON_CACHE_DIR).unwrap_or_else(|| {
493+
uv_cache::Cache::from_settings(false, None)
494+
.unwrap()
495+
.bucket(CacheBucket::Python)
496+
.into()
497+
});
498+
self.extra_env.push((key, value));
499+
self
500+
}
501+
502+
/// Disable the shared global cache for Python downloads.
503+
#[must_use]
504+
pub fn without_python_download_cache(mut self) -> Self {
505+
let key: OsString = EnvVars::UV_PYTHON_CACHE_DIR.into();
506+
self.extra_env.retain(|(existing, _)| existing != &key);
507+
let empty_cache = self.temp_dir.child("python-cache-empty");
508+
self.extra_env
509+
.push((key, empty_cache.as_os_str().to_owned()));
500510
self
501511
}
502512

@@ -514,6 +524,8 @@ impl TestContext {
514524
pub fn with_managed_python_dirs(mut self) -> Self {
515525
let managed = self.temp_dir.join("managed");
516526

527+
self = self.with_python_download_cache();
528+
517529
self.extra_env.push((
518530
EnvVars::UV_PYTHON_BIN_DIR.into(),
519531
self.bin_dir.as_os_str().to_owned(),

crates/uv/tests/it/python_install.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,18 +2679,19 @@ fn python_install_no_cache() {
26792679
"cpython-3.12.[PATCH]-[DATE]-[PLATFORM].tar.gz",
26802680
));
26812681
filters.push((r"releases/download/\d{8}/", "releases/download/[DATE]/"));
2682+
let offline_cache = context.temp_dir.child("offline-python-cache");
26822683
uv_snapshot!(filters, context
26832684
.python_install()
26842685
.arg("3.12")
2685-
.arg("--offline"), @r"
2686+
.arg("--offline")
2687+
.env(EnvVars::UV_PYTHON_CACHE_DIR, offline_cache.as_os_str()), @r"
26862688
success: false
26872689
exit_code: 1
26882690
----- stdout -----
26892691
26902692
----- stderr -----
26912693
error: Failed to install cpython-3.12.12-[PLATFORM]
2692-
Caused by: Failed to download https://github.com/astral-sh/python-build-standalone/releases/download/[DATE]/cpython-3.12.[PATCH]-[DATE]-[PLATFORM].tar.gz
2693-
Caused by: Network connectivity is disabled, but the requested data wasn't found in the cache for: `https://github.com/astral-sh/python-build-standalone/releases/download/[DATE]/cpython-3.12.[PATCH]-[DATE]-[PLATFORM].tar.gz`
2694+
Caused by: An offline Python installation was requested, but cpython-3.12.[PATCH]-[DATE]-[PLATFORM].tar.gz) is missing in offline-python-cache
26942695
");
26952696
}
26962697

@@ -2709,9 +2710,10 @@ fn python_install_emulated_macos() {
27092710
if !arch_status.is_ok_and(|x| x.success()) {
27102711
// Rosetta is not available to run the x86_64 interpreter
27112712
// fail the test in CI, otherwise skip it
2712-
if env::var("CI").is_ok() {
2713-
panic!("x86_64 emulation is not available on this CI runner");
2714-
}
2713+
assert!(
2714+
env::var("CI").is_err(),
2715+
"x86_64 emulation is not available on this CI runner"
2716+
);
27152717
debug!("Skipping test because x86_64 emulation is not available");
27162718
return;
27172719
}

0 commit comments

Comments
 (0)