Skip to content

Commit

Permalink
Update test context to avoid discovery of external Pythons (#4222)
Browse files Browse the repository at this point in the history
By setting the test search path to an empty path, we avoid accidentally
pulling interpreters from the system during a test case.

Cherry-picked from #4214
  • Loading branch information
zanieb authored Jun 10, 2024
1 parent 98d1ea6 commit 546e23b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
17 changes: 12 additions & 5 deletions crates/uv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub const INSTA_FILTERS: &[(&str, &str)] = &[
),
];

/// Create a context for tests which simplfiies shared behavior across tests.
///
/// * Set the current directory to a temporary directory (`temp_dir`).
/// * Set the cache dir to a different temporary directory (`cache_dir`).
/// * Set a cutoff for versions used in the resolution so the snapshots don't change after a new release.
/// * Set the venv to a fresh `.venv` in `temp_dir`.
#[derive(Debug)]
pub struct TestContext {
pub temp_dir: assert_fs::TempDir,
Expand Down Expand Up @@ -153,11 +159,7 @@ impl TestContext {
}
}

/// Set shared defaults between tests:
/// * Set the current directory to a temporary directory (`temp_dir`).
/// * Set the cache dir to a different temporary directory (`cache_dir`).
/// * Set a cutoff for versions used in the resolution so the snapshots don't change after a new release.
/// * Set the venv to a fresh `.venv` in `temp_dir`.
/// Create a `pip compile` command for testing.
pub fn compile(&self) -> std::process::Command {
let mut command = self.compile_without_exclude_newer();
command.arg("--exclude-newer").arg(EXCLUDE_NEWER);
Expand All @@ -178,6 +180,7 @@ impl TestContext {
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", "/dev/null")
.current_dir(self.temp_dir.path());

if cfg!(all(windows, debug_assertions)) {
Expand Down Expand Up @@ -211,6 +214,7 @@ impl TestContext {
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", "/dev/null")
.current_dir(&self.temp_dir);

if cfg!(all(windows, debug_assertions)) {
Expand All @@ -231,6 +235,7 @@ impl TestContext {
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", "/dev/null")
.current_dir(&self.temp_dir);

if cfg!(all(windows, debug_assertions)) {
Expand Down Expand Up @@ -263,6 +268,7 @@ impl TestContext {
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", "/dev/null")
.current_dir(&self.temp_dir);

if cfg!(all(windows, debug_assertions)) {
Expand Down Expand Up @@ -295,6 +301,7 @@ impl TestContext {
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", "/dev/null")
.current_dir(&self.temp_dir);

if cfg!(all(windows, debug_assertions)) {
Expand Down
21 changes: 14 additions & 7 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ use anyhow::Result;
use assert_fs::prelude::*;
use indoc::indoc;

use crate::common::get_toolchain;
use common::{uv_snapshot, TestContext};
use common::{python_path_with_versions, uv_snapshot, TestContext};

mod common;

/// Run with different python versions, which also depend on different dependency versions.
#[test]
fn run_with_python_version() -> Result<()> {
let context = TestContext::new("3.12");
let python_path = python_path_with_versions(&context.temp_dir, &["3.11", "3.12"])
.expect("Failed to create Python test path");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! { r#"
Expand Down Expand Up @@ -43,7 +44,8 @@ fn run_with_python_version() -> Result<()> {
.arg("--preview")
.arg("python")
.arg("-B")
.arg("main.py");
.arg("main.py")
.env("UV_TEST_PYTHON_PATH", &python_path);
uv_snapshot!(context.filters(), command_with_args, @r###"
success: true
exit_code: 0
Expand All @@ -66,10 +68,11 @@ fn run_with_python_version() -> Result<()> {
let command_with_args = command
.arg("--preview")
.arg("-p")
.arg(get_toolchain("3.12"))
.arg("3.12")
.arg("python")
.arg("-B")
.arg("main.py");
.arg("main.py")
.env("UV_TEST_PYTHON_PATH", &python_path);
uv_snapshot!(context.filters(), command_with_args, @r###"
success: true
exit_code: 0
Expand All @@ -87,16 +90,20 @@ fn run_with_python_version() -> Result<()> {
let command_with_args = command
.arg("--preview")
.arg("-p")
.arg(get_toolchain("3.11"))
.arg("3.11")
.arg("python")
.arg("-B")
.arg("main.py");
.arg("main.py")
.env("UV_TEST_PYTHON_PATH", &python_path)
.env_remove("VIRTUAL_ENV");

let mut filters = context.filters();
filters.push((
r"Using Python 3.11.\d+ interpreter at: .*",
"Using Python 3.11.[X] interpreter at: [PYTHON]",
));
filters.push((r"3.11.\d+", "3.11.[X]"));

uv_snapshot!(filters, command_with_args, @r###"
success: true
exit_code: 0
Expand Down

0 comments on commit 546e23b

Please sign in to comment.