From 0ae3fce5999c734e8d311ed50df2ad8009b1fa51 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 28 Jan 2025 13:15:15 -0600 Subject: [PATCH] Add test coverage for #10978 (#11029) --- crates/uv/tests/it/pip_compile.rs | 84 +++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/crates/uv/tests/it/pip_compile.rs b/crates/uv/tests/it/pip_compile.rs index 9e18062c4df7..991657499839 100644 --- a/crates/uv/tests/it/pip_compile.rs +++ b/crates/uv/tests/it/pip_compile.rs @@ -1344,6 +1344,90 @@ fn compile_python_312_annotation_line() -> Result<()> { Ok(()) } +/// Compile for 3.12 when only a different interpreter version is available. +#[test] +fn compile_fallback_interpreter() -> Result<()> { + let context = TestContext::new("3.10"); + let requirements_in = context.temp_dir.child("requirements.in"); + requirements_in.write_str("black==23.10.1")?; + + uv_snapshot!(context.filters(), context.pip_compile() + .arg("requirements.in") + .arg("--python-version") + .arg("3.12"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by uv via the following command: + # uv pip compile --cache-dir [CACHE_DIR] requirements.in --python-version 3.12 + black==23.10.[X] + # via -r requirements.in + click==8.1.7 + # via black + mypy-extensions==1.0.0 + # via black + packaging==24.0 + # via black + pathspec==0.12.1 + # via black + platformdirs==4.2.0 + # via black + + ----- stderr ----- + warning: The requested Python version 3.12 is not available; 3.10.[X] will be used to build dependencies instead. + Resolved 6 packages in [TIME] + "### + ); + + Ok(()) +} + +/// Compile for 3.12 when only a different interpreter version is available, there's also +/// a broken interpreter in the PATH. +#[test] +#[cfg(unix)] +fn compile_fallback_interpreter_broken_in_path() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let context = TestContext::new("3.10"); + let requirements_in = context.temp_dir.child("requirements.in"); + requirements_in.write_str("black==23.10.1")?; + + // Create a "broken" Python executable in the test context `bin` + let contents = r"#!/bin/sh + echo 'error: intentionally broken python executable' >&2 + exit 1"; + let python = context + .bin_dir + .join(format!("python3{}", std::env::consts::EXE_SUFFIX)); + fs_err::write(&python, contents).unwrap(); + + let mut perms = fs_err::metadata(&python).unwrap().permissions(); + perms.set_mode(0o755); + fs_err::set_permissions(&python, perms).unwrap(); + + uv_snapshot!(context.filters(), context.pip_compile() + .arg("requirements.in") + .arg("--python-version") + .arg("3.12") + // In tests, we ignore `PATH` during Python discovery so we need to add the context `bin` + .env("UV_TEST_PYTHON_PATH", context.bin_dir.as_os_str()), @r###" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: Failed to inspect Python interpreter from search path at `[BIN]/python3` + Caused by: Querying Python at `[BIN]/python3` failed with exit status exit status: 1 + + [stderr] + error: intentionally broken python executable + "### + ); + + Ok(()) +} + /// Resolve a specific version of Black at Python 3.12 without deps. #[test] fn compile_python_312_no_deps() -> Result<()> {