@@ -585,8 +585,11 @@ fn syntect_to_crossterm_color(syntect: syntect::highlighting::Color) -> style::C
585
585
586
586
#[ cfg( test) ]
587
587
mod tests {
588
+ use std:: path:: PathBuf ;
588
589
use std:: sync:: Arc ;
589
590
591
+ use lazy_static:: lazy_static;
592
+
590
593
use super :: * ;
591
594
592
595
const TEST_FILE_CONTENTS : & str = "\
@@ -596,8 +599,12 @@ mod tests {
596
599
4: Hello world!
597
600
" ;
598
601
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
+ }
601
608
602
609
/// Sets up the following filesystem structure:
603
610
/// ```text
@@ -612,10 +619,17 @@ mod tests {
612
619
async fn setup_test_directory ( ) -> Arc < Context > {
613
620
let ctx = Context :: builder ( ) . with_test_home ( ) . await . unwrap ( ) . build_fake ( ) ;
614
621
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
+
619
633
ctx
620
634
}
621
635
@@ -670,7 +684,7 @@ mod tests {
670
684
671
685
let file_text = "Hello, world!" ;
672
686
let v = serde_json:: json!( {
673
- "path" : "/ my-file" ,
687
+ "path" : "my-file" ,
674
688
"command" : "create" ,
675
689
"file_text" : file_text
676
690
} ) ;
@@ -681,13 +695,13 @@ mod tests {
681
695
. unwrap ( ) ;
682
696
683
697
assert_eq ! (
684
- ctx. fs( ) . read_to_string( "/ my-file" ) . await . unwrap( ) ,
698
+ ctx. fs( ) . read_to_string( "my-file" ) . await . unwrap( ) ,
685
699
format!( "{}\n " , file_text)
686
700
) ;
687
701
688
702
let file_text = "Goodbye, world!\n See you later" ;
689
703
let v = serde_json:: json!( {
690
- "path" : "/ my-file" ,
704
+ "path" : "my-file" ,
691
705
"command" : "create" ,
692
706
"file_text" : file_text
693
707
} ) ;
@@ -699,13 +713,13 @@ mod tests {
699
713
700
714
// File should end with a newline
701
715
assert_eq ! (
702
- ctx. fs( ) . read_to_string( "/ my-file" ) . await . unwrap( ) ,
716
+ ctx. fs( ) . read_to_string( "my-file" ) . await . unwrap( ) ,
703
717
format!( "{}\n " , file_text)
704
718
) ;
705
719
706
720
let file_text = "This is a new string" ;
707
721
let v = serde_json:: json!( {
708
- "path" : "/ my-file" ,
722
+ "path" : "my-file" ,
709
723
"command" : "create" ,
710
724
"new_str" : file_text
711
725
} ) ;
@@ -716,7 +730,7 @@ mod tests {
716
730
. unwrap ( ) ;
717
731
718
732
assert_eq ! (
719
- ctx. fs( ) . read_to_string( "/ my-file" ) . await . unwrap( ) ,
733
+ ctx. fs( ) . read_to_string( "my-file" ) . await . unwrap( ) ,
720
734
format!( "{}\n " , file_text)
721
735
) ;
722
736
}
@@ -728,7 +742,7 @@ mod tests {
728
742
729
743
// No instances found
730
744
let v = serde_json:: json!( {
731
- "path" : TEST_FILE_PATH ,
745
+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
732
746
"command" : "str_replace" ,
733
747
"old_str" : "asjidfopjaieopr" ,
734
748
"new_str" : "1623749" ,
@@ -743,7 +757,7 @@ mod tests {
743
757
744
758
// Multiple instances found
745
759
let v = serde_json:: json!( {
746
- "path" : TEST_FILE_PATH ,
760
+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
747
761
"command" : "str_replace" ,
748
762
"old_str" : "Hello world!" ,
749
763
"new_str" : "Goodbye world!" ,
@@ -758,7 +772,7 @@ mod tests {
758
772
759
773
// Single instance found and replaced
760
774
let v = serde_json:: json!( {
761
- "path" : TEST_FILE_PATH ,
775
+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
762
776
"command" : "str_replace" ,
763
777
"old_str" : "1: Hello world!" ,
764
778
"new_str" : "1: Goodbye world!" ,
@@ -770,7 +784,7 @@ mod tests {
770
784
. unwrap ( ) ;
771
785
assert_eq ! (
772
786
ctx. fs( )
773
- . read_to_string( TEST_FILE_PATH )
787
+ . read_to_string( TEST_FILE_PATH . as_path ( ) )
774
788
. await
775
789
. unwrap( )
776
790
. lines( )
@@ -788,7 +802,7 @@ mod tests {
788
802
789
803
let new_str = "1: New first line!\n " ;
790
804
let v = serde_json:: json!( {
791
- "path" : TEST_FILE_PATH ,
805
+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
792
806
"command" : "insert" ,
793
807
"insert_line" : 0 ,
794
808
"new_str" : new_str,
@@ -798,7 +812,7 @@ mod tests {
798
812
. invoke ( & ctx, & mut stdout)
799
813
. await
800
814
. 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 ( ) ;
802
816
assert_eq ! (
803
817
format!( "{}\n " , actual. lines( ) . next( ) . unwrap( ) ) ,
804
818
new_str,
@@ -819,7 +833,7 @@ mod tests {
819
833
820
834
let new_str = "2: New second line!\n " ;
821
835
let v = serde_json:: json!( {
822
- "path" : TEST_FILE_PATH ,
836
+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
823
837
"command" : "insert" ,
824
838
"insert_line" : 1 ,
825
839
"new_str" : new_str,
@@ -830,7 +844,7 @@ mod tests {
830
844
. invoke ( & ctx, & mut stdout)
831
845
. await
832
846
. 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 ( ) ;
834
848
assert_eq ! (
835
849
format!( "{}\n " , actual. lines( ) . nth( 1 ) . unwrap( ) ) ,
836
850
new_str,
@@ -849,7 +863,7 @@ mod tests {
849
863
let ctx = Context :: builder ( ) . with_test_home ( ) . await . unwrap ( ) . build_fake ( ) ;
850
864
let mut stdout = std:: io:: stdout ( ) ;
851
865
852
- let test_file_path = "/ file.txt" ;
866
+ let test_file_path = "file.txt" ;
853
867
let test_file_contents = "hello there" ;
854
868
ctx. fs ( ) . write ( test_file_path, test_file_contents) . await . unwrap ( ) ;
855
869
@@ -894,7 +908,7 @@ mod tests {
894
908
// Test appending to existing file
895
909
let content_to_append = "5: Appended line" ;
896
910
let v = serde_json:: json!( {
897
- "path" : TEST_FILE_PATH ,
911
+ "path" : TEST_FILE_PATH . to_str ( ) . unwrap_or_default ( ) ,
898
912
"command" : "append" ,
899
913
"new_str" : content_to_append,
900
914
} ) ;
@@ -905,15 +919,15 @@ mod tests {
905
919
. await
906
920
. unwrap ( ) ;
907
921
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 ( ) ;
909
923
assert_eq ! (
910
924
actual,
911
925
format!( "{}{}\n " , TEST_FILE_CONTENTS , content_to_append) ,
912
926
"Content should be appended to the end of the file with a newline added"
913
927
) ;
914
928
915
929
// 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" ;
917
931
let content = "This is a new file created by append" ;
918
932
let v = serde_json:: json!( {
919
933
"path" : new_file_path,
0 commit comments