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.
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)] = &[
(r"--cache-dir [^\s]+", "--cache-dir [CACHE_DIR]"),
// Operation times
@ -157,6 +158,38 @@ impl TestContext {
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.
pub fn assert_command(&self, command: &str) -> Assert {
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.
///
/// 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>,
filters: impl AsRef<[(&'a str, &'a str)]>,
filters: impl AsRef<[(T, T)]>,
windows_filters: bool,
) -> (String, Output) {
let program = command
@ -411,9 +444,9 @@ pub fn run_and_format<'a>(
for (matcher, replacement) in filters.as_ref() {
// 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) {
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 uv_fs::Simplified;
use crate::common::{get_bin, EXCLUDE_NEWER};
use crate::common::get_bin;
mod common;
@ -3034,7 +3034,7 @@ fn recursive_extras_direct_url() -> Result<()> {
let requirements_in = context.temp_dir.child("requirements.in");
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)) {
// TODO(konstin): Reduce stack usage in debug mode enough that the tests pass with the
// default windows stack of 1MB
@ -3042,19 +3042,13 @@ fn recursive_extras_direct_url() -> Result<()> {
}
uv_snapshot!(context.filters(), command
.arg("pip")
.arg("compile")
.arg(requirements_in.path())
.arg("--cache-dir")
.arg(context.cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("VIRTUAL_ENV", context.venv.as_os_str()), @r###"
.arg(requirements_in.path())
.current_dir(current_dir().unwrap()), @r###"
success: true
exit_code: 0
----- stdout -----
# 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
# via black
aiosignal==1.3.1
@ -6450,21 +6444,8 @@ fn local_version_of_remote_package() -> Result<()> {
"###);
// Actually install the local dependency
// TODO(zanieb): We should have an `install` utility on the context instead of doing this
let mut command = Command::new(get_bin());
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());
}
let mut command = context.install();
command.arg(root_path.join("anyio_local"));
uv_snapshot!(
context.filters(),
command, @r###"

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,13 @@
#![cfg(feature = "python")]
use std::ffi::OsString;
use std::process::Command;
use anyhow::Result;
use assert_cmd::prelude::*;
use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*;
use fs_err::PathExt;
use uv_fs::Simplified;
use crate::common::{
@ -14,214 +16,166 @@ use crate::common::{
mod common;
struct VenvTestContext {
cache_dir: assert_fs::TempDir,
temp_dir: assert_fs::TempDir,
venv: ChildPath,
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");
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() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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");
fn create_venv() {
let context = VenvTestContext::new(&["3.12"]);
// Create a virtual environment at `.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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.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), @r###"
.arg("3.12"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
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.
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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
"###
);
venv.assert(predicates::path::is_dir());
Ok(())
context.venv.assert(predicates::path::is_dir());
}
#[test]
fn create_venv_defaults_to_cwd() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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 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")
fn create_venv_defaults_to_cwd() {
let context = VenvTestContext::new(&["3.12"]);
uv_snapshot!(context.filters(), context.venv_command()
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
"###
);
venv.assert(predicates::path::is_dir());
Ok(())
context.venv.assert(predicates::path::is_dir());
}
#[test]
fn seed() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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 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())
fn seed() {
let context = VenvTestContext::new(&["3.12"]);
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--seed")
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
+ pip==24.0
Activate with: source .venv/bin/activate
"###
);
venv.assert(predicates::path::is_dir());
Ok(())
context.venv.assert(predicates::path::is_dir());
}
#[test]
fn seed_older_python_version() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let cache_dir = assert_fs::TempDir::new()?;
let bin = create_bin_with_executables(&temp_dir, &["3.10"]).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_prompt, "Activate with: source .venv/bin/activate"),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
fn seed_older_python_version() {
let context = VenvTestContext::new(&["3.10"]);
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--seed")
.arg("--python")
.arg("3.10")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.10.13 interpreter at: [PATH]
Creating virtualenv at: .venv
+ pip==24.0
+ setuptools==69.2.0
@ -230,31 +184,19 @@ fn seed_older_python_version() -> Result<()> {
"###
);
venv.assert(predicates::path::is_dir());
Ok(())
context.venv.assert(predicates::path::is_dir());
}
#[test]
fn create_venv_unknown_python_minor() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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");
fn create_venv_unknown_python_minor() {
let context = VenvTestContext::new(&["3.12"]);
let mut command = Command::new(get_bin());
let mut command = context.venv_command();
command
.arg("venv")
.arg(venv.as_os_str())
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.15")
.arg("--cache-dir")
.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);
.env("UV_NO_WRAP", "1");
if cfg!(windows) {
uv_snapshot!(&mut command, @r###"
success: false
@ -277,19 +219,13 @@ fn create_venv_unknown_python_minor() -> Result<()> {
);
}
venv.assert(predicates::path::missing());
Ok(())
context.venv.assert(predicates::path::missing());
}
#[test]
fn create_venv_unknown_python_patch() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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");
fn create_venv_unknown_python_patch() {
let context = VenvTestContext::new(&["3.12"]);
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filters = &[
(
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\?",
"No Python 3.8.0 in `PATH`. Is Python 3.8.0 installed?",
),
(&filter_venv, ".venv"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
uv_snapshot!(filters, context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.8.0")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: false
exit_code: 1
----- stdout -----
@ -322,38 +250,18 @@ fn create_venv_unknown_python_patch() -> Result<()> {
"###
);
venv.assert(predicates::path::missing());
Ok(())
context.venv.assert(predicates::path::missing());
}
#[test]
fn create_venv_python_patch() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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");
fn create_venv_python_patch() {
let context = VenvTestContext::new(&["3.12.1"]);
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.12.1")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: true
exit_code: 0
----- stdout -----
@ -365,47 +273,27 @@ fn create_venv_python_patch() -> Result<()> {
"###
);
venv.assert(predicates::path::is_dir());
Ok(())
context.venv.assert(predicates::path::is_dir());
}
#[test]
fn file_exists() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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 context = VenvTestContext::new(&["3.12"]);
// 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());
let filters = &[
(
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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: false
exit_code: 1
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
uv::venv::creation
@ -419,89 +307,50 @@ fn file_exists() -> Result<()> {
#[test]
fn empty_dir_exists() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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 context = VenvTestContext::new(&["3.12"]);
// Create an empty directory at `.venv`. Creating a virtualenv at the same path should succeed.
venv.create_dir_all()?;
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())
context.venv.create_dir_all()?;
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
"###
);
venv.assert(predicates::path::is_dir());
context.venv.assert(predicates::path::is_dir());
Ok(())
}
#[test]
fn non_empty_dir_exists() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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 context = VenvTestContext::new(&["3.12"]);
// Create a non-empty directory at `.venv`. Creating a virtualenv at the same path should fail.
venv.create_dir_all()?;
venv.child("file").touch()?;
context.venv.create_dir_all()?;
context.venv.child("file").touch()?;
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filters = &[
(
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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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###"
.env("UV_NO_WRAP", "1"), @r###"
success: false
exit_code: 1
----- stdout -----
----- stderr -----
Using Python [VERSION] interpreter at: [PATH]
Using Python 3.12.1 interpreter at: [PATH]
Creating virtualenv at: .venv
uv::venv::creation
@ -516,14 +365,10 @@ fn non_empty_dir_exists() -> Result<()> {
#[test]
#[cfg(windows)]
fn windows_shims() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let cache_dir = assert_fs::TempDir::new()?;
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 context = VenvTestContext::new(&["3.9", "3.8"]);
let shim_path = context.temp_dir.child("shim");
let py38 = std::env::split_paths(&bin)
let py38 = std::env::split_paths(&context.bin)
.last()
.expect("create_bin_with_executables to set up the python versions");
// 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.
let filter_venv = regex::escape(&venv.simplified_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--clear")
.arg("--cache-dir")
.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###"
.env("UV_TEST_PYTHON_PATH", format!("{};{}", shim_path.display(), context.bin.simplified_display())), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
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
Activate with: source .venv/bin/activate
"###
);
venv.assert(predicates::path::is_dir());
context.venv.assert(predicates::path::is_dir());
Ok(())
}
#[test]
fn virtualenv_compatibility() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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");
fn virtualenv_compatibility() {
let context = VenvTestContext::new(&["3.12"]);
// Create a virtual environment at `.venv`, passing the redundant `--clear` flag.
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())
uv_snapshot!(context.filters(), context.venv_command()
.arg(context.venv.as_os_str())
.arg("--clear")
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.arg(cache_dir.path())
.arg("--exclude-newer")
.arg(EXCLUDE_NEWER)
.env("UV_TEST_PYTHON_PATH", bin)
.current_dir(&temp_dir), @r###"
.arg("3.12"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
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
Activate with: source .venv/bin/activate
"###
);
venv.assert(predicates::path::is_dir());
Ok(())
context.venv.assert(predicates::path::is_dir());
}
#[test]
@ -644,27 +451,18 @@ fn verify_pyvenv_cfg() {
/// Ensure that a nested virtual environment uses the same `home` directory as the parent.
#[test]
fn verify_nested_pyvenv_cfg() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
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 context = VenvTestContext::new(&["3.12"]);
// Create a virtual environment at `.venv`.
Command::new(get_bin())
.arg("venv")
.arg(venv.as_os_str())
context
.venv_command()
.arg(context.venv.as_os_str())
.arg("--python")
.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()
.success();
let pyvenv_cfg = venv.child("pyvenv.cfg");
let pyvenv_cfg = context.venv.child("pyvenv.cfg");
// Check pyvenv.cfg exists
pyvenv_cfg.assert(predicates::path::is_file());
@ -677,19 +475,13 @@ fn verify_nested_pyvenv_cfg() -> Result<()> {
.expect("home line not found");
// Now, create a virtual environment from within the virtual environment.
let subvenv = temp_dir.child(".subvenv");
Command::new(get_bin())
.arg("venv")
let subvenv = context.temp_dir.child(".subvenv");
context
.venv_command()
.arg(subvenv.as_os_str())
.arg("--python")
.arg("3.12")
.arg("--cache-dir")
.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)
.env("VIRTUAL_ENV", context.venv.as_os_str())
.assert()
.success();