@@ -33,40 +33,6 @@ pub fn get_install_dir() -> Result<PathBuf> {
3333 Err ( anyhow ! ( "unable to determine install directory" ) )
3434}
3535
36- pub fn resolve_binary_path ( name : & str ) -> Result < PathBuf > {
37- if let Ok ( path) = which:: which ( name) {
38- return Ok ( path) ;
39- }
40-
41- for dir in candidate_bin_dirs ( ) {
42- let candidate = dir. join ( name) ;
43- if candidate. exists ( ) {
44- return Ok ( candidate) ;
45- }
46- #[ cfg( windows) ]
47- {
48- let exe_candidate = candidate. with_extension ( "exe" ) ;
49- if exe_candidate. exists ( ) {
50- return Ok ( exe_candidate) ;
51- }
52- }
53- }
54-
55- Err ( anyhow ! ( "cannot find binary path" ) )
56- }
57-
58- fn candidate_bin_dirs ( ) -> Vec < PathBuf > {
59- let mut dirs = Vec :: new ( ) ;
60-
61- // Only check our sandboxed install directory
62- if let Ok ( install_dir) = get_install_dir ( ) {
63- dirs. push ( install_dir. join ( "bin" ) ) ;
64- dirs. push ( install_dir) ;
65- }
66-
67- dirs
68- }
69-
7036fn home_dir ( ) -> Option < PathBuf > {
7137 env:: var_os ( "HOME" )
7238 . or_else ( || env:: var_os ( "USERPROFILE" ) )
@@ -76,9 +42,11 @@ fn home_dir() -> Option<PathBuf> {
7642#[ cfg( test) ]
7743mod tests {
7844 use super :: * ;
45+ use crate :: test_support:: env_lock;
7946
8047 #[ test]
8148 fn get_install_dir_respects_cargox_install_dir ( ) {
49+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
8250 let temp = tempfile:: tempdir ( ) . unwrap ( ) ;
8351 let custom_path = temp. path ( ) . to_path_buf ( ) ;
8452
@@ -95,6 +63,7 @@ mod tests {
9563
9664 #[ test]
9765 fn get_install_dir_ignores_cargo_install_root ( ) {
66+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
9867 let temp = tempfile:: tempdir ( ) . unwrap ( ) ;
9968 let cargo_root = temp. path ( ) . join ( "cargo_root" ) ;
10069
@@ -114,6 +83,7 @@ mod tests {
11483
11584 #[ test]
11685 fn get_install_dir_uses_xdg_directories ( ) {
86+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
11787 // Clear any override env vars
11888 unsafe {
11989 env:: remove_var ( "CARGOX_INSTALL_DIR" ) ;
@@ -134,129 +104,4 @@ mod tests {
134104 #[ cfg( target_os = "windows" ) ]
135105 assert ! ( result. to_string_lossy( ) . contains( "AppData" ) ) ;
136106 }
137-
138- #[ test]
139- fn candidate_bin_dirs_only_checks_cargox_dir ( ) {
140- // Clear any override env vars
141- unsafe {
142- env:: remove_var ( "CARGOX_INSTALL_DIR" ) ;
143- }
144-
145- let dirs = candidate_bin_dirs ( ) ;
146-
147- // Should only have 2 entries: install_dir/bin and install_dir
148- assert_eq ! ( dirs. len( ) , 2 ) ;
149-
150- // Both should contain "cargox"
151- for dir in & dirs {
152- assert ! ( dir. to_string_lossy( ) . contains( "cargox" ) ) ;
153- }
154-
155- // Should NOT contain any standard cargo directories
156- let dir_strings: Vec < String > = dirs
157- . iter ( )
158- . map ( |d| d. to_string_lossy ( ) . to_string ( ) )
159- . collect ( ) ;
160-
161- for dir_str in & dir_strings {
162- assert ! (
163- !dir_str. contains( "/.cargo/bin" ) ,
164- "Should not check ~/.cargo/bin, found: {dir_str}"
165- ) ;
166- assert ! (
167- !dir_str. contains( "\\ .cargo\\ bin" ) ,
168- "Should not check ~/.cargo/bin, found: {dir_str}"
169- ) ;
170- }
171- }
172-
173- #[ test]
174- fn candidate_bin_dirs_ignores_cargo_env_vars ( ) {
175- let temp = tempfile:: tempdir ( ) . unwrap ( ) ;
176-
177- // Set various cargo-related env vars
178- unsafe {
179- env:: set_var ( "CARGO_INSTALL_ROOT" , temp. path ( ) . join ( "cargo_root" ) ) ;
180- env:: set_var ( "CARGO_HOME" , temp. path ( ) . join ( "cargo_home" ) ) ;
181- env:: set_var ( "BINSTALL_INSTALL_PATH" , temp. path ( ) . join ( "binstall" ) ) ;
182- }
183-
184- let dirs = candidate_bin_dirs ( ) ;
185-
186- // Clean up
187- unsafe {
188- env:: remove_var ( "CARGO_INSTALL_ROOT" ) ;
189- env:: remove_var ( "CARGO_HOME" ) ;
190- env:: remove_var ( "BINSTALL_INSTALL_PATH" ) ;
191- }
192-
193- // None of the candidate dirs should match the env var paths
194- let dir_strings: Vec < String > = dirs
195- . iter ( )
196- . map ( |d| d. to_string_lossy ( ) . to_string ( ) )
197- . collect ( ) ;
198-
199- for dir_str in & dir_strings {
200- assert ! (
201- !dir_str. contains( "cargo_root" ) ,
202- "Should ignore CARGO_INSTALL_ROOT, found: {dir_str}"
203- ) ;
204- assert ! (
205- !dir_str. contains( "cargo_home" ) ,
206- "Should ignore CARGO_HOME, found: {dir_str}"
207- ) ;
208- assert ! (
209- !dir_str. contains( "binstall" ) ,
210- "Should ignore BINSTALL_INSTALL_PATH, found: {dir_str}"
211- ) ;
212- }
213- }
214-
215- #[ test]
216- fn resolve_binary_path_checks_which_first ( ) {
217- // This test verifies that we check PATH first via which
218- // We use a binary that's definitely on PATH (like "ls" on Unix or "cmd" on Windows)
219- #[ cfg( unix) ]
220- let result = resolve_binary_path ( "ls" ) ;
221-
222- #[ cfg( windows) ]
223- let result = resolve_binary_path ( "cmd" ) ;
224-
225- // Should find the system binary via which
226- assert ! ( result. is_ok( ) ) ;
227- }
228-
229- #[ test]
230- fn resolve_binary_path_falls_back_to_cargox_dir ( ) {
231- // Clear any override env vars
232- unsafe {
233- env:: remove_var ( "CARGOX_INSTALL_DIR" ) ;
234- }
235-
236- // Try to find a binary that doesn't exist
237- let result = resolve_binary_path ( "this_binary_definitely_does_not_exist_12345" ) ;
238-
239- // Should fail because it's not in PATH and not in our install dir
240- assert ! ( result. is_err( ) ) ;
241- }
242-
243- #[ test]
244- fn cargox_install_dir_override_is_respected ( ) {
245- let temp = tempfile:: tempdir ( ) . unwrap ( ) ;
246- let custom_dir = temp. path ( ) . join ( "my_custom_cargox" ) ;
247-
248- unsafe {
249- env:: set_var ( "CARGOX_INSTALL_DIR" , & custom_dir) ;
250- }
251-
252- let dirs = candidate_bin_dirs ( ) ;
253-
254- unsafe {
255- env:: remove_var ( "CARGOX_INSTALL_DIR" ) ;
256- }
257-
258- // Should use the custom directory
259- assert ! ( dirs. iter( ) . any( |d| d == & custom_dir. join( "bin" ) ) ) ;
260- assert ! ( dirs. iter( ) . any( |d| d == & custom_dir) ) ;
261- }
262107}
0 commit comments