Skip to content

Commit

Permalink
test: pip_list: Fix replacement pattern computation (astral-sh#2237)
Browse files Browse the repository at this point in the history
## 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 astral-sh#2232

## Test Plan

`cargo test --test pip_list` in a directory named `uv-test` to provoke
the bug.
  • Loading branch information
mgorny authored Mar 6, 2024
1 parent 3ca7776 commit c6af78d
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions crates/uv/tests/pip_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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::<Vec<_>>();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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::<Vec<_>>();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c6af78d

Please sign in to comment.