@@ -585,8 +585,11 @@ fn syntect_to_crossterm_color(syntect: syntect::highlighting::Color) -> style::C
585585
586586#[ cfg( test) ]
587587mod tests {
588+ use std:: path:: PathBuf ;
588589 use std:: sync:: Arc ;
589590
591+ use lazy_static:: lazy_static;
592+
590593 use super :: * ;
591594
592595 const TEST_FILE_CONTENTS : & str = "\
@@ -596,8 +599,12 @@ mod tests {
5965994: Hello world!
597600" ;
598601
599- const TEST_FILE_PATH : & str = "/test_file.txt" ;
600- const TEST_HIDDEN_FILE_PATH : & str = "/aaaa2/.hidden" ;
602+ // Use platform-agnostic paths with lazy_static
603+ lazy_static ! {
604+ // Use relative paths without leading slashes for cross-platform compatibility
605+ static ref TEST_FILE_PATH : PathBuf = PathBuf :: from( "test_file.txt" ) ;
606+ static ref TEST_HIDDEN_FILE_PATH : PathBuf = PathBuf :: from( "aaaa2" ) . join( ".hidden" ) ;
607+ }
601608
602609 /// Sets up the following filesystem structure:
603610 /// ```text
@@ -612,10 +619,17 @@ mod tests {
612619 async fn setup_test_directory ( ) -> Arc < Context > {
613620 let ctx = Context :: builder ( ) . with_test_home ( ) . await . unwrap ( ) . build_fake ( ) ;
614621 let fs = ctx. fs ( ) ;
615- fs. write ( TEST_FILE_PATH , TEST_FILE_CONTENTS ) . await . unwrap ( ) ;
616- fs. create_dir_all ( "/aaaa1/bbbb1/cccc1" ) . await . unwrap ( ) ;
617- fs. create_dir_all ( "/aaaa2" ) . await . unwrap ( ) ;
618- fs. write ( TEST_HIDDEN_FILE_PATH , "this is a hidden file" ) . await . unwrap ( ) ;
622+
623+ // Use platform-agnostic paths
624+ fs. write ( TEST_FILE_PATH . as_path ( ) , TEST_FILE_CONTENTS ) . await . unwrap ( ) ;
625+ fs. create_dir_all ( PathBuf :: from ( "aaaa1" ) . join ( "bbbb1" ) . join ( "cccc1" ) )
626+ . await
627+ . unwrap ( ) ;
628+ fs. create_dir_all ( PathBuf :: from ( "aaaa2" ) ) . await . unwrap ( ) ;
629+ fs. write ( TEST_HIDDEN_FILE_PATH . as_path ( ) , "this is a hidden file" )
630+ . await
631+ . unwrap ( ) ;
632+
619633 ctx
620634 }
621635
@@ -670,7 +684,7 @@ mod tests {
670684
671685 let file_text = "Hello, world!" ;
672686 let v = serde_json:: json!( {
673- "path" : "/ my-file" ,
687+ "path" : "my-file" ,
674688 "command" : "create" ,
675689 "file_text" : file_text
676690 } ) ;
@@ -681,13 +695,13 @@ mod tests {
681695 . unwrap ( ) ;
682696
683697 assert_eq ! (
684- ctx. fs( ) . read_to_string( "/ my-file" ) . await . unwrap( ) ,
698+ ctx. fs( ) . read_to_string( "my-file" ) . await . unwrap( ) ,
685699 format!( "{}\n " , file_text)
686700 ) ;
687701
688702 let file_text = "Goodbye, world!\n See you later" ;
689703 let v = serde_json:: json!( {
690- "path" : "/ my-file" ,
704+ "path" : "my-file" ,
691705 "command" : "create" ,
692706 "file_text" : file_text
693707 } ) ;
@@ -699,13 +713,13 @@ mod tests {
699713
700714 // File should end with a newline
701715 assert_eq ! (
702- ctx. fs( ) . read_to_string( "/ my-file" ) . await . unwrap( ) ,
716+ ctx. fs( ) . read_to_string( "my-file" ) . await . unwrap( ) ,
703717 format!( "{}\n " , file_text)
704718 ) ;
705719
706720 let file_text = "This is a new string" ;
707721 let v = serde_json:: json!( {
708- "path" : "/ my-file" ,
722+ "path" : "my-file" ,
709723 "command" : "create" ,
710724 "new_str" : file_text
711725 } ) ;
@@ -716,7 +730,7 @@ mod tests {
716730 . unwrap ( ) ;
717731
718732 assert_eq ! (
719- ctx. fs( ) . read_to_string( "/ my-file" ) . await . unwrap( ) ,
733+ ctx. fs( ) . read_to_string( "my-file" ) . await . unwrap( ) ,
720734 format!( "{}\n " , file_text)
721735 ) ;
722736 }
@@ -728,7 +742,7 @@ mod tests {
728742
729743 // No instances found
730744 let v = serde_json:: json!( {
731- "path" : TEST_FILE_PATH ,
745+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
732746 "command" : "str_replace" ,
733747 "old_str" : "asjidfopjaieopr" ,
734748 "new_str" : "1623749" ,
@@ -743,7 +757,7 @@ mod tests {
743757
744758 // Multiple instances found
745759 let v = serde_json:: json!( {
746- "path" : TEST_FILE_PATH ,
760+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
747761 "command" : "str_replace" ,
748762 "old_str" : "Hello world!" ,
749763 "new_str" : "Goodbye world!" ,
@@ -758,7 +772,7 @@ mod tests {
758772
759773 // Single instance found and replaced
760774 let v = serde_json:: json!( {
761- "path" : TEST_FILE_PATH ,
775+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
762776 "command" : "str_replace" ,
763777 "old_str" : "1: Hello world!" ,
764778 "new_str" : "1: Goodbye world!" ,
@@ -770,7 +784,7 @@ mod tests {
770784 . unwrap ( ) ;
771785 assert_eq ! (
772786 ctx. fs( )
773- . read_to_string( TEST_FILE_PATH )
787+ . read_to_string( TEST_FILE_PATH . as_path ( ) )
774788 . await
775789 . unwrap( )
776790 . lines( )
@@ -788,7 +802,7 @@ mod tests {
788802
789803 let new_str = "1: New first line!\n " ;
790804 let v = serde_json:: json!( {
791- "path" : TEST_FILE_PATH ,
805+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
792806 "command" : "insert" ,
793807 "insert_line" : 0 ,
794808 "new_str" : new_str,
@@ -798,7 +812,7 @@ mod tests {
798812 . invoke ( & ctx, & mut stdout)
799813 . await
800814 . unwrap ( ) ;
801- let actual = ctx. fs ( ) . read_to_string ( TEST_FILE_PATH ) . await . unwrap ( ) ;
815+ let actual = ctx. fs ( ) . read_to_string ( TEST_FILE_PATH . as_path ( ) ) . await . unwrap ( ) ;
802816 assert_eq ! (
803817 format!( "{}\n " , actual. lines( ) . next( ) . unwrap( ) ) ,
804818 new_str,
@@ -819,7 +833,7 @@ mod tests {
819833
820834 let new_str = "2: New second line!\n " ;
821835 let v = serde_json:: json!( {
822- "path" : TEST_FILE_PATH ,
836+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
823837 "command" : "insert" ,
824838 "insert_line" : 1 ,
825839 "new_str" : new_str,
@@ -830,7 +844,7 @@ mod tests {
830844 . invoke ( & ctx, & mut stdout)
831845 . await
832846 . unwrap ( ) ;
833- let actual = ctx. fs ( ) . read_to_string ( TEST_FILE_PATH ) . await . unwrap ( ) ;
847+ let actual = ctx. fs ( ) . read_to_string ( TEST_FILE_PATH . as_path ( ) ) . await . unwrap ( ) ;
834848 assert_eq ! (
835849 format!( "{}\n " , actual. lines( ) . nth( 1 ) . unwrap( ) ) ,
836850 new_str,
@@ -849,7 +863,7 @@ mod tests {
849863 let ctx = Context :: builder ( ) . with_test_home ( ) . await . unwrap ( ) . build_fake ( ) ;
850864 let mut stdout = std:: io:: stdout ( ) ;
851865
852- let test_file_path = "/ file.txt" ;
866+ let test_file_path = "file.txt" ;
853867 let test_file_contents = "hello there" ;
854868 ctx. fs ( ) . write ( test_file_path, test_file_contents) . await . unwrap ( ) ;
855869
@@ -894,7 +908,7 @@ mod tests {
894908 // Test appending to existing file
895909 let content_to_append = "5: Appended line" ;
896910 let v = serde_json:: json!( {
897- "path" : TEST_FILE_PATH ,
911+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
898912 "command" : "append" ,
899913 "new_str" : content_to_append,
900914 } ) ;
@@ -905,15 +919,15 @@ mod tests {
905919 . await
906920 . unwrap ( ) ;
907921
908- let actual = ctx. fs ( ) . read_to_string ( TEST_FILE_PATH ) . await . unwrap ( ) ;
922+ let actual = ctx. fs ( ) . read_to_string ( TEST_FILE_PATH . as_path ( ) ) . await . unwrap ( ) ;
909923 assert_eq ! (
910924 actual,
911925 format!( "{}{}\n " , TEST_FILE_CONTENTS , content_to_append) ,
912926 "Content should be appended to the end of the file with a newline added"
913927 ) ;
914928
915929 // Test appending to non-existent file (should fail)
916- let new_file_path = "/ new_append_file.txt" ;
930+ let new_file_path = "new_append_file.txt" ;
917931 let content = "This is a new file created by append" ;
918932 let v = serde_json:: json!( {
919933 "path" : new_file_path,
0 commit comments