1+ use crate :: command_runner:: { CommandRunner , RealCommandRunner } ;
12use crate :: errors:: CliError ;
23use crate :: essential:: { BASE_COMMAND , connect_to_client, create_config_file, create_configs} ;
34use clap:: { Args , Subcommand } ;
@@ -453,13 +454,16 @@ fn rm_installation_files(file_to_remove: InstallationType) -> Result<(), CliErro
453454// Returns a CliError if something fails
454455
455456fn download_file ( src : & str ) -> Result < ( ) , CliError > {
456- let output =
457- Command :: new ( "wget" )
458- . args ( [ src] )
459- . output ( )
460- . map_err ( |e| CliError :: InstallerError {
461- reason : e. to_string ( ) ,
462- } ) ?;
457+ download_file_with ( & RealCommandRunner , src)
458+ }
459+
460+ fn download_file_with ( runner : & dyn CommandRunner , src : & str ) -> Result < ( ) , CliError > {
461+ let args = [ src. to_string ( ) ] ;
462+ let output = runner
463+ . run ( "wget" , & args)
464+ . map_err ( |e| CliError :: InstallerError {
465+ reason : e. to_string ( ) ,
466+ } ) ?;
463467
464468 if !output. status . success ( ) {
465469 return Err ( CliError :: InstallerError {
@@ -486,6 +490,13 @@ fn download_file(src: &str) -> Result<(), CliError> {
486490// Returns an CliError if something fails
487491
488492fn rm_file ( file_to_remove : & str ) -> Result < ( ) , CliError > {
493+ // rm -f never fails for a missing file, so check existence first
494+ if !std:: path:: Path :: new ( file_to_remove) . exists ( ) {
495+ return Err ( CliError :: InstallerError {
496+ reason : format ! ( "File not found: {}" , file_to_remove) ,
497+ } ) ;
498+ }
499+
489500 let output = Command :: new ( "rm" )
490501 . args ( [ "-f" , file_to_remove] )
491502 . output ( )
@@ -508,3 +519,78 @@ fn rm_file(file_to_remove: &str) -> Result<(), CliError> {
508519 thread:: sleep ( Duration :: from_secs ( 2 ) ) ;
509520 Ok ( ( ) )
510521}
522+
523+ #[ cfg( test) ]
524+ mod tests {
525+ use super :: * ;
526+
527+ #[ test]
528+ fn test_rm_file ( ) {
529+ let file_name = "test_file.txt" ;
530+ std:: fs:: write ( file_name, "test content" ) . unwrap ( ) ;
531+ assert ! ( rm_file( file_name) . is_ok( ) ) ;
532+ assert ! ( !std:: path:: Path :: new( file_name) . exists( ) ) ;
533+ }
534+
535+ #[ test]
536+ fn test_download_file_with_success ( ) {
537+ let runner = crate :: command_runner:: test_support:: StubCommandRunner :: success ( "" ) ;
538+ assert ! ( download_file_with( & runner, "https://example.com/file.yaml" ) . is_ok( ) ) ;
539+ }
540+
541+ #[ test]
542+ fn test_download_file_with_command_failure ( ) {
543+ let runner = crate :: command_runner:: test_support:: StubCommandRunner :: failure ( "404 Not Found" ) ;
544+ assert ! ( download_file_with( & runner, "https://example.com/missing.yaml" ) . is_err( ) ) ;
545+ }
546+
547+ #[ test]
548+ fn test_download_file_with_io_error ( ) {
549+ let runner = crate :: command_runner:: test_support:: StubCommandRunner :: io_error ( ) ;
550+ assert ! ( download_file_with( & runner, "https://example.com/file.yaml" ) . is_err( ) ) ;
551+ }
552+
553+ #[ test]
554+ fn test_rm_file_failure ( ) {
555+ let file_name = "non_existent_file.txt" ;
556+ assert ! ( rm_file( file_name) . is_err( ) ) ;
557+ }
558+
559+ #[ test]
560+ fn test_rm_file_cleanup_only ( ) {
561+ let file_name = "test_file.txt" ;
562+ std:: fs:: write ( file_name, "test content" ) . unwrap ( ) ;
563+ // Clean up without removing
564+ rm_file ( file_name) . unwrap ( ) ;
565+ }
566+
567+ #[ test]
568+ fn test_rm_file_failure_cleanup ( ) {
569+ let file_name = "non_existent_file.txt" ;
570+ // Attempt to clean up a non-existent file
571+ assert ! ( rm_file( file_name) . is_err( ) ) ;
572+ }
573+
574+ #[ test]
575+ fn test_rm_file_success_cleanup ( ) {
576+ let file_name = "test_file.txt" ;
577+ std:: fs:: write ( file_name, "test content" ) . unwrap ( ) ;
578+ // Clean up a file that exists
579+ assert ! ( rm_file( file_name) . is_ok( ) ) ;
580+ }
581+
582+ #[ test]
583+ fn test_rm_file_success_cleanup_only ( ) {
584+ let file_name = "test_file.txt" ;
585+ std:: fs:: write ( file_name, "test content" ) . unwrap ( ) ;
586+ // Clean up a file that exists
587+ assert ! ( rm_file( file_name) . is_ok( ) ) ;
588+ }
589+
590+ #[ test]
591+ fn test_rm_file_failure_cleanup_only ( ) {
592+ let file_name = "non_existent_file.txt" ;
593+ // Attempt to clean up a non-existent file
594+ assert ! ( rm_file( file_name) . is_err( ) ) ;
595+ }
596+ }
0 commit comments