From c6af78d15301e752a0272934d54fbd9b818cc553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 6 Mar 2024 14:32:07 +0100 Subject: [PATCH] test: pip_list: Fix replacement pattern computation (#2237) ## Summary Fix computing replacements pattern for pip_list tests to count characters in the original directory string rather than the regex::escape'd string. The latter yields incorrect results if the workspace path contains characters such as `-` or `.`. Fixes #2232 ## Test Plan `cargo test --test pip_list` in a directory named `uv-test` to provoke the bug. --- crates/uv/tests/pip_list.rs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/crates/uv/tests/pip_list.rs b/crates/uv/tests/pip_list.rs index c40f478a4..bb91f213f 100644 --- a/crates/uv/tests/pip_list.rs +++ b/crates/uv/tests/pip_list.rs @@ -98,13 +98,11 @@ fn list_editable() -> Result<()> { let context = TestContext::new("3.12"); let current_dir = std::env::current_dir()?; - let workspace_dir = regex::escape( - Url::from_directory_path(current_dir.join("..").join("..").canonicalize()?) - .unwrap() - .as_str(), - ); + let workspace_dir = + Url::from_directory_path(current_dir.join("..").join("..").canonicalize()?).unwrap(); + let workspace_dir_re = regex::escape(workspace_dir.as_str()); - let filters = [(workspace_dir.as_str(), "file://[WORKSPACE_DIR]/")] + let filters = [(workspace_dir_re.as_str(), "file://[WORKSPACE_DIR]/")] .into_iter() .chain(INSTA_FILTERS.to_vec()) .collect::>(); @@ -159,7 +157,7 @@ fn list_editable() -> Result<()> { let find_whitespace = " ".repeat(25 + workspace_len_difference); let replace_whitespace = " ".repeat(57); - let search_workspace = workspace_dir.as_str().strip_prefix(prefix).unwrap(); + let search_workspace = workspace_dir_re.as_str().strip_prefix(prefix).unwrap(); let replace_workspace = "[WORKSPACE_DIR]/"; let filters = INSTA_FILTERS @@ -200,13 +198,11 @@ fn list_editable_only() -> Result<()> { let context = TestContext::new("3.12"); let current_dir = std::env::current_dir()?; - let workspace_dir = regex::escape( - Url::from_directory_path(current_dir.join("..").join("..").canonicalize()?) - .unwrap() - .as_str(), - ); + let workspace_dir = + Url::from_directory_path(current_dir.join("..").join("..").canonicalize()?).unwrap(); + let workspace_dir_re = regex::escape(workspace_dir.as_str()); - let filters = [(workspace_dir.as_str(), "file://[WORKSPACE_DIR]/")] + let filters = [(workspace_dir_re.as_str(), "file://[WORKSPACE_DIR]/")] .into_iter() .chain(INSTA_FILTERS.to_vec()) .collect::>(); @@ -254,7 +250,7 @@ fn list_editable_only() -> Result<()> { let find_whitespace = " ".repeat(25 + workspace_len_difference); let replace_whitespace = " ".repeat(57); - let search_workspace = workspace_dir.as_str().strip_prefix(prefix).unwrap(); + let search_workspace = workspace_dir_re.as_str().strip_prefix(prefix).unwrap(); let replace_workspace = "[WORKSPACE_DIR]/"; let filters = INSTA_FILTERS @@ -331,13 +327,11 @@ fn list_exclude() -> Result<()> { let context = TestContext::new("3.12"); let current_dir = std::env::current_dir()?; - let workspace_dir = regex::escape( - Url::from_directory_path(current_dir.join("..").join("..").canonicalize()?) - .unwrap() - .as_str(), - ); + let workspace_dir = + Url::from_directory_path(current_dir.join("..").join("..").canonicalize()?).unwrap(); + let workspace_dir_re = regex::escape(workspace_dir.as_str()); - let filters = [(workspace_dir.as_str(), "file://[WORKSPACE_DIR]/")] + let filters = [(workspace_dir_re.as_str(), "file://[WORKSPACE_DIR]/")] .into_iter() .chain(INSTA_FILTERS.to_vec()) .collect::>(); @@ -385,7 +379,7 @@ fn list_exclude() -> Result<()> { let find_whitespace = " ".repeat(25 + workspace_len_difference); let replace_whitespace = " ".repeat(57); - let search_workspace = workspace_dir.as_str().strip_prefix(prefix).unwrap(); + let search_workspace = workspace_dir_re.as_str().strip_prefix(prefix).unwrap(); let replace_workspace = "[WORKSPACE_DIR]/"; let filters = INSTA_FILTERS