Support `copy_dir_all` in unit tests too (#9124)

Refactoring for https://github.com/astral-sh/uv/pull/9091
This commit is contained in:
konsti 2024-11-14 19:05:15 +01:00 committed by GitHub
parent 50cfbd904c
commit c2ccc2c5dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 29 additions and 29 deletions

View File

@ -443,3 +443,18 @@ impl<Reader: tokio::io::AsyncRead + Unpin, Callback: Fn(usize) + Unpin> tokio::i
})
}
}
/// Recursively copy a directory and its contents.
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> 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(())
}

View File

@ -1289,21 +1289,6 @@ pub fn run_and_format_with_status<T: AsRef<str>>(
(snapshot, output, status)
}
/// Recursively copy a directory and its contents.
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> 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<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> {
for entry in ignore::Walk::new(&src) {

View File

@ -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(())

View File

@ -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();

View File

@ -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) {

View File

@ -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");

View File

@ -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<()> {

View File

@ -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() {

View File

@ -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]