uv/tests: move ecosystem project copying to TestContext

So that we can easily reuse ecosystem projects in other tests.
This commit is contained in:
Andrew Gallant
2024-08-15 13:52:21 -04:00
committed by Andrew Gallant
parent 74066ec29b
commit b268f5eb8a
2 changed files with 36 additions and 33 deletions
+34 -1
View File
@@ -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 {
+2 -32
View File
@@ -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(())
}