Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use empty string? Would it be more portable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried we might treat an empty string as an empty variable but I guess we don't after looking at the source. Since this passes on Windows CI I think I'd prefer to leave it as-is because the intent is clearer? 🤷‍♀️

.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