Skip to content

Commit

Permalink
Remove VIRTUAL_ENV from project commmands by default (#6976)
Browse files Browse the repository at this point in the history
And use test context helpers for commands consistently.

Needed for #6864
  • Loading branch information
zanieb committed Sep 3, 2024
1 parent 1234b6d commit 1e89d3e
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 117 deletions.
94 changes: 63 additions & 31 deletions crates/uv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl TestContext {
/// Create a uv command for testing.
pub fn command(&self) -> Command {
let mut command = Command::new(get_bin());
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

Expand All @@ -403,18 +403,21 @@ impl TestContext {
/// * Hide other Python python with `UV_PYTHON_INSTALL_DIR` and installed interpreters with
/// `UV_TEST_PYTHON_PATH`.
/// * Increase the stack size to avoid stack overflows on windows due to large async functions.
pub fn add_shared_args(&self, command: &mut Command) {
pub fn add_shared_args(&self, command: &mut Command, activate_venv: bool) {
command
.arg("--cache-dir")
.arg(self.cache_dir.path())
.env("VIRTUAL_ENV", self.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.env("HOME", self.home_dir.as_os_str())
.env("UV_PYTHON_INSTALL_DIR", "")
.env("UV_TEST_PYTHON_PATH", self.python_path())
.env("UV_EXCLUDE_NEWER", EXCLUDE_NEWER)
.current_dir(self.temp_dir.path());

if activate_venv {
command.env("VIRTUAL_ENV", self.venv.as_os_str());
}

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
Expand All @@ -426,48 +429,77 @@ impl TestContext {
pub fn pip_compile(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("compile");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

/// Create a `pip compile` command for testing.
pub fn pip_sync(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("sync");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

pub fn pip_show(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("show");
self.add_shared_args(&mut command, true);
command
}

/// Create a `pip freeze` command with options shared across scenarios.
pub fn pip_freeze(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("freeze");
self.add_shared_args(&mut command, true);
command
}

/// Create a `pip check` command with options shared across scenarios.
pub fn pip_check(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("check");
self.add_shared_args(&mut command, true);
command
}

pub fn pip_list(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("list");
self.add_shared_args(&mut command, true);
command
}

/// Create a `uv venv` command
pub fn venv(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("venv");
self.add_shared_args(&mut command);
command.env_remove("VIRTUAL_ENV");
self.add_shared_args(&mut command, false);
command
}

/// Create a `pip install` command with options shared across scenarios.
pub fn pip_install(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("install");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

/// Create a `pip uninstall` command with options shared across scenarios.
pub fn pip_uninstall(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("uninstall");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

/// Create a `pip tree` command for testing.
pub fn pip_tree(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("tree");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

Expand All @@ -490,31 +522,31 @@ impl TestContext {
pub fn init(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("init");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv sync` command with options shared across scenarios.
pub fn sync(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("sync");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv lock` command with options shared across scenarios.
pub fn lock(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("lock");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv export` command with options shared across scenarios.
pub fn export(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("export");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

Expand All @@ -527,7 +559,7 @@ impl TestContext {
.env("UV_PREVIEW", "1")
.env("UV_PYTHON_INSTALL_DIR", "")
.current_dir(&self.temp_dir);
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

Expand All @@ -540,23 +572,23 @@ impl TestContext {
.env("UV_PREVIEW", "1")
.env("UV_PYTHON_INSTALL_DIR", "")
.current_dir(&self.temp_dir);
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

/// Create a `uv python dir` command with options shared across scenarios.
pub fn python_dir(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("python").arg("dir");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

/// Create a `uv run` command with options shared across scenarios.
pub fn run(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("run").env("UV_SHOW_RESOLUTION", "1");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, true);
command
}

Expand All @@ -567,23 +599,23 @@ impl TestContext {
.arg("tool")
.arg("run")
.env("UV_SHOW_RESOLUTION", "1");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv upgrade run` command with options shared across scenarios.
pub fn tool_upgrade(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("upgrade");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv tool install` command with options shared across scenarios.
pub fn tool_install(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("install");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command.env("UV_EXCLUDE_NEWER", EXCLUDE_NEWER);
command
}
Expand All @@ -592,79 +624,79 @@ impl TestContext {
pub fn tool_list(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("list");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv tool dir` command with options shared across scenarios.
pub fn tool_dir(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("dir");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv tool uninstall` command with options shared across scenarios.
pub fn tool_uninstall(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("uninstall");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv add` command for the given requirements.
pub fn add(&self, reqs: &[&str]) -> Command {
let mut command = Command::new(get_bin());
command.arg("add").args(reqs);
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv add --no-sync` command for the given requirements.
pub fn add_no_sync(&self, reqs: &[&str]) -> Command {
let mut command = Command::new(get_bin());
command.arg("add").arg("--no-sync").args(reqs);
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv remove` command for the given requirements.
pub fn remove(&self, reqs: &[&str]) -> Command {
let mut command = Command::new(get_bin());
command.arg("remove").args(reqs);
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv remove --no-sync` command for the given requirements.
pub fn remove_no_sync(&self, reqs: &[&str]) -> Command {
let mut command = Command::new(get_bin());
command.arg("remove").arg("--no-sync").args(reqs);
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv tree` command with options shared across scenarios.
pub fn tree(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tree");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv cache clean` command.
pub fn clean(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("cache").arg("clean");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

/// Create a `uv cache prune` command.
pub fn prune(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("cache").arg("prune");
self.add_shared_args(&mut command);
self.add_shared_args(&mut command, false);
command
}

Expand Down
18 changes: 4 additions & 14 deletions crates/uv/tests/pip_check.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
use std::process::Command;

use anyhow::Result;
use assert_fs::fixture::FileWriteStr;
use assert_fs::fixture::PathChild;

use common::uv_snapshot;

use crate::common::{get_bin, TestContext};
use crate::common::TestContext;

mod common;

/// Create a `pip check` command with options shared across scenarios.
fn check_command(context: &TestContext) -> Command {
let mut command = Command::new(get_bin());
command.arg("pip").arg("check");
context.add_shared_args(&mut command);
command
}

#[test]
fn check_compatible_packages() -> Result<()> {
let context = TestContext::new("3.12");
Expand Down Expand Up @@ -46,7 +36,7 @@ fn check_compatible_packages() -> Result<()> {
"###
);

uv_snapshot!(check_command(&context), @r###"
uv_snapshot!(context.pip_check(), @r###"
success: true
exit_code: 0
----- stdout -----
Expand Down Expand Up @@ -113,7 +103,7 @@ fn check_incompatible_packages() -> Result<()> {
"###
);

uv_snapshot!(check_command(&context), @r###"
uv_snapshot!(context.pip_check(), @r###"
success: false
exit_code: 1
----- stdout -----
Expand Down Expand Up @@ -185,7 +175,7 @@ fn check_multiple_incompatible_packages() -> Result<()> {
"###
);

uv_snapshot!(check_command(&context), @r###"
uv_snapshot!(context.pip_check(), @r###"
success: false
exit_code: 1
----- stdout -----
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/pip_compile_scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn command(context: &TestContext, python_versions: &[&str]) -> Command {
.arg(packse_index_url())
.arg("--find-links")
.arg(build_vendor_links_url());
context.add_shared_args(&mut command);
context.add_shared_args(&mut command, true);
command.env_remove("UV_EXCLUDE_NEWER");
command.env("UV_TEST_PYTHON_PATH", python_path);

Expand Down
Loading

0 comments on commit 1e89d3e

Please sign in to comment.