@@ -356,6 +356,84 @@ fn handle_python_show(_opts: GlobalOpts) {
356356#[ cfg( test) ]
357357mod tests {
358358 use super :: * ;
359+ use std:: fs;
360+ use std:: io;
361+ use std:: path:: Path ;
362+ use tempfile:: TempDir ;
363+
364+ struct TestConfigGuard {
365+ _dir : TempDir ,
366+ }
367+
368+ impl Drop for TestConfigGuard {
369+ fn drop ( & mut self ) {
370+ std:: env:: remove_var ( "R2X_CONFIG" ) ;
371+ }
372+ }
373+
374+ fn setup_test_config ( ) -> TestConfigGuard {
375+ let dir = TempDir :: new ( ) . expect ( "tempdir" ) ;
376+ let base = dir. path ( ) ;
377+ let config_path = base. join ( "r2x.toml" ) ;
378+ let cache_path = base. join ( "cache" ) ;
379+ let venv_path = base. join ( ".venv" ) ;
380+ let uv_path = if cfg ! ( windows) {
381+ base. join ( "uv.bat" )
382+ } else {
383+ base. join ( "uv" )
384+ } ;
385+
386+ fs:: create_dir_all ( & cache_path) . unwrap ( ) ;
387+ fs:: create_dir_all ( & venv_path) . unwrap ( ) ;
388+ create_fake_python ( & venv_path) . unwrap ( ) ;
389+ write_stub_uv ( & uv_path) . unwrap ( ) ;
390+
391+ let config_contents = format ! (
392+ "cache_path = \" {}\" \n uv_path = \" {}\" \n venv_path = \" {}\" \n python_version = \" 3.12\" \n " ,
393+ cache_path. to_string_lossy( ) ,
394+ uv_path. to_string_lossy( ) ,
395+ venv_path. to_string_lossy( )
396+ ) ;
397+
398+ fs:: write ( & config_path, config_contents) . unwrap ( ) ;
399+ std:: env:: set_var ( "R2X_CONFIG" , & config_path) ;
400+
401+ TestConfigGuard { _dir : dir }
402+ }
403+
404+ #[ cfg( unix) ]
405+ fn write_stub_uv ( path : & Path ) -> io:: Result < ( ) > {
406+ use std:: os:: unix:: fs:: PermissionsExt ;
407+ fs:: write ( path, "#!/bin/sh\n exit 0\n " ) ?;
408+ let mut perms = fs:: metadata ( path) ?. permissions ( ) ;
409+ perms. set_mode ( 0o755 ) ;
410+ fs:: set_permissions ( path, perms)
411+ }
412+
413+ #[ cfg( windows) ]
414+ fn write_stub_uv ( path : & Path ) -> io:: Result < ( ) > {
415+ fs:: write ( path, "@echo off\r \n exit /b 0\r \n " )
416+ }
417+
418+ #[ cfg( unix) ]
419+ fn create_fake_python ( venv_path : & Path ) -> io:: Result < ( ) > {
420+ use std:: os:: unix:: fs:: PermissionsExt ;
421+ let bin = venv_path. join ( "bin" ) ;
422+ fs:: create_dir_all ( & bin) ?;
423+ let python = bin. join ( "python" ) ;
424+ fs:: write ( & python, "#!/bin/sh\n exit 0\n " ) ?;
425+ let mut perms = fs:: metadata ( & python) ?. permissions ( ) ;
426+ perms. set_mode ( 0o755 ) ;
427+ fs:: set_permissions ( python, perms)
428+ }
429+
430+ #[ cfg( windows) ]
431+ fn create_fake_python ( venv_path : & Path ) -> io:: Result < ( ) > {
432+ let scripts = venv_path. join ( "Scripts" ) ;
433+ fs:: create_dir_all ( & scripts) ?;
434+ fs:: write ( scripts. join ( "python.exe" ) , [ ] ) ?;
435+ Ok ( ( ) )
436+ }
359437
360438 fn normal_opts ( ) -> GlobalOpts {
361439 GlobalOpts {
@@ -367,6 +445,7 @@ mod tests {
367445
368446 #[ test]
369447 fn test_python_install ( ) {
448+ let _guard = setup_test_config ( ) ;
370449 handle_python (
371450 PythonAction :: Install {
372451 version : Some ( "3.12" . to_string ( ) ) ,
@@ -377,35 +456,49 @@ mod tests {
377456
378457 #[ test]
379458 fn test_python_install_no_version ( ) {
459+ let _guard = setup_test_config ( ) ;
380460 handle_python ( PythonAction :: Install { version : None } , normal_opts ( ) ) ;
381461 }
382462
383463 #[ test]
384464 fn test_python_path ( ) {
465+ let _guard = setup_test_config ( ) ;
385466 handle_python ( PythonAction :: Path , normal_opts ( ) ) ;
386467 }
387468
388469 #[ test]
389470 fn test_python_show ( ) {
471+ let _guard = setup_test_config ( ) ;
390472 handle_python ( PythonAction :: Show , normal_opts ( ) ) ;
391473 }
392474
393475 #[ test]
394476 fn test_venv_create ( ) {
477+ let _guard = setup_test_config ( ) ;
395478 handle_venv ( None , false , normal_opts ( ) ) ;
396479 }
397480
398481 #[ test]
399482 fn test_venv_create_skip_confirm ( ) {
483+ let _guard = setup_test_config ( ) ;
400484 handle_venv ( None , true , normal_opts ( ) ) ;
401485 }
402486
403487 #[ test]
404488 fn test_venv_path ( ) {
489+ let _guard = setup_test_config ( ) ;
405490 handle_venv (
406491 Some ( VenvAction :: Path { new_path : None } ) ,
407492 false ,
408493 normal_opts ( ) ,
409494 ) ;
410495 }
496+
497+ #[ test]
498+ fn test_venv_create_yes_flag ( ) {
499+ let _guard = setup_test_config ( ) ;
500+ std:: env:: set_var ( "R2X_VENV_YES" , "1" ) ;
501+ handle_venv ( None , false , normal_opts ( ) ) ;
502+ std:: env:: remove_var ( "R2X_VENV_YES" ) ;
503+ }
411504}
0 commit comments