Skip to content

Commit 0e99e40

Browse files
committed
Escape paths
1 parent 4273db1 commit 0e99e40

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

crates/uv/tests/common/mod.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// The `unreachable_pub` is to silence false positives in RustRover.
22
#![allow(dead_code, unreachable_pub)]
33

4-
use std::borrow::BorrowMut;
5-
use std::env;
6-
use std::path::{Path, PathBuf};
7-
use std::process::Output;
8-
94
use assert_cmd::assert::{Assert, OutputAssertExt};
105
use assert_cmd::Command;
116
use assert_fs::assert::PathAssert;
@@ -15,8 +10,14 @@ use fs_err::os::unix::fs::symlink as symlink_file;
1510
#[cfg(windows)]
1611
use fs_err::os::windows::fs::symlink_file;
1712
use platform_host::Platform;
13+
use regex;
1814
use regex::Regex;
15+
use std::borrow::BorrowMut;
16+
use std::env;
17+
use std::path::{Path, PathBuf};
18+
use std::process::Output;
1919
use uv_cache::Cache;
20+
use uv_fs::Normalized;
2021

2122
use uv_interpreter::find_requested_python;
2223

@@ -45,10 +46,10 @@ pub struct TestContext {
4546
pub cache_dir: assert_fs::TempDir,
4647
pub venv: PathBuf,
4748

48-
// Canonical paths, we store these for creating filters
49-
temp_dir_path: PathBuf,
50-
cache_dir_path: PathBuf,
51-
venv_path: PathBuf,
49+
// Escaped patterns matching the paths for filters
50+
temp_dir_pattern: String,
51+
cache_dir_pattern: String,
52+
venv_pattern: String,
5253
}
5354

5455
impl TestContext {
@@ -57,23 +58,17 @@ impl TestContext {
5758
let cache_dir = assert_fs::TempDir::new().expect("Failed to create cache dir");
5859
let venv = create_venv(&temp_dir, &cache_dir, python_version);
5960

60-
let cache_dir_path = cache_dir
61-
.canonicalize()
62-
.expect("Failed to create canonical path");
63-
let venv_path = venv
64-
.canonicalize()
65-
.expect("Failed to create canonical path");
66-
let temp_dir_path = temp_dir
67-
.canonicalize()
68-
.expect("Failed to create canonical path");
61+
let cache_dir_pattern = Self::path_pattern(&cache_dir);
62+
let temp_dir_pattern = Self::path_pattern(&temp_dir);
63+
let venv_pattern = Self::path_pattern(&venv);
6964

7065
Self {
7166
temp_dir,
7267
cache_dir,
7368
venv,
74-
temp_dir_path,
75-
cache_dir_path,
76-
venv_path,
69+
temp_dir_pattern,
70+
cache_dir_pattern,
71+
venv_pattern,
7772
}
7873
}
7974

@@ -116,14 +111,26 @@ impl TestContext {
116111
.stdout(version);
117112
}
118113

114+
/// Generate an escaped regex pattern for the given path.
115+
fn path_pattern(path: impl AsRef<Path>) -> String {
116+
regex::escape(
117+
&path
118+
.as_ref()
119+
.canonicalize()
120+
.expect("Failed to create canonical path")
121+
.normalized()
122+
.to_string_lossy(),
123+
)
124+
}
125+
119126
/// Canonical snapshot filters for this test context.
120127
pub fn filters<'a>(&'a self) -> Vec<(&'a str, &'a str)> {
121128
let mut filters = INSTA_FILTERS.to_vec();
122129

123-
filters.push((self.cache_dir_path.to_str().unwrap(), "[CACHE DIR]"));
124-
filters.push((self.venv_path.to_str().unwrap(), "[VENV DIR]"));
130+
filters.push((&self.cache_dir_pattern, "[CACHE DIR]"));
131+
filters.push((&self.venv_pattern, "[VENV DIR]"));
125132
// Note the temporary directoy comes last because the others are nested within
126-
filters.push((self.temp_dir_path.to_str().unwrap(), "[TEMP DIR]"));
133+
filters.push((&self.temp_dir_pattern, "[TEMP DIR]"));
127134

128135
filters
129136
}
@@ -264,7 +271,7 @@ pub fn create_bin_with_executables(
264271
/// This function is derived from `insta_cmd`s `spawn_with_info`.
265272
pub fn run_and_format<'a>(
266273
mut command: impl BorrowMut<std::process::Command>,
267-
filters: impl AsRef<[(AsRef<str>, AsRef<str>)]>,
274+
filters: impl AsRef<[(&'a str, &'a str)]>,
268275
windows_filters: bool,
269276
) -> (String, Output) {
270277
let program = command

0 commit comments

Comments
 (0)