Skip to content

Commit 02018f8

Browse files
committed
refactor: streamline test Python cache helpers
Make `with_python_download_cache()` private and remove redundant explicit calls across test files. Fixes #16721
1 parent 0f7ced8 commit 02018f8

File tree

5 files changed

+138
-73
lines changed

5 files changed

+138
-73
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl TestContext {
486486

487487
/// Use a shared global cache for Python downloads.
488488
#[must_use]
489-
pub fn with_python_download_cache(mut self) -> Self {
489+
fn with_python_download_cache(mut self) -> Self {
490490
let key: OsString = EnvVars::UV_PYTHON_CACHE_DIR.into();
491491
self.extra_env.retain(|(existing, _)| existing != &key);
492492
let value = env::var_os(EnvVars::UV_PYTHON_CACHE_DIR).unwrap_or_else(|| {
@@ -504,9 +504,6 @@ impl TestContext {
504504
pub fn without_python_download_cache(mut self) -> Self {
505505
let key: OsString = EnvVars::UV_PYTHON_CACHE_DIR.into();
506506
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()));
510507
self
511508
}
512509

crates/uv/tests/it/pip_install.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13121,7 +13121,6 @@ fn pip_install_no_sources_editable_to_registry_switch() -> Result<()> {
1312113121
#[test]
1312213122
fn install_with_system_interpreter() {
1312313123
let context = TestContext::new_with_versions(&[])
13124-
.with_python_download_cache()
1312513124
.with_managed_python_dirs()
1312613125
.with_filtered_python_keys();
1312713126

@@ -13145,3 +13144,81 @@ fn install_with_system_interpreter() {
1314513144
"
1314613145
);
1314713146
}
13147+
13148+
/// Test that a missing Python version is not installed when not using `--target` or `--prefix`.
13149+
#[cfg(feature = "python-managed")]
13150+
#[test]
13151+
fn install_missing_python_no_target() {
13152+
// Create a context that only has Python 3.11 available.
13153+
let context = TestContext::new("3.11").with_managed_python_dirs();
13154+
13155+
// Request Python 3.12; which should fail
13156+
uv_snapshot!(context.filters(), context.pip_install()
13157+
.arg("--python").arg("3.12")
13158+
.arg("anyio"), @r###"
13159+
success: false
13160+
exit_code: 2
13161+
----- stdout -----
13162+
13163+
----- stderr -----
13164+
error: No virtual environment found for Python 3.12; run `uv venv` to create an environment, or pass `--system` to install into a non-virtual environment
13165+
"###
13166+
);
13167+
}
13168+
13169+
// If there are no python interpreters available, `uv pip install` into a target should install one.
13170+
#[cfg(feature = "python-managed")]
13171+
#[test]
13172+
fn install_missing_python_with_target() {
13173+
// Create a context with no installed python interpreters.
13174+
let context = TestContext::new_with_versions(&[]).with_managed_python_dirs();
13175+
13176+
let target_dir = context.temp_dir.child("target-dir");
13177+
13178+
uv_snapshot!(context.filters(), context.pip_install()
13179+
.arg("anyio")
13180+
.arg("--target").arg(target_dir.path()), @r###"
13181+
success: true
13182+
exit_code: 0
13183+
----- stdout -----
13184+
13185+
----- stderr -----
13186+
Using CPython 3.14.0
13187+
Resolved 3 packages in [TIME]
13188+
Prepared 3 packages in [TIME]
13189+
Installed 3 packages in [TIME]
13190+
+ anyio==4.3.0
13191+
+ idna==3.6
13192+
+ sniffio==1.3.1
13193+
"###
13194+
);
13195+
}
13196+
13197+
#[cfg(feature = "python-managed")]
13198+
#[test]
13199+
fn install_missing_python_version_with_target() {
13200+
// Create a context that only has Python 3.11 available.
13201+
let context = TestContext::new("3.11").with_managed_python_dirs();
13202+
13203+
let target_dir = context.temp_dir.child("target-dir");
13204+
13205+
// Request Python 3.12 which is not installed in this context.
13206+
uv_snapshot!(context.filters(), context.pip_install()
13207+
.arg("anyio")
13208+
.arg("--python").arg("3.12")
13209+
.arg("--target").arg(target_dir.path()), @r###"
13210+
success: true
13211+
exit_code: 0
13212+
----- stdout -----
13213+
13214+
----- stderr -----
13215+
Using CPython 3.12.12
13216+
Resolved 3 packages in [TIME]
13217+
Prepared 3 packages in [TIME]
13218+
Installed 3 packages in [TIME]
13219+
+ anyio==4.3.0
13220+
+ idna==3.6
13221+
+ sniffio==1.3.1
13222+
"###
13223+
);
13224+
}

crates/uv/tests/it/python_find.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,6 @@ fn python_find_freethreaded_313() {
12641264
.with_filtered_python_keys()
12651265
.with_filtered_python_sources()
12661266
.with_managed_python_dirs()
1267-
.with_python_download_cache()
12681267
.with_filtered_python_install_bin()
12691268
.with_filtered_python_names()
12701269
.with_filtered_exe_suffix();
@@ -1303,7 +1302,6 @@ fn python_find_freethreaded_314() {
13031302
.with_filtered_python_keys()
13041303
.with_filtered_python_sources()
13051304
.with_managed_python_dirs()
1306-
.with_python_download_cache()
13071305
.with_filtered_python_install_bin()
13081306
.with_filtered_python_names()
13091307
.with_filtered_exe_suffix();
@@ -1380,7 +1378,6 @@ fn python_find_prerelease_version_specifiers() {
13801378
.with_filtered_python_keys()
13811379
.with_filtered_python_sources()
13821380
.with_managed_python_dirs()
1383-
.with_python_download_cache()
13841381
.with_filtered_python_install_bin()
13851382
.with_filtered_python_names()
13861383
.with_filtered_exe_suffix();
@@ -1479,7 +1476,6 @@ fn python_find_prerelease_with_patch_request() {
14791476
.with_filtered_python_keys()
14801477
.with_filtered_python_sources()
14811478
.with_managed_python_dirs()
1482-
.with_python_download_cache()
14831479
.with_filtered_python_install_bin()
14841480
.with_filtered_python_names()
14851481
.with_filtered_exe_suffix();

0 commit comments

Comments
 (0)