Fix `python_module` test failures w/ system Python and installed uv (#15611)

## Summary

Override `sys.base_prefix` when performing `python_module` tests, in
order to prevent `find_uv_bin()` from finding `uv` installed alongside
system Python, and therefore fix test failures on Gentoo.

Fixes #15368

## Test Plan

```
cargo test --profile=fast-build --features git --features pypi --features python --no-default-features --test it python_module
```

Signed-off-by: Michał Górny <mgorny@gentoo.org>
This commit is contained in:
Michał Górny 2025-09-02 15:45:14 +02:00 committed by GitHub
parent d5bcc0535a
commit 4aaf71a38a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 17 deletions

View File

@ -19,6 +19,17 @@ fn user_scheme_bin_filter() -> (String, String) {
} }
} }
// Override sys.base_prefix with a path that's guaranteed not to contain
// uv, as otherwise the tests may pick up an already installed uv
// when testing against the system Python install. See #15368.
const TEST_SCRIPT: &str = "
import sys
import uv
sys.base_prefix = '/dev/null'
print(uv.find_uv_bin())
";
#[test] #[test]
fn find_uv_bin_venv() { fn find_uv_bin_venv() {
let context = TestContext::new("3.12") let context = TestContext::new("3.12")
@ -48,7 +59,7 @@ fn find_uv_bin_venv() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -91,7 +102,7 @@ fn find_uv_bin_target() {
// We should find the binary in the target directory // We should find the binary in the target directory
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())") .arg(TEST_SCRIPT)
.env(EnvVars::PYTHONPATH, context.temp_dir.child("target").path()), @r" .env(EnvVars::PYTHONPATH, context.temp_dir.child("target").path()), @r"
success: true success: true
exit_code: 0 exit_code: 0
@ -137,7 +148,7 @@ fn find_uv_bin_prefix() {
// We should find the binary in the prefix directory // We should find the binary in the prefix directory
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())") .arg(TEST_SCRIPT)
.env( .env(
EnvVars::PYTHONPATH, EnvVars::PYTHONPATH,
site_packages_path(&context.temp_dir.join("prefix"), "python3.12"), site_packages_path(&context.temp_dir.join("prefix"), "python3.12"),
@ -231,7 +242,7 @@ fn find_uv_bin_in_ephemeral_environment() -> anyhow::Result<()> {
.arg(context.workspace_root.join("scripts/packages/fake-uv")) .arg(context.workspace_root.join("scripts/packages/fake-uv"))
.arg("python") .arg("python")
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -283,7 +294,7 @@ fn find_uv_bin_in_parent_of_ephemeral_environment() -> anyhow::Result<()> {
.arg("anyio") .arg("anyio")
.arg("python") .arg("python")
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), .arg(TEST_SCRIPT),
@r" @r"
success: true success: true
exit_code: 0 exit_code: 0
@ -351,7 +362,7 @@ fn find_uv_bin_user_bin() {
// We should find the binary in the virtual environment first // We should find the binary in the virtual environment first
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -372,7 +383,7 @@ fn find_uv_bin_user_bin() {
// We should find the binary in the bin now // We should find the binary in the bin now
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -442,19 +453,19 @@ fn find_uv_bin_error_message() {
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r#" .arg(TEST_SCRIPT), @r#"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Traceback (most recent call last): Traceback (most recent call last):
File "<string>", line 1, in <module> File "<string>", line 6, in <module>
File "[SITE_PACKAGES]/uv/_find_uv.py", line 50, in find_uv_bin File "[SITE_PACKAGES]/uv/_find_uv.py", line 50, in find_uv_bin
raise UvNotFound( raise UvNotFound(
uv._find_uv.UvNotFound: Could not find the uv binary in any of the following locations: uv._find_uv.UvNotFound: Could not find the uv binary in any of the following locations:
- [VENV]/[BIN] - [VENV]/[BIN]
- [PYTHON-BIN-3.12]/ - /dev/null/[BIN]
- [SITE_PACKAGES]/[BIN] - [SITE_PACKAGES]/[BIN]
- [USER_SCHEME]/[BIN] - [USER_SCHEME]/[BIN]
"# "#
@ -491,7 +502,7 @@ fn find_uv_bin_py38() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -531,7 +542,7 @@ fn find_uv_bin_py39() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -571,7 +582,7 @@ fn find_uv_bin_py310() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -611,7 +622,7 @@ fn find_uv_bin_py311() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -651,7 +662,7 @@ fn find_uv_bin_py312() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -691,7 +702,7 @@ fn find_uv_bin_py313() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -731,7 +742,7 @@ fn find_uv_bin_py314() {
// We should find the binary in the virtual environment // We should find the binary in the virtual environment
uv_snapshot!(context.filters(), context.python_command() uv_snapshot!(context.filters(), context.python_command()
.arg("-c") .arg("-c")
.arg("import uv; print(uv.find_uv_bin())"), @r" .arg(TEST_SCRIPT), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----