Don't use exclude newer manually in test functions (#2697)

With this change, all usages of `EXCLUDE_NEWER` are now in command
wrappers, not in the test functions themselves.

For the venv test, i refactored them into the same kind of test context
abstraction that the other test modules have in the second commit.

The third commit makes`"INSTA_FILTERS` "private", removing the last
remaining individual usage.

Pending windows CI 🤞
This commit is contained in:
konsti 2024-04-01 16:07:25 +02:00 committed by GitHub
parent 9df6e33b85
commit 05ed7ac64b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 338 additions and 562 deletions

View file

@ -23,6 +23,7 @@ use uv_interpreter::find_requested_python;
// Exclude any packages uploaded after this date. // Exclude any packages uploaded after this date.
pub static EXCLUDE_NEWER: &str = "2024-03-25T00:00:00Z"; pub static EXCLUDE_NEWER: &str = "2024-03-25T00:00:00Z";
#[doc(hidden)] // Macro and test context only, don't use directly.
pub const INSTA_FILTERS: &[(&str, &str)] = &[ pub const INSTA_FILTERS: &[(&str, &str)] = &[
(r"--cache-dir [^\s]+", "--cache-dir [CACHE_DIR]"), (r"--cache-dir [^\s]+", "--cache-dir [CACHE_DIR]"),
// Operation times // Operation times
@ -157,6 +158,38 @@ impl TestContext {
cmd cmd
} }
/// Create a `pip install` command with options shared across scenarios.
pub fn install(&self) -> std::process::Command {
let mut command = self.install_without_exclude_newer();
command.arg("--exclude-newer").arg(EXCLUDE_NEWER);
command
}
/// Create a `pip install` command with no `--exclude-newer` option.
///
/// One should avoid using this in tests to the extent possible because
/// it can result in tests failing when the index state changes. Therefore,
/// if you use this, there should be some other kind of mitigation in place.
/// For example, pinning package versions.
pub fn install_without_exclude_newer(&self) -> std::process::Command {
let mut command = std::process::Command::new(get_bin());
command
.arg("pip")
.arg("install")
.arg("--cache-dir")
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.current_dir(&self.temp_dir);
if cfg!(all(windows, debug_assertions)) {
// TODO(konstin): Reduce stack usage in debug mode enough that the tests pass with the
// default windows stack of 1MB
command.env("UV_STACK_SIZE", (4 * 1024 * 1024).to_string());
}
command
}
/// Run the given python code and check whether it succeeds. /// Run the given python code and check whether it succeeds.
pub fn assert_command(&self, command: &str) -> Assert { pub fn assert_command(&self, command: &str) -> Assert {
std::process::Command::new(venv_to_interpreter(&self.venv)) std::process::Command::new(venv_to_interpreter(&self.venv))
@ -386,9 +419,9 @@ pub fn create_bin_with_executables(
/// Execute the command and format its output status, stdout and stderr into a snapshot string. /// Execute the command and format its output status, stdout and stderr into a snapshot string.
/// ///
/// This function is derived from `insta_cmd`s `spawn_with_info`. /// This function is derived from `insta_cmd`s `spawn_with_info`.
pub fn run_and_format<'a>( pub fn run_and_format<T: AsRef<str>>(
mut command: impl BorrowMut<std::process::Command>, mut command: impl BorrowMut<std::process::Command>,
filters: impl AsRef<[(&'a str, &'a str)]>, filters: impl AsRef<[(T, T)]>,
windows_filters: bool, windows_filters: bool,
) -> (String, Output) { ) -> (String, Output) {
let program = command let program = command
@ -411,9 +444,9 @@ pub fn run_and_format<'a>(
for (matcher, replacement) in filters.as_ref() { for (matcher, replacement) in filters.as_ref() {
// TODO(konstin): Cache regex compilation // TODO(konstin): Cache regex compilation
let re = Regex::new(matcher).expect("Do you need to regex::escape your filter?"); let re = Regex::new(matcher.as_ref()).expect("Do you need to regex::escape your filter?");
if re.is_match(&snapshot) { if re.is_match(&snapshot) {
snapshot = re.replace_all(&snapshot, *replacement).to_string(); snapshot = re.replace_all(&snapshot, replacement.as_ref()).to_string();
} }
} }

View file

@ -15,7 +15,7 @@ use url::Url;
use common::{uv_snapshot, TestContext}; use common::{uv_snapshot, TestContext};
use uv_fs::Simplified; use uv_fs::Simplified;
use crate::common::{get_bin, EXCLUDE_NEWER}; use crate::common::get_bin;
mod common; mod common;
@ -3034,7 +3034,7 @@ fn recursive_extras_direct_url() -> Result<()> {
let requirements_in = context.temp_dir.child("requirements.in"); let requirements_in = context.temp_dir.child("requirements.in");
requirements_in.write_str("black[dev] @ ../../scripts/packages/black_editable")?; requirements_in.write_str("black[dev] @ ../../scripts/packages/black_editable")?;
let mut command = Command::new(get_bin()); let mut command = context.compile();
if cfg!(all(windows, debug_assertions)) { if cfg!(all(windows, debug_assertions)) {
// TODO(konstin): Reduce stack usage in debug mode enough that the tests pass with the // TODO(konstin): Reduce stack usage in debug mode enough that the tests pass with the
// default windows stack of 1MB // default windows stack of 1MB
@ -3042,19 +3042,13 @@ fn recursive_extras_direct_url() -> Result<()> {
} }
uv_snapshot!(context.filters(), command uv_snapshot!(context.filters(), command
.arg("pip")
.arg("compile")
.arg(requirements_in.path()) .arg(requirements_in.path())
.arg("--cache-dir") .current_dir(current_dir().unwrap()), @r###"
.arg(context.cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("VIRTUAL_ENV", context.venv.as_os_str()), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
# This file was autogenerated by uv via the following command: # This file was autogenerated by uv via the following command:
# uv pip compile [TEMP_DIR]/requirements.in --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z # uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2024-03-25T00:00:00Z [TEMP_DIR]/requirements.in
aiohttp==3.9.3 aiohttp==3.9.3
# via black # via black
aiosignal==1.3.1 aiosignal==1.3.1
@ -6450,21 +6444,8 @@ fn local_version_of_remote_package() -> Result<()> {
"###); "###);
// Actually install the local dependency // Actually install the local dependency
// TODO(zanieb): We should have an `install` utility on the context instead of doing this let mut command = context.install();
let mut command = Command::new(get_bin()); command.arg(root_path.join("anyio_local"));
command
.arg("pip")
.arg("install")
.arg(root_path.join("anyio_local"))
.arg("--cache-dir")
.arg(context.cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("VIRTUAL_ENV", context.venv.as_os_str())
.current_dir(context.temp_dir.path());
if cfg!(all(windows, debug_assertions)) {
command.env("UV_STACK_SIZE", (8 * 1024 * 1024).to_string());
}
uv_snapshot!( uv_snapshot!(
context.filters(), context.filters(),
command, @r###" command, @r###"

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,13 @@
#![cfg(feature = "python")] #![cfg(feature = "python")]
use std::ffi::OsString;
use std::process::Command; use std::process::Command;
use anyhow::Result; use anyhow::Result;
use assert_cmd::prelude::*; use assert_cmd::prelude::*;
use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*; use assert_fs::prelude::*;
use fs_err::PathExt;
use uv_fs::Simplified; use uv_fs::Simplified;
use crate::common::{ use crate::common::{
@ -14,214 +16,166 @@ use crate::common::{
mod common; mod common;
#[test] struct VenvTestContext {
fn create_venv() -> Result<()> { cache_dir: assert_fs::TempDir,
let temp_dir = assert_fs::TempDir::new()?; temp_dir: assert_fs::TempDir,
let cache_dir = assert_fs::TempDir::new()?; venv: ChildPath,
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir"); bin: OsString,
}
impl VenvTestContext {
fn new(python_versions: &[&str]) -> Self {
let temp_dir = assert_fs::TempDir::new().unwrap();
let bin = create_bin_with_executables(&temp_dir, python_versions)
.expect("Failed to create bin dir");
let venv = temp_dir.child(".venv"); let venv = temp_dir.child(".venv");
Self {
cache_dir: assert_fs::TempDir::new().unwrap(),
temp_dir,
venv,
bin,
}
}
fn venv_command(&self) -> Command {
let mut command = Command::new(get_bin());
command
.arg("venv")
.arg("--cache-dir")
.arg(self.cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_TEST_PYTHON_PATH", self.bin.clone())
.current_dir(self.temp_dir.path());
command
}
fn filters(&self) -> Vec<(String, String)> {
// On windows, a directory can have multiple names (https://superuser.com/a/1666770), e.g.
// `C:\Users\KONSTA~1` and `C:\Users\Konstantin` are the same.
let venv_full = regex::escape(&self.venv.display().to_string());
let mut filters = vec![(venv_full, ".venv".to_string())];
// For mac, otherwise it shows some /var/folders/ path.
if let Ok(canonicalized) = self.venv.path().fs_err_canonicalize() {
let venv_full = regex::escape(&canonicalized.simplified_display().to_string());
filters.push((venv_full, ".venv".to_string()));
}
filters.push((
r"interpreter at: .+".to_string(),
"interpreter at: [PATH]".to_string(),
));
filters.push((
r"Activate with: (?:.*)\\Scripts\\activate".to_string(),
"Activate with: source .venv/bin/activate".to_string(),
));
filters
}
}
#[test]
fn create_venv() {
let context = VenvTestContext::new(&["3.12"]);
// Create a virtual environment at `.venv`. // Create a virtual environment at `.venv`.
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate"; .arg(context.venv.as_os_str())
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12"), @r###"
.arg("--cache-dir")
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_TEST_PYTHON_PATH", bin.clone())
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
// Create a virtual environment at the same location, which should replace it. // Create a virtual environment at the same location, which should replace it.
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate"; .arg(context.venv.as_os_str())
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(())
} }
#[test] #[test]
fn create_venv_defaults_to_cwd() -> Result<()> { fn create_venv_defaults_to_cwd() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?; uv_snapshot!(context.filters(), context.venv_command()
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(&filter_venv, ".venv"),
(filter_prompt, "Activate with: source .venv/bin/activate"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(())
} }
#[test] #[test]
fn seed() -> Result<()> { fn seed() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?; uv_snapshot!(context.filters(), context.venv_command()
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir"); .arg(context.venv.as_os_str())
let venv = temp_dir.child(".venv");
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--seed") .arg("--seed")
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
+ pip==24.0 + pip==24.0
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(())
} }
#[test] #[test]
fn seed_older_python_version() -> Result<()> { fn seed_older_python_version() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.10"]);
let cache_dir = assert_fs::TempDir::new()?; uv_snapshot!(context.filters(), context.venv_command()
let bin = create_bin_with_executables(&temp_dir, &["3.10"]).expect("Failed to create bin dir"); .arg(context.venv.as_os_str())
let venv = temp_dir.child(".venv");
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--seed") .arg("--seed")
.arg("--python") .arg("--python")
.arg("3.10") .arg("3.10")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.10.13 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
+ pip==24.0 + pip==24.0
+ setuptools==69.2.0 + setuptools==69.2.0
@ -230,31 +184,19 @@ fn seed_older_python_version() -> Result<()> {
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(())
} }
#[test] #[test]
fn create_venv_unknown_python_minor() -> Result<()> { fn create_venv_unknown_python_minor() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
let mut command = Command::new(get_bin()); let mut command = context.venv_command();
command command
.arg("venv") .arg(context.venv.as_os_str())
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.15") .arg("3.15")
.arg("--cache-dir") .env("UV_NO_WRAP", "1");
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir);
if cfg!(windows) { if cfg!(windows) {
uv_snapshot!(&mut command, @r###" uv_snapshot!(&mut command, @r###"
success: false success: false
@ -277,19 +219,13 @@ fn create_venv_unknown_python_minor() -> Result<()> {
); );
} }
venv.assert(predicates::path::missing()); context.venv.assert(predicates::path::missing());
Ok(())
} }
#[test] #[test]
fn create_venv_unknown_python_patch() -> Result<()> { fn create_venv_unknown_python_patch() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filters = &[ let filters = &[
( (
r"Using Python 3\.\d+\.\d+ interpreter at: .+", r"Using Python 3\.\d+\.\d+ interpreter at: .+",
@ -299,20 +235,12 @@ fn create_venv_unknown_python_patch() -> Result<()> {
r"No Python 3\.8\.0 found through `py --list-paths` or in `PATH`\. Is Python 3\.8\.0 installed\?", r"No Python 3\.8\.0 found through `py --list-paths` or in `PATH`\. Is Python 3\.8\.0 installed\?",
"No Python 3.8.0 in `PATH`. Is Python 3.8.0 installed?", "No Python 3.8.0 in `PATH`. Is Python 3.8.0 installed?",
), ),
(&filter_venv, ".venv"),
]; ];
uv_snapshot!(filters, Command::new(get_bin()) uv_snapshot!(filters, context.venv_command()
.arg("venv") .arg(context.venv.as_os_str())
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.8.0") .arg("3.8.0")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
@ -322,38 +250,18 @@ fn create_venv_unknown_python_patch() -> Result<()> {
"### "###
); );
venv.assert(predicates::path::missing()); context.venv.assert(predicates::path::missing());
Ok(())
} }
#[test] #[test]
fn create_venv_python_patch() -> Result<()> { fn create_venv_python_patch() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12.1"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin =
create_bin_with_executables(&temp_dir, &["3.12.1"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate"; .arg(context.venv.as_os_str())
let filters = &[
(r"interpreter at: .+", "interpreter at: [PATH]"),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12.1") .arg("3.12.1")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -365,47 +273,27 @@ fn create_venv_python_patch() -> Result<()> {
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(())
} }
#[test] #[test]
fn file_exists() -> Result<()> { fn file_exists() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
// Create a file at `.venv`. Creating a virtualenv at the same path should fail. // Create a file at `.venv`. Creating a virtualenv at the same path should fail.
venv.touch()?; context.venv.touch()?;
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filters = &[ .arg(context.venv.as_os_str())
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
uv::venv::creation uv::venv::creation
@ -419,89 +307,50 @@ fn file_exists() -> Result<()> {
#[test] #[test]
fn empty_dir_exists() -> Result<()> { fn empty_dir_exists() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
// Create an empty directory at `.venv`. Creating a virtualenv at the same path should succeed. // Create an empty directory at `.venv`. Creating a virtualenv at the same path should succeed.
venv.create_dir_all()?; context.venv.create_dir_all()?;
uv_snapshot!(context.filters(), context.venv_command()
let filter_venv = regex::escape(&venv.simplified_display().to_string()); .arg(context.venv.as_os_str())
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(()) Ok(())
} }
#[test] #[test]
fn non_empty_dir_exists() -> Result<()> { fn non_empty_dir_exists() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
// Create a non-empty directory at `.venv`. Creating a virtualenv at the same path should fail. // Create a non-empty directory at `.venv`. Creating a virtualenv at the same path should fail.
venv.create_dir_all()?; context.venv.create_dir_all()?;
venv.child("file").touch()?; context.venv.child("file").touch()?;
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filters = &[ .arg(context.venv.as_os_str())
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("UV_NO_WRAP", "1"), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_NO_WRAP", "1")
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
uv::venv::creation uv::venv::creation
@ -516,14 +365,10 @@ fn non_empty_dir_exists() -> Result<()> {
#[test] #[test]
#[cfg(windows)] #[cfg(windows)]
fn windows_shims() -> Result<()> { fn windows_shims() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.9", "3.8"]);
let cache_dir = assert_fs::TempDir::new()?; let shim_path = context.temp_dir.child("shim");
let bin =
create_bin_with_executables(&temp_dir, &["3.9", "3.8"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
let shim_path = temp_dir.child("shim");
let py38 = std::env::split_paths(&bin) let py38 = std::env::split_paths(&context.bin)
.last() .last()
.expect("create_bin_with_executables to set up the python versions"); .expect("create_bin_with_executables to set up the python versions");
// We want 3.8 and the first version should be 3.9. // We want 3.8 and the first version should be 3.9.
@ -540,88 +385,50 @@ fn windows_shims() -> Result<()> {
)?; )?;
// Create a virtual environment at `.venv`, passing the redundant `--clear` flag. // Create a virtual environment at `.venv`, passing the redundant `--clear` flag.
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate"; .arg(context.venv.as_os_str())
let filters = &[
(
r"Using Python 3\.8.\d+ interpreter at: .+",
"Using Python 3.8.x interpreter at: [PATH]",
),
(&filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--clear") .arg("--clear")
.arg("--cache-dir") .env("UV_TEST_PYTHON_PATH", format!("{};{}", shim_path.display(), context.bin.simplified_display())), @r###"
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_TEST_PYTHON_PATH", format!("{};{}", shim_path.display(), bin.simplified_display()))
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
warning: virtualenv's `--clear` has no effect (uv always clears the virtual environment). warning: virtualenv's `--clear` has no effect (uv always clears the virtual environment).
Using Python 3.8.x interpreter at: [PATH] Using Python 3.8.12 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(()) Ok(())
} }
#[test] #[test]
fn virtualenv_compatibility() -> Result<()> { fn virtualenv_compatibility() {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
// Create a virtual environment at `.venv`, passing the redundant `--clear` flag. // Create a virtual environment at `.venv`, passing the redundant `--clear` flag.
let filter_venv = regex::escape(&venv.simplified_display().to_string()); uv_snapshot!(context.filters(), context.venv_command()
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate"; .arg(context.venv.as_os_str())
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at: .+",
"Using Python [VERSION] interpreter at: [PATH]",
),
(filter_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
.arg("--clear") .arg("--clear")
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12"), @r###"
.arg("--cache-dir")
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
warning: virtualenv's `--clear` has no effect (uv always clears the virtual environment). warning: virtualenv's `--clear` has no effect (uv always clears the virtual environment).
Using Python [VERSION] interpreter at: [PATH] Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv Creating virtualenv at: .venv
Activate with: source .venv/bin/activate Activate with: source .venv/bin/activate
"### "###
); );
venv.assert(predicates::path::is_dir()); context.venv.assert(predicates::path::is_dir());
Ok(())
} }
#[test] #[test]
@ -644,27 +451,18 @@ fn verify_pyvenv_cfg() {
/// Ensure that a nested virtual environment uses the same `home` directory as the parent. /// Ensure that a nested virtual environment uses the same `home` directory as the parent.
#[test] #[test]
fn verify_nested_pyvenv_cfg() -> Result<()> { fn verify_nested_pyvenv_cfg() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?; let context = VenvTestContext::new(&["3.12"]);
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.12"]).expect("Failed to create bin dir");
let venv = temp_dir.child(".venv");
// Create a virtual environment at `.venv`. // Create a virtual environment at `.venv`.
Command::new(get_bin()) context
.arg("venv") .venv_command()
.arg(venv.as_os_str()) .arg(context.venv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir")
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_TEST_PYTHON_PATH", bin.clone())
.current_dir(&temp_dir)
.assert() .assert()
.success(); .success();
let pyvenv_cfg = venv.child("pyvenv.cfg"); let pyvenv_cfg = context.venv.child("pyvenv.cfg");
// Check pyvenv.cfg exists // Check pyvenv.cfg exists
pyvenv_cfg.assert(predicates::path::is_file()); pyvenv_cfg.assert(predicates::path::is_file());
@ -677,19 +475,13 @@ fn verify_nested_pyvenv_cfg() -> Result<()> {
.expect("home line not found"); .expect("home line not found");
// Now, create a virtual environment from within the virtual environment. // Now, create a virtual environment from within the virtual environment.
let subvenv = temp_dir.child(".subvenv"); let subvenv = context.temp_dir.child(".subvenv");
Command::new(get_bin()) context
.arg("venv") .venv_command()
.arg(subvenv.as_os_str()) .arg(subvenv.as_os_str())
.arg("--python") .arg("--python")
.arg("3.12") .arg("3.12")
.arg("--cache-dir") .env("VIRTUAL_ENV", context.venv.as_os_str())
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("VIRTUAL_ENV", venv.as_os_str())
.env("UV_TEST_PYTHON_PATH", bin.clone())
.current_dir(&temp_dir)
.assert() .assert()
.success(); .success();