Skip to content

Commit

Permalink
Test if git is used without flagging
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Jan 22, 2025
1 parent d6d0593 commit c48e963
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ jobs:
with:
tool: cargo-nextest

- name: "Disable git"
run: |
which git
- name: "Cargo test"
working-directory: ${{ env.UV_WORKSPACE }}
env:
Expand Down
34 changes: 34 additions & 0 deletions crates/uv/tests/it/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub struct TestContext {
pub cache_dir: ChildPath,
pub python_dir: ChildPath,
pub home_dir: ChildPath,
pub bin_dir: ChildPath,
pub venv: ChildPath,
pub workspace_root: PathBuf,

Expand Down Expand Up @@ -274,6 +275,14 @@ impl TestContext {
let python_dir = ChildPath::new(root.path()).child("python");
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");

// git is disabled
if cfg!(not(feature = "git")) {
Self::setup_fake_git(&bin_dir).expect("Failed to setup fake git");
}

let home_dir = ChildPath::new(root.path()).child("home");
fs_err::create_dir_all(&home_dir).expect("Failed to create test home directory");

Expand Down Expand Up @@ -441,6 +450,7 @@ impl TestContext {
cache_dir,
python_dir,
home_dir,
bin_dir,
venv,
workspace_root,
python_version,
Expand All @@ -457,6 +467,24 @@ impl TestContext {
command
}

fn setup_fake_git(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)?;

// Make it executable (equivalent to chmod +x)
if 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.
///
/// * Use a temporary cache directory
Expand All @@ -468,12 +496,18 @@ impl TestContext {
/// `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.
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
.arg("--cache-dir")
.arg(self.cache_dir.path())
// When running the tests in a venv, ignore that venv, otherwise we'll capture warnings.
.env_remove(EnvVars::VIRTUAL_ENV)
.env(EnvVars::UV_NO_WRAP, "1")
.env(EnvVars::PATH, path)
.env(EnvVars::HOME, self.home_dir.as_os_str())
.env(EnvVars::UV_PYTHON_INSTALL_DIR, "")
.env(EnvVars::UV_TEST_PYTHON_PATH, self.python_path())
Expand Down

0 comments on commit c48e963

Please sign in to comment.