From c2ccc2c5dcc91bcd3a6ac9920ac352e726bbc7fe Mon Sep 17 00:00:00 2001 From: konsti Date: Thu, 14 Nov 2024 19:05:15 +0100 Subject: [PATCH] Support `copy_dir_all` in unit tests too (#9124) Refactoring for https://github.com/astral-sh/uv/pull/9091 --- crates/uv-fs/src/lib.rs | 15 +++++++++++++++ crates/uv/tests/it/common/mod.rs | 15 --------------- crates/uv/tests/it/pip_freeze.rs | 6 +++--- crates/uv/tests/it/pip_install.rs | 4 ++-- crates/uv/tests/it/pip_sync.rs | 5 ++--- crates/uv/tests/it/pip_uninstall.rs | 2 +- crates/uv/tests/it/run.rs | 4 ++-- crates/uv/tests/it/tool_install.rs | 4 ++-- crates/uv/tests/it/tool_run.rs | 3 ++- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/crates/uv-fs/src/lib.rs b/crates/uv-fs/src/lib.rs index 1cde77a9d..be4bbb869 100644 --- a/crates/uv-fs/src/lib.rs +++ b/crates/uv-fs/src/lib.rs @@ -443,3 +443,18 @@ impl tokio::i }) } } + +/// Recursively copy a directory and its contents. +pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { + fs_err::create_dir_all(&dst)?; + for entry in fs_err::read_dir(src.as_ref())? { + let entry = entry?; + let ty = entry.file_type()?; + if ty.is_dir() { + copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; + } else { + fs_err::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; + } + } + Ok(()) +} diff --git a/crates/uv/tests/it/common/mod.rs b/crates/uv/tests/it/common/mod.rs index 2fd4d4b98..2a02636b9 100644 --- a/crates/uv/tests/it/common/mod.rs +++ b/crates/uv/tests/it/common/mod.rs @@ -1289,21 +1289,6 @@ pub fn run_and_format_with_status>( (snapshot, output, status) } -/// Recursively copy a directory and its contents. -pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { - fs_err::create_dir_all(&dst)?; - for entry in fs_err::read_dir(src.as_ref())? { - let entry = entry?; - let ty = entry.file_type()?; - if ty.is_dir() { - copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; - } else { - fs_err::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; - } - } - 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) { diff --git a/crates/uv/tests/it/pip_freeze.rs b/crates/uv/tests/it/pip_freeze.rs index 985a6a8bf..50b637068 100644 --- a/crates/uv/tests/it/pip_freeze.rs +++ b/crates/uv/tests/it/pip_freeze.rs @@ -39,7 +39,7 @@ fn freeze_many() -> Result<()> { #[test] #[cfg(unix)] fn freeze_duplicate() -> Result<()> { - use crate::common::copy_dir_all; + use uv_fs::copy_dir_all; // Sync a version of `pip` into a virtual environment. let context1 = TestContext::new("3.12"); @@ -72,7 +72,7 @@ fn freeze_duplicate() -> Result<()> { )?; // Run `pip freeze`. - uv_snapshot!(context1.filters(), context1.pip_freeze().arg("--strict"), @r#" + uv_snapshot!(context1.filters(), context1.pip_freeze().arg("--strict"), @r###" success: true exit_code: 0 ----- stdout ----- @@ -83,7 +83,7 @@ fn freeze_duplicate() -> Result<()> { warning: The package `pip` has multiple installed distributions: - [SITE_PACKAGES]/pip-21.3.1.dist-info - [SITE_PACKAGES]/pip-22.1.1.dist-info - "# + "### ); Ok(()) diff --git a/crates/uv/tests/it/pip_install.rs b/crates/uv/tests/it/pip_install.rs index be6ef7a62..23565d04f 100644 --- a/crates/uv/tests/it/pip_install.rs +++ b/crates/uv/tests/it/pip_install.rs @@ -3183,7 +3183,7 @@ fn config_settings() { /// Reinstall a duplicate package in a virtual environment. #[test] fn reinstall_duplicate() -> Result<()> { - use crate::common::copy_dir_all; + use uv_fs::copy_dir_all; // Sync a version of `pip` into a virtual environment. let context1 = TestContext::new("3.12"); @@ -5399,7 +5399,7 @@ fn already_installed_local_version_of_remote_package() { #[cfg(unix)] fn already_installed_multiple_versions() -> Result<()> { fn prepare(context: &TestContext) -> Result<()> { - use crate::common::copy_dir_all; + use uv_fs::copy_dir_all; // Install into the base environment context.pip_install().arg("anyio==3.7.0").assert().success(); diff --git a/crates/uv/tests/it/pip_sync.rs b/crates/uv/tests/it/pip_sync.rs index 26e740ed3..c3a51e7c7 100644 --- a/crates/uv/tests/it/pip_sync.rs +++ b/crates/uv/tests/it/pip_sync.rs @@ -12,10 +12,9 @@ use predicates::Predicate; use url::Url; use crate::common::{ - copy_dir_all, download_to_disk, site_packages_path, uv_snapshot, venv_to_interpreter, - TestContext, + download_to_disk, site_packages_path, uv_snapshot, venv_to_interpreter, TestContext, }; -use uv_fs::Simplified; +use uv_fs::{copy_dir_all, Simplified}; use uv_static::EnvVars; fn check_command(venv: &Path, command: &str, temp_dir: &Path) { diff --git a/crates/uv/tests/it/pip_uninstall.rs b/crates/uv/tests/it/pip_uninstall.rs index 903aa5e27..3a0f24926 100644 --- a/crates/uv/tests/it/pip_uninstall.rs +++ b/crates/uv/tests/it/pip_uninstall.rs @@ -327,7 +327,7 @@ fn uninstall_duplicate_by_path() -> Result<()> { /// Uninstall a duplicate package in a virtual environment. #[test] fn uninstall_duplicate() -> Result<()> { - use crate::common::copy_dir_all; + use uv_fs::copy_dir_all; // Sync a version of `pip` into a virtual environment. let context1 = TestContext::new("3.12"); diff --git a/crates/uv/tests/it/run.rs b/crates/uv/tests/it/run.rs index cf3b69915..82502c437 100644 --- a/crates/uv/tests/it/run.rs +++ b/crates/uv/tests/it/run.rs @@ -7,11 +7,11 @@ use indoc::indoc; use insta::assert_snapshot; use predicates::str::contains; use std::path::Path; - +use uv_fs::copy_dir_all; use uv_python::PYTHON_VERSION_FILENAME; use uv_static::EnvVars; -use crate::common::{copy_dir_all, uv_snapshot, TestContext}; +use crate::common::{uv_snapshot, TestContext}; #[test] fn run_with_python_version() -> Result<()> { diff --git a/crates/uv/tests/it/tool_install.rs b/crates/uv/tests/it/tool_install.rs index d5db82e56..b09f3e8de 100644 --- a/crates/uv/tests/it/tool_install.rs +++ b/crates/uv/tests/it/tool_install.rs @@ -8,10 +8,10 @@ use assert_fs::{ use indoc::indoc; use insta::assert_snapshot; use predicates::prelude::predicate; - +use uv_fs::copy_dir_all; use uv_static::EnvVars; -use crate::common::{copy_dir_all, uv_snapshot, TestContext}; +use crate::common::{uv_snapshot, TestContext}; #[test] fn tool_install() { diff --git a/crates/uv/tests/it/tool_run.rs b/crates/uv/tests/it/tool_run.rs index 94f62fe53..617d9d1fb 100644 --- a/crates/uv/tests/it/tool_run.rs +++ b/crates/uv/tests/it/tool_run.rs @@ -1,7 +1,8 @@ -use crate::common::{copy_dir_all, uv_snapshot, TestContext}; +use crate::common::{uv_snapshot, TestContext}; use assert_cmd::prelude::*; use assert_fs::prelude::*; use indoc::indoc; +use uv_fs::copy_dir_all; use uv_static::EnvVars; #[test]