Add test coverage for use of `git` without feature flag (#10874)

Shoves a broken `git` executable onto the front of the `PATH` in the
test context when the `git` feature is disabled so they fail if they're
missing the feature-gate.
This commit is contained in:
Zanie Blue 2025-01-23 09:55:06 -06:00 committed by GitHub
parent b2dac9979d
commit a0620bcf92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 45 additions and 2 deletions

View File

@ -621,6 +621,7 @@ fn branching_between_registry_and_direct_url() -> Result<()> {
/// ] /// ]
/// ``` /// ```
#[test] #[test]
#[cfg(feature = "git")]
fn branching_urls_of_different_sources_disjoint() -> Result<()> { fn branching_urls_of_different_sources_disjoint() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
@ -703,6 +704,7 @@ fn branching_urls_of_different_sources_disjoint() -> Result<()> {
/// ] /// ]
/// ``` /// ```
#[test] #[test]
#[cfg(feature = "git")]
fn branching_urls_of_different_sources_conflict() -> Result<()> { fn branching_urls_of_different_sources_conflict() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");

View File

@ -89,6 +89,7 @@ pub struct TestContext {
pub cache_dir: ChildPath, pub cache_dir: ChildPath,
pub python_dir: ChildPath, pub python_dir: ChildPath,
pub home_dir: ChildPath, pub home_dir: ChildPath,
pub bin_dir: ChildPath,
pub venv: ChildPath, pub venv: ChildPath,
pub workspace_root: PathBuf, pub workspace_root: PathBuf,
@ -274,6 +275,14 @@ impl TestContext {
let python_dir = ChildPath::new(root.path()).child("python"); let python_dir = ChildPath::new(root.path()).child("python");
fs_err::create_dir_all(&python_dir).expect("Failed to create test Python directory"); fs_err::create_dir_all(&python_dir).expect("Failed to create test Python directory");
let bin_dir = ChildPath::new(root.path()).child("bin");
fs_err::create_dir_all(&bin_dir).expect("Failed to create test bin directory");
// When the `git` feature is disabled, enforce that the test suite does not use `git`
if cfg!(not(feature = "git")) {
Self::disallow_git_cli(&bin_dir).expect("Failed to setup disallowed `git` command");
}
let home_dir = ChildPath::new(root.path()).child("home"); let home_dir = ChildPath::new(root.path()).child("home");
fs_err::create_dir_all(&home_dir).expect("Failed to create test home directory"); fs_err::create_dir_all(&home_dir).expect("Failed to create test home directory");
@ -441,6 +450,7 @@ impl TestContext {
cache_dir, cache_dir,
python_dir, python_dir,
home_dir, home_dir,
bin_dir,
venv, venv,
workspace_root, workspace_root,
python_version, python_version,
@ -457,6 +467,24 @@ impl TestContext {
command command
} }
fn disallow_git_cli(bin_dir: &Path) -> std::io::Result<()> {
let contents = r"#!/bin/sh
echo 'error: `git` operations are not allowed are you missing a cfg for the `git` feature?' >&2
exit 127";
let git = bin_dir.join(format!("git{}", env::consts::EXE_SUFFIX));
fs_err::write(&git, contents)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs_err::metadata(&git)?.permissions();
perms.set_mode(0o755);
fs_err::set_permissions(&git, perms)?;
}
Ok(())
}
/// Shared behaviour for almost all test commands. /// Shared behaviour for almost all test commands.
/// ///
/// * Use a temporary cache directory /// * Use a temporary cache directory
@ -468,12 +496,18 @@ impl TestContext {
/// `UV_TEST_PYTHON_PATH` and an active venv (if applicable) by removing `VIRTUAL_ENV`. /// `UV_TEST_PYTHON_PATH` and an active venv (if applicable) by removing `VIRTUAL_ENV`.
/// * Increase the stack size to avoid stack overflows on windows due to large async functions. /// * Increase the stack size to avoid stack overflows on windows due to large async functions.
pub fn add_shared_args(&self, command: &mut Command, activate_venv: bool) { pub fn add_shared_args(&self, command: &mut Command, activate_venv: bool) {
// Push the test context bin to the front of the PATH
let mut path = OsString::from(self.bin_dir.as_ref());
path.push(if cfg!(windows) { ";" } else { ":" });
path.push(env::var(EnvVars::PATH).unwrap_or_default());
command command
.arg("--cache-dir") .arg("--cache-dir")
.arg(self.cache_dir.path()) .arg(self.cache_dir.path())
// When running the tests in a venv, ignore that venv, otherwise we'll capture warnings. // When running the tests in a venv, ignore that venv, otherwise we'll capture warnings.
.env_remove(EnvVars::VIRTUAL_ENV) .env_remove(EnvVars::VIRTUAL_ENV)
.env(EnvVars::UV_NO_WRAP, "1") .env(EnvVars::UV_NO_WRAP, "1")
.env(EnvVars::PATH, path)
.env(EnvVars::HOME, self.home_dir.as_os_str()) .env(EnvVars::HOME, self.home_dir.as_os_str())
.env(EnvVars::UV_PYTHON_INSTALL_DIR, "") .env(EnvVars::UV_PYTHON_INSTALL_DIR, "")
.env(EnvVars::UV_TEST_PYTHON_PATH, self.python_path()) .env(EnvVars::UV_TEST_PYTHON_PATH, self.python_path())

View File

@ -4545,6 +4545,7 @@ fn add_repeat() -> Result<()> {
/// Add from requirement file. /// Add from requirement file.
#[test] #[test]
#[cfg(feature = "git")]
fn add_requirements_file() -> Result<()> { fn add_requirements_file() -> Result<()> {
let context = TestContext::new("3.12").with_filtered_counts(); let context = TestContext::new("3.12").with_filtered_counts();

View File

@ -10768,6 +10768,7 @@ fn lock_mismatched_sources() -> Result<()> {
/// ///
/// See: <https://github.com/astral-sh/uv/issues/4604> /// See: <https://github.com/astral-sh/uv/issues/4604>
#[test] #[test]
#[cfg(feature = "git")]
fn lock_mismatched_versions() -> Result<()> { fn lock_mismatched_versions() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");

View File

@ -28,7 +28,7 @@ mod export;
mod help; mod help;
#[cfg(all(feature = "python", feature = "pypi"))] #[cfg(all(feature = "python", feature = "pypi", feature = "git"))]
mod init; mod init;
#[cfg(all(feature = "python", feature = "pypi"))] #[cfg(all(feature = "python", feature = "pypi"))]

View File

@ -6812,6 +6812,7 @@ fn verify_hashes_editable() -> Result<()> {
} }
#[test] #[test]
#[cfg(feature = "git")]
fn tool_uv_sources() -> Result<()> { fn tool_uv_sources() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
// Use a subdir to test path normalization. // Use a subdir to test path normalization.
@ -8262,6 +8263,7 @@ fn cyclic_build_dependency() {
} }
#[test] #[test]
#[cfg(feature = "git")]
fn direct_url_json_git_default() -> Result<()> { fn direct_url_json_git_default() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
let requirements_txt = context.temp_dir.child("requirements.txt"); let requirements_txt = context.temp_dir.child("requirements.txt");
@ -8299,6 +8301,7 @@ fn direct_url_json_git_default() -> Result<()> {
} }
#[test] #[test]
#[cfg(feature = "git")]
fn direct_url_json_git_tag() -> Result<()> { fn direct_url_json_git_tag() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
let requirements_txt = context.temp_dir.child("requirements.txt"); let requirements_txt = context.temp_dir.child("requirements.txt");

View File

@ -581,6 +581,7 @@ fn run_pythonw_script() -> Result<()> {
/// Run a PEP 723-compatible script with `tool.uv` metadata. /// Run a PEP 723-compatible script with `tool.uv` metadata.
#[test] #[test]
#[cfg(feature = "git")]
fn run_pep723_script_metadata() -> Result<()> { fn run_pep723_script_metadata() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");

View File

@ -3183,6 +3183,7 @@ fn sync_custom_environment_path() -> Result<()> {
} }
#[test] #[test]
#[cfg(feature = "git")]
fn sync_workspace_custom_environment_path() -> Result<()> { fn sync_workspace_custom_environment_path() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
@ -5276,7 +5277,7 @@ fn sync_derivation_chain_group() -> Result<()> {
/// See: <https://github.com/astral-sh/uv/issues/9743> /// See: <https://github.com/astral-sh/uv/issues/9743>
#[test] #[test]
#[cfg(feature = "slow-tests")] #[cfg(all(feature = "slow-tests", feature = "git"))]
fn sync_stale_egg_info() -> Result<()> { fn sync_stale_egg_info() -> Result<()> {
let context = TestContext::new("3.13"); let context = TestContext::new("3.13");