From b268f5eb8a8a941d5b7add47a0efed1e3d1d3175 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Thu, 15 Aug 2024 13:52:21 -0400 Subject: [PATCH] uv/tests: move ecosystem project copying to TestContext So that we can easily reuse ecosystem projects in other tests. --- crates/uv/tests/common/mod.rs | 35 ++++++++++++++++++++++++++++++++++- crates/uv/tests/ecosystem.rs | 34 ++-------------------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/crates/uv/tests/common/mod.rs b/crates/uv/tests/common/mod.rs index 7a1565842..4bbcaeabc 100644 --- a/crates/uv/tests/common/mod.rs +++ b/crates/uv/tests/common/mod.rs @@ -11,7 +11,7 @@ use std::str::FromStr; use assert_cmd::assert::{Assert, OutputAssertExt}; use assert_fs::assert::PathAssert; -use assert_fs::fixture::{ChildPath, PathChild, PathCreateDir, SymlinkToFile}; +use assert_fs::fixture::{ChildPath, FileWriteStr, PathChild, PathCreateDir, SymlinkToFile}; use base64::{prelude::BASE64_STANDARD as base64, Engine}; use indoc::formatdoc; use itertools::Itertools; @@ -749,6 +749,39 @@ impl TestContext { ); create_venv_from_executable(&self.venv, &self.cache_dir, &executable); } + + /// Copies the files from the ecosystem project given into this text + /// context. + /// + /// This will almost always write at least a `pyproject.toml` into this + /// test context. + /// + /// The given name should correspond to the name of a sub-directory (not a + /// path to it) in the top-level `ecosystem` directory. + /// + /// This panics (fails the current test) for any failure. + pub fn copy_ecosystem_project(&self, name: &str) { + let project_dir = PathBuf::from(format!("../../ecosystem/{name}")); + // Ideally I think we'd probably just do a recursive copy, + // but for now we just look for the specific files we want. + let required_files = ["pyproject.toml"]; + for file_name in required_files { + let file_contents = fs_err::read_to_string(project_dir.join(file_name)).unwrap(); + let test_file = self.temp_dir.child(file_name); + test_file.write_str(&file_contents).unwrap(); + } + + let optional_files = ["PKG-INFO"]; + for file_name in optional_files { + let path = project_dir.join(file_name); + if !path.exists() { + continue; + } + let file_contents = fs_err::read_to_string(path).unwrap(); + let test_file = self.temp_dir.child(file_name); + test_file.write_str(&file_contents).unwrap(); + } + } } pub fn site_packages_path(venv: &Path, python: &str) -> PathBuf { diff --git a/crates/uv/tests/ecosystem.rs b/crates/uv/tests/ecosystem.rs index 4d2429a76..2299d6366 100644 --- a/crates/uv/tests/ecosystem.rs +++ b/crates/uv/tests/ecosystem.rs @@ -1,9 +1,6 @@ #![cfg(all(feature = "python", feature = "pypi"))] -use std::path::{Path, PathBuf}; - use anyhow::Result; -use assert_fs::prelude::*; use insta::assert_snapshot; use common::{deterministic_lock, TestContext}; @@ -96,9 +93,8 @@ fn pretix() -> Result<()> { /// is, there should be a directory at `./ecosystem/{name}` from the /// root of the `uv` repository. fn lock_ecosystem_package(python_version: &str, name: &str) -> Result<()> { - let dir = PathBuf::from(format!("../../ecosystem/{name}")); let context = TestContext::new(python_version); - setup_project_dir(&context, &dir)?; + context.copy_ecosystem_project(name); deterministic_lock! { context => let mut cmd = context.lock(); @@ -129,9 +125,8 @@ fn lock_ecosystem_package(python_version: &str, name: &str) -> Result<()> { /// ecosystem packages even if re-locking is producing different /// results. fn lock_ecosystem_package_non_deterministic(python_version: &str, name: &str) -> Result<()> { - let dir = PathBuf::from(format!("../../ecosystem/{name}")); let context = TestContext::new(python_version); - setup_project_dir(&context, &dir)?; + context.copy_ecosystem_project(name); let mut cmd = context.lock(); cmd.env("UV_EXCLUDE_NEWER", EXCLUDE_NEWER); @@ -151,28 +146,3 @@ fn lock_ecosystem_package_non_deterministic(python_version: &str, name: &str) -> }); Ok(()) } - -/// Copies the project specific files from `project_dir` into the given -/// test context. -fn setup_project_dir(ctx: &TestContext, project_dir: &Path) -> Result<()> { - // Ideally I think we'd probably just do a recursive copy, - // but for now we just look for the specific files we want. - let required_files = ["pyproject.toml"]; - for file_name in required_files { - let file_contents = fs_err::read_to_string(project_dir.join(file_name))?; - let test_file = ctx.temp_dir.child(file_name); - test_file.write_str(&file_contents)?; - } - - let optional_files = ["PKG-INFO"]; - for file_name in optional_files { - let path = project_dir.join(file_name); - if !path.exists() { - continue; - } - let file_contents = fs_err::read_to_string(path)?; - let test_file = ctx.temp_dir.child(file_name); - test_file.write_str(&file_contents)?; - } - Ok(()) -}