From da7d5549a370b762e7301524194cc9f59934485a Mon Sep 17 00:00:00 2001 From: konsti Date: Tue, 4 Jun 2024 14:58:07 +0200 Subject: [PATCH] Don't copy gitignored files in workspace tests (#4012) The workspace test directories can be used both in tests and directly for developing/debugging. In the latter, we shouldn't copy the venv and the lockfile when running tests. Using the ignore crate over manual recursion we exclude those files. --- Cargo.lock | 1 + crates/uv/Cargo.toml | 1 + crates/uv/tests/common/mod.rs | 15 +++++++++++++++ crates/uv/tests/workspace.rs | 6 +++--- scripts/workspaces/.gitignore | 1 + 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 scripts/workspaces/.gitignore diff --git a/Cargo.lock b/Cargo.lock index 415e3bb26..bb955e8ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4383,6 +4383,7 @@ dependencies = [ "filetime", "flate2", "fs-err", + "ignore", "indicatif", "indoc", "insta", diff --git a/crates/uv/Cargo.toml b/crates/uv/Cargo.toml index 2896fa526..8cdd2eabe 100644 --- a/crates/uv/Cargo.toml +++ b/crates/uv/Cargo.toml @@ -78,6 +78,7 @@ assert_fs = { version = "1.1.0" } base64 = { version = "0.22.0" } byteorder = { version = "1.5.0" } filetime = { version = "0.2.23" } +ignore = { version = "0.4.22" } indoc = { version = "2.0.4" } insta = { version = "1.36.1", features = ["filters", "json"] } predicates = { version = "3.0.4" } diff --git a/crates/uv/tests/common/mod.rs b/crates/uv/tests/common/mod.rs index bd84c7518..fabc4a7cf 100644 --- a/crates/uv/tests/common/mod.rs +++ b/crates/uv/tests/common/mod.rs @@ -628,6 +628,21 @@ pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Re Ok(()) } +/// Recursively copy a directory and its contents, skipping gitignored files. +pub fn copy_dir_ignore(src: impl AsRef, dst: impl AsRef) -> anyhow::Result<()> { + for entry in ignore::Walk::new(&src) { + let entry = entry?; + let relative = entry.path().strip_prefix(&src)?; + let ty = entry.file_type().unwrap(); + if ty.is_dir() { + fs_err::create_dir(dst.as_ref().join(relative))?; + } else { + fs_err::copy(entry.path(), dst.as_ref().join(relative))?; + } + } + Ok(()) +} + /// Utility macro to return the name of the current function. /// /// https://stackoverflow.com/a/40234666/3549270 diff --git a/crates/uv/tests/workspace.rs b/crates/uv/tests/workspace.rs index 5d60855e6..d0b8cbb1f 100644 --- a/crates/uv/tests/workspace.rs +++ b/crates/uv/tests/workspace.rs @@ -4,7 +4,7 @@ use std::process::Command; use anyhow::Result; -use crate::common::{copy_dir_all, get_bin, uv_snapshot, TestContext, EXCLUDE_NEWER}; +use crate::common::{copy_dir_ignore, get_bin, uv_snapshot, TestContext, EXCLUDE_NEWER}; mod common; @@ -373,7 +373,7 @@ fn test_uv_run_with_package_virtual_workspace() -> Result<()> { let context = TestContext::new("3.12"); let work_dir = context.temp_dir.join("albatross-virtual-workspace"); - copy_dir_all( + copy_dir_ignore( workspaces_dir().join("albatross-virtual-workspace"), &work_dir, )?; @@ -444,7 +444,7 @@ fn test_uv_run_with_package_root_workspace() -> Result<()> { let context = TestContext::new("3.12"); let work_dir = context.temp_dir.join("albatross-root-workspace"); - copy_dir_all(workspaces_dir().join("albatross-root-workspace"), &work_dir)?; + copy_dir_ignore(workspaces_dir().join("albatross-root-workspace"), &work_dir)?; // TODO(konsti): `--python` is being ignored atm, so we need to create the correct venv // ourselves and add the output filters. diff --git a/scripts/workspaces/.gitignore b/scripts/workspaces/.gitignore new file mode 100644 index 000000000..07df930ad --- /dev/null +++ b/scripts/workspaces/.gitignore @@ -0,0 +1 @@ +uv.lock