1
1
// The `unreachable_pub` is to silence false positives in RustRover.
2
2
#![ allow( dead_code, unreachable_pub) ]
3
3
4
- use std:: borrow:: BorrowMut ;
5
- use std:: env;
6
- use std:: path:: { Path , PathBuf } ;
7
- use std:: process:: Output ;
8
-
9
4
use assert_cmd:: assert:: { Assert , OutputAssertExt } ;
10
5
use assert_cmd:: Command ;
11
6
use assert_fs:: assert:: PathAssert ;
@@ -15,8 +10,14 @@ use fs_err::os::unix::fs::symlink as symlink_file;
15
10
#[ cfg( windows) ]
16
11
use fs_err:: os:: windows:: fs:: symlink_file;
17
12
use platform_host:: Platform ;
13
+ use regex;
18
14
use regex:: Regex ;
15
+ use std:: borrow:: BorrowMut ;
16
+ use std:: env;
17
+ use std:: path:: { Path , PathBuf } ;
18
+ use std:: process:: Output ;
19
19
use uv_cache:: Cache ;
20
+ use uv_fs:: Normalized ;
20
21
21
22
use uv_interpreter:: find_requested_python;
22
23
@@ -45,10 +46,10 @@ pub struct TestContext {
45
46
pub cache_dir : assert_fs:: TempDir ,
46
47
pub venv : PathBuf ,
47
48
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 ,
52
53
}
53
54
54
55
impl TestContext {
@@ -57,23 +58,17 @@ impl TestContext {
57
58
let cache_dir = assert_fs:: TempDir :: new ( ) . expect ( "Failed to create cache dir" ) ;
58
59
let venv = create_venv ( & temp_dir, & cache_dir, python_version) ;
59
60
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) ;
69
64
70
65
Self {
71
66
temp_dir,
72
67
cache_dir,
73
68
venv,
74
- temp_dir_path ,
75
- cache_dir_path ,
76
- venv_path ,
69
+ temp_dir_pattern ,
70
+ cache_dir_pattern ,
71
+ venv_pattern ,
77
72
}
78
73
}
79
74
@@ -116,14 +111,26 @@ impl TestContext {
116
111
. stdout ( version) ;
117
112
}
118
113
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
+
119
126
/// Canonical snapshot filters for this test context.
120
127
pub fn filters < ' a > ( & ' a self ) -> Vec < ( & ' a str , & ' a str ) > {
121
128
let mut filters = INSTA_FILTERS . to_vec ( ) ;
122
129
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]" ) ) ;
125
132
// 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]" ) ) ;
127
134
128
135
filters
129
136
}
@@ -264,7 +271,7 @@ pub fn create_bin_with_executables(
264
271
/// This function is derived from `insta_cmd`s `spawn_with_info`.
265
272
pub fn run_and_format < ' a > (
266
273
mut command : impl BorrowMut < std:: process:: Command > ,
267
- filters : impl AsRef < [ ( AsRef < str > , AsRef < str > ) ] > ,
274
+ filters : impl AsRef < [ ( & ' a str , & ' a str ) ] > ,
268
275
windows_filters : bool ,
269
276
) -> ( String , Output ) {
270
277
let program = command
0 commit comments