@@ -103,6 +103,8 @@ pub fn delete(args: &DeleteArgs) -> Result<(), ErrorKind> {
103
103
mod tests {
104
104
use super :: * ;
105
105
use std:: io:: Cursor ;
106
+ use std:: io:: Write ;
107
+ use tempfile:: NamedTempFile ;
106
108
107
109
// Helper function to capture stdout
108
110
// TODO: use more efficient method
@@ -482,4 +484,105 @@ mod tests {
482
484
_ => panic ! ( "Unexpected error type" ) ,
483
485
}
484
486
}
487
+
488
+ #[ test]
489
+ fn test_load_valid_env_file ( ) {
490
+ let mut temp_file = NamedTempFile :: new ( ) . unwrap ( ) ;
491
+ writeln ! ( temp_file, "TEST_VAR=test_value\n OTHER_VAR=other_value" ) . unwrap ( ) ;
492
+
493
+ let args = LoadArgs {
494
+ file : temp_file. path ( ) . to_string_lossy ( ) . to_string ( ) ,
495
+ global : false ,
496
+ process : None ,
497
+ } ;
498
+
499
+ let result = load ( & args) ;
500
+ assert ! ( result. is_ok( ) ) ;
501
+ assert_eq ! ( env:: var( "TEST_VAR" ) . unwrap( ) , "test_value" ) ;
502
+ assert_eq ! ( env:: var( "OTHER_VAR" ) . unwrap( ) , "other_value" ) ;
503
+
504
+ env:: remove_var ( "TEST_VAR" ) ;
505
+ env:: remove_var ( "OTHER_VAR" ) ;
506
+ }
507
+
508
+ #[ test]
509
+ fn test_load_nonexistent_file ( ) {
510
+ let args = LoadArgs {
511
+ file : "nonexistent.env" . to_string ( ) ,
512
+ global : false ,
513
+ process : None ,
514
+ } ;
515
+
516
+ let result = load ( & args) ;
517
+ assert ! ( result. is_err( ) ) ;
518
+ assert ! ( matches!( result. unwrap_err( ) , ErrorKind :: FileError ( _) ) ) ;
519
+ }
520
+
521
+ #[ test]
522
+ fn test_load_invalid_env_file ( ) {
523
+ let mut temp_file = NamedTempFile :: new ( ) . unwrap ( ) ;
524
+ // Using invalid .env format that dotenv_parser will reject
525
+ writeln ! ( temp_file, "TEST_VAR test_value" ) . unwrap ( ) ;
526
+
527
+ let args = LoadArgs {
528
+ file : temp_file. path ( ) . to_string_lossy ( ) . to_string ( ) ,
529
+ global : false ,
530
+ process : None ,
531
+ } ;
532
+
533
+ let result = load ( & args) ;
534
+ assert ! ( result. is_err( ) ) ;
535
+ assert ! ( matches!( result. unwrap_err( ) , ErrorKind :: ParsingError ( _) ) ) ;
536
+ }
537
+
538
+ #[ test]
539
+ fn test_load_with_process ( ) {
540
+ let mut temp_file = NamedTempFile :: new ( ) . unwrap ( ) ;
541
+ writeln ! ( temp_file, "TEST_PROCESS_VAR=process_value" ) . unwrap ( ) ;
542
+
543
+ #[ cfg( windows) ]
544
+ let cmd = "cmd /C echo test" ; // Simple echo command for Windows
545
+ #[ cfg( not( windows) ) ]
546
+ let cmd = "echo test" ; // Simple echo command for Unix
547
+
548
+ let args = LoadArgs {
549
+ file : temp_file. path ( ) . to_string_lossy ( ) . to_string ( ) ,
550
+ global : false ,
551
+ process : Some ( cmd. to_string ( ) ) ,
552
+ } ;
553
+
554
+ // First verify the variable is set correctly
555
+ let result = load ( & args) ;
556
+ assert ! ( result. is_ok( ) , "Load operation failed: {:?}" , result) ;
557
+ }
558
+
559
+ #[ test]
560
+ fn test_load_empty_file ( ) {
561
+ let temp_file = NamedTempFile :: new ( ) . unwrap ( ) ;
562
+
563
+ let args = LoadArgs {
564
+ file : temp_file. path ( ) . to_string_lossy ( ) . to_string ( ) ,
565
+ global : false ,
566
+ process : None ,
567
+ } ;
568
+
569
+ let result = load ( & args) ;
570
+ assert ! ( result. is_ok( ) ) ;
571
+ }
572
+
573
+ #[ test]
574
+ fn test_load_with_invalid_variable_name ( ) {
575
+ let mut temp_file = NamedTempFile :: new ( ) . unwrap ( ) ;
576
+ writeln ! ( temp_file, "TEST_VAR=test_value\n INVALID NAME=value" ) . unwrap ( ) ;
577
+
578
+ let args = LoadArgs {
579
+ file : temp_file. path ( ) . to_string_lossy ( ) . to_string ( ) ,
580
+ global : false ,
581
+ process : None ,
582
+ } ;
583
+
584
+ let result = load ( & args) ;
585
+ assert ! ( result. is_err( ) ) ;
586
+ assert ! ( matches!( result. unwrap_err( ) , ErrorKind :: ParsingError ( _) ) ) ;
587
+ }
485
588
}
0 commit comments