@@ -596,38 +596,11 @@ fn syntect_to_crossterm_color(syntect: syntect::highlighting::Color) -> style::C
596
596
#[ cfg( test) ]
597
597
mod tests {
598
598
use super :: * ;
599
-
600
- const TEST_FILE_CONTENTS : & str = "\
601
- 1: Hello world!
602
- 2: This is line 2
603
- 3: asdf
604
- 4: Hello world!
605
- " ;
606
-
607
- const TEST_FILE_PATH : & str = "/test_file.txt" ;
608
- const TEST_HIDDEN_FILE_PATH : & str = "/aaaa2/.hidden" ;
609
-
610
- /// Sets up the following filesystem structure:
611
- /// ```text
612
- /// test_file.txt
613
- /// /home/testuser/
614
- /// /aaaa1/
615
- /// /bbbb1/
616
- /// /cccc1/
617
- /// /aaaa2/
618
- /// .hidden
619
- /// ```
620
- async fn setup_test_directory ( ) -> Context {
621
- let ctx = Context :: new ( ) ;
622
- ctx. fs . write ( TEST_FILE_PATH , TEST_FILE_CONTENTS ) . await . unwrap ( ) ;
623
- ctx. fs . create_dir_all ( "/aaaa1/bbbb1/cccc1" ) . await . unwrap ( ) ;
624
- ctx. fs . create_dir_all ( "/aaaa2" ) . await . unwrap ( ) ;
625
- ctx. fs
626
- . write ( TEST_HIDDEN_FILE_PATH , "this is a hidden file" )
627
- . await
628
- . unwrap ( ) ;
629
- ctx
630
- }
599
+ use crate :: cli:: chat:: util:: test:: {
600
+ TEST_FILE_CONTENTS ,
601
+ TEST_FILE_PATH ,
602
+ setup_test_directory,
603
+ } ;
631
604
632
605
#[ test]
633
606
fn test_fs_write_deserialize ( ) {
@@ -960,74 +933,75 @@ mod tests {
960
933
assert_eq ! ( terminal_width_required_for_line_count( 100 ) , 3 ) ;
961
934
assert_eq ! ( terminal_width_required_for_line_count( 999 ) , 3 ) ;
962
935
}
963
- }
964
- #[ tokio:: test]
965
- async fn test_fs_write_with_tilde_paths ( ) {
966
- // Create a test context
967
- let ctx = Context :: builder ( ) . with_test_home ( ) . await . unwrap ( ) . build_fake ( ) ;
968
- let mut stdout = std:: io:: stdout ( ) ;
969
-
970
- // Get the home directory from the context
971
- let home_dir = ctx. env ( ) . home ( ) . unwrap_or_default ( ) ;
972
- println ! ( "Test home directory: {:?}" , home_dir) ;
973
-
974
- // Create a file directly in the home directory first to ensure it exists
975
- let home_path = ctx. fs ( ) . chroot_path ( & home_dir) ;
976
- println ! ( "Chrooted home path: {:?}" , home_path) ;
977
-
978
- // Ensure the home directory exists
979
- ctx. fs ( ) . create_dir_all ( & home_path) . await . unwrap ( ) ;
980
-
981
- let v = serde_json:: json!( {
982
- "path" : "~/file.txt" ,
983
- "command" : "create" ,
984
- "file_text" : "content in home file"
985
- } ) ;
986
-
987
- let result = serde_json:: from_value :: < FsWrite > ( v)
988
- . unwrap ( )
989
- . invoke ( & ctx, & mut stdout)
990
- . await ;
991
-
992
- match & result {
993
- Ok ( _) => println ! ( "Writing to ~/file.txt succeeded" ) ,
994
- Err ( e) => println ! ( "Writing to ~/file.txt failed: {:?}" , e) ,
995
- }
996
936
997
- assert ! ( result. is_ok( ) , "Writing to ~/file.txt should succeed" ) ;
937
+ #[ tokio:: test]
938
+ async fn test_fs_write_with_tilde_paths ( ) {
939
+ // Create a test context
940
+ let ctx = Context :: new ( ) ;
941
+ let mut stdout = std:: io:: stdout ( ) ;
942
+
943
+ // Get the home directory from the context
944
+ let home_dir = ctx. env . home ( ) . unwrap_or_default ( ) ;
945
+ println ! ( "Test home directory: {:?}" , home_dir) ;
998
946
999
- // Verify content was written correctly
1000
- let file_path = home_path . join ( "file.txt" ) ;
1001
- println ! ( "Checking file at : {:?}" , file_path ) ;
947
+ // Create a file directly in the home directory first to ensure it exists
948
+ let home_path = ctx . fs . chroot_path ( & home_dir ) ;
949
+ println ! ( "Chrooted home path : {:?}" , home_path ) ;
1002
950
1003
- let content_result = ctx. fs ( ) . read_to_string ( & file_path) . await ;
1004
- match & content_result {
1005
- Ok ( content) => println ! ( "Read content: {:?}" , content) ,
1006
- Err ( e) => println ! ( "Failed to read content: {:?}" , e) ,
1007
- }
951
+ // Ensure the home directory exists
952
+ ctx. fs . create_dir_all ( & home_path) . await . unwrap ( ) ;
953
+
954
+ let v = serde_json:: json!( {
955
+ "path" : "~/file.txt" ,
956
+ "command" : "create" ,
957
+ "file_text" : "content in home file"
958
+ } ) ;
959
+
960
+ let result = serde_json:: from_value :: < FsWrite > ( v)
961
+ . unwrap ( )
962
+ . invoke ( & ctx, & mut stdout)
963
+ . await ;
964
+
965
+ match & result {
966
+ Ok ( _) => println ! ( "Writing to ~/file.txt succeeded" ) ,
967
+ Err ( e) => println ! ( "Writing to ~/file.txt failed: {:?}" , e) ,
968
+ }
969
+
970
+ assert ! ( result. is_ok( ) , "Writing to ~/file.txt should succeed" ) ;
971
+
972
+ // Verify content was written correctly
973
+ let file_path = home_path. join ( "file.txt" ) ;
974
+ println ! ( "Checking file at: {:?}" , file_path) ;
975
+
976
+ let content_result = ctx. fs . read_to_string ( & file_path) . await ;
977
+ match & content_result {
978
+ Ok ( content) => println ! ( "Read content: {:?}" , content) ,
979
+ Err ( e) => println ! ( "Failed to read content: {:?}" , e) ,
980
+ }
1008
981
1009
- assert ! ( content_result. is_ok( ) , "Should be able to read from expanded path" ) ;
1010
- assert_eq ! ( content_result. unwrap( ) , "content in home file\n " ) ;
982
+ assert ! ( content_result. is_ok( ) , "Should be able to read from expanded path" ) ;
983
+ assert_eq ! ( content_result. unwrap( ) , "content in home file\n " ) ;
1011
984
1012
- // Test with "~/nested/path/file.txt" to ensure deep paths work
1013
- let nested_dir = home_path. join ( "nested" ) . join ( "path" ) ;
1014
- ctx. fs ( ) . create_dir_all ( & nested_dir) . await . unwrap ( ) ;
985
+ // Test with "~/nested/path/file.txt" to ensure deep paths work
986
+ let nested_dir = home_path. join ( "nested" ) . join ( "path" ) ;
987
+ ctx. fs . create_dir_all ( & nested_dir) . await . unwrap ( ) ;
1015
988
1016
- let v = serde_json:: json!( {
1017
- "path" : "~/nested/path/file.txt" ,
1018
- "command" : "create" ,
1019
- "file_text" : "content in nested path"
1020
- } ) ;
989
+ let v = serde_json:: json!( {
990
+ "path" : "~/nested/path/file.txt" ,
991
+ "command" : "create" ,
992
+ "file_text" : "content in nested path"
993
+ } ) ;
1021
994
1022
- let result = serde_json:: from_value :: < FsWrite > ( v)
1023
- . unwrap ( )
1024
- . invoke ( & ctx, & mut stdout)
1025
- . await ;
995
+ let result = serde_json:: from_value :: < FsWrite > ( v)
996
+ . unwrap ( )
997
+ . invoke ( & ctx, & mut stdout)
998
+ . await ;
1026
999
1027
- assert ! ( result. is_ok( ) , "Writing to ~/nested/path/file.txt should succeed" ) ;
1000
+ assert ! ( result. is_ok( ) , "Writing to ~/nested/path/file.txt should succeed" ) ;
1028
1001
1029
- // Verify nested path content
1030
- let nested_file_path = nested_dir. join ( "file.txt" ) ;
1031
- let nested_content = ctx. fs ( ) . read_to_string ( & nested_file_path) . await . unwrap ( ) ;
1032
- assert_eq ! ( nested_content, "content in nested path\n " ) ;
1002
+ // Verify nested path content
1003
+ let nested_file_path = nested_dir. join ( "file.txt" ) ;
1004
+ let nested_content = ctx. fs . read_to_string ( & nested_file_path) . await . unwrap ( ) ;
1005
+ assert_eq ! ( nested_content, "content in nested path\n " ) ;
1006
+ }
1033
1007
}
0 commit comments