@@ -618,9 +618,16 @@ impl Drop for FsEventWatcher {
618618
619619#[ cfg( test) ]
620620mod tests {
621+ use std:: time:: Duration ;
622+
621623 use crate :: ErrorKind ;
622624
623625 use super :: * ;
626+ use crate :: test:: * ;
627+
628+ fn watcher ( ) -> ( TestWatcher < FsEventWatcher > , Receiver ) {
629+ channel ( )
630+ }
624631
625632 #[ test]
626633 fn test_fsevent_watcher_drop ( ) {
@@ -686,4 +693,351 @@ mod tests {
686693 "actual: {unwatch_result:#?}"
687694 ) ;
688695 }
696+
697+ #[ test]
698+ fn create_file ( ) {
699+ let tmpdir = testdir ( ) ;
700+ let ( mut watcher, mut rx) = watcher ( ) ;
701+ watcher. watch_recursively ( & tmpdir) ;
702+
703+ let path = tmpdir. path ( ) . join ( "entry" ) ;
704+ std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
705+
706+ rx. wait_unordered ( [ expected ( path) . create_file ( ) ] ) ;
707+ }
708+
709+ #[ test]
710+ fn write_file ( ) {
711+ let tmpdir = testdir ( ) ;
712+
713+ let path = tmpdir. path ( ) . join ( "entry" ) ;
714+ std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
715+
716+ let ( mut watcher, mut rx) = watcher ( ) ;
717+
718+ watcher. watch_recursively ( & tmpdir) ;
719+
720+ std:: fs:: write ( & path, b"123" ) . expect ( "write" ) ;
721+
722+ rx. wait_unordered ( [ expected ( & path) . modify_data_content ( ) ] ) ;
723+ }
724+
725+ #[ test]
726+ fn chmod_file ( ) {
727+ let tmpdir = testdir ( ) ;
728+ let ( mut watcher, mut rx) = watcher ( ) ;
729+
730+ let path = tmpdir. path ( ) . join ( "entry" ) ;
731+ let file = std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
732+ let mut permissions = file. metadata ( ) . expect ( "metadata" ) . permissions ( ) ;
733+ permissions. set_readonly ( true ) ;
734+
735+ watcher. watch_recursively ( & tmpdir) ;
736+ file. set_permissions ( permissions) . expect ( "set_permissions" ) ;
737+
738+ rx. wait_unordered ( [ expected ( & path) . modify_meta_owner ( ) ] ) ;
739+ }
740+
741+ #[ test]
742+ fn rename_file ( ) {
743+ let tmpdir = testdir ( ) ;
744+ let ( mut watcher, mut rx) = watcher ( ) ;
745+
746+ let path = tmpdir. path ( ) . join ( "entry" ) ;
747+ std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
748+
749+ watcher. watch_recursively ( & tmpdir) ;
750+ let new_path = tmpdir. path ( ) . join ( "renamed" ) ;
751+
752+ std:: fs:: rename ( & path, & new_path) . expect ( "rename" ) ;
753+
754+ rx. wait_unordered ( [ expected ( path) . rename_any ( ) , expected ( new_path) . rename_any ( ) ] ) ;
755+ }
756+
757+ #[ test]
758+ fn delete_file ( ) {
759+ let tmpdir = testdir ( ) ;
760+ let ( mut watcher, mut rx) = watcher ( ) ;
761+ let file = tmpdir. path ( ) . join ( "file" ) ;
762+ std:: fs:: write ( & file, "" ) . expect ( "write" ) ;
763+
764+ watcher. watch_nonrecursively ( & tmpdir) ;
765+
766+ std:: fs:: remove_file ( & file) . expect ( "remove" ) ;
767+
768+ rx. wait_unordered ( [ expected ( & file) . remove_file ( ) ] ) ;
769+ }
770+
771+ #[ test]
772+ fn delete_self_file ( ) {
773+ let tmpdir = testdir ( ) ;
774+ let ( mut watcher, mut rx) = watcher ( ) ;
775+ let file = tmpdir. path ( ) . join ( "file" ) ;
776+ std:: fs:: write ( & file, "" ) . expect ( "write" ) ;
777+
778+ watcher. watch_nonrecursively ( & file) ;
779+
780+ std:: fs:: remove_file ( & file) . expect ( "remove" ) ;
781+
782+ rx. wait_unordered ( [ expected ( file) . remove_file ( ) ] ) ;
783+ }
784+
785+ #[ test]
786+ fn create_write_overwrite ( ) {
787+ let tmpdir = testdir ( ) ;
788+ let ( mut watcher, mut rx) = watcher ( ) ;
789+ let overwritten_file = tmpdir. path ( ) . join ( "overwritten_file" ) ;
790+ let overwriting_file = tmpdir. path ( ) . join ( "overwriting_file" ) ;
791+ std:: fs:: write ( & overwritten_file, "123" ) . expect ( "write1" ) ;
792+
793+ watcher. watch_nonrecursively ( & tmpdir) ;
794+
795+ std:: fs:: File :: create ( & overwriting_file) . expect ( "create" ) ;
796+ std:: fs:: write ( & overwriting_file, "321" ) . expect ( "write2" ) ;
797+ std:: fs:: rename ( & overwriting_file, & overwritten_file) . expect ( "rename" ) ;
798+
799+ rx. wait_unordered ( [
800+ expected ( & overwriting_file) . create ( ) ,
801+ expected ( & overwriting_file) . modify_data_content ( ) . multiple ( ) ,
802+ expected ( & overwriting_file) . rename_any ( ) ,
803+ expected ( & overwritten_file) . rename_any ( ) ,
804+ ] ) ;
805+ }
806+
807+ #[ test]
808+ fn create_dir ( ) {
809+ let tmpdir = testdir ( ) ;
810+ let ( mut watcher, mut rx) = watcher ( ) ;
811+ watcher. watch_recursively ( & tmpdir) ;
812+
813+ let path = tmpdir. path ( ) . join ( "entry" ) ;
814+ std:: fs:: create_dir ( & path) . expect ( "create" ) ;
815+
816+ rx. wait_unordered ( [ expected ( & path) . create_folder ( ) ] ) ;
817+ }
818+
819+ #[ test]
820+ fn chmod_dir ( ) {
821+ let tmpdir = testdir ( ) ;
822+ let ( mut watcher, mut rx) = watcher ( ) ;
823+
824+ let path = tmpdir. path ( ) . join ( "entry" ) ;
825+ std:: fs:: create_dir ( & path) . expect ( "create_dir" ) ;
826+ let mut permissions = std:: fs:: metadata ( & path) . expect ( "metadata" ) . permissions ( ) ;
827+ permissions. set_readonly ( true ) ;
828+
829+ watcher. watch_recursively ( & tmpdir) ;
830+ std:: fs:: set_permissions ( & path, permissions) . expect ( "set_permissions" ) ;
831+
832+ rx. wait_unordered ( [ expected ( & path) . modify_meta_owner ( ) ] ) ;
833+ }
834+
835+ #[ test]
836+ fn rename_dir ( ) {
837+ let tmpdir = testdir ( ) ;
838+ let ( mut watcher, mut rx) = watcher ( ) ;
839+
840+ let path = tmpdir. path ( ) . join ( "entry" ) ;
841+ let new_path = tmpdir. path ( ) . join ( "new_path" ) ;
842+ std:: fs:: create_dir ( & path) . expect ( "create_dir" ) ;
843+
844+ watcher. watch_recursively ( & tmpdir) ;
845+ std:: fs:: rename ( & path, & new_path) . expect ( "rename" ) ;
846+
847+ rx. wait_ordered ( [
848+ expected ( & path) . rename_any ( ) ,
849+ expected ( & new_path) . rename_any ( ) ,
850+ ] ) ;
851+ }
852+
853+ #[ test]
854+ fn delete_dir ( ) {
855+ let tmpdir = testdir ( ) ;
856+ let ( mut watcher, mut rx) = watcher ( ) ;
857+
858+ let path = tmpdir. path ( ) . join ( "entry" ) ;
859+ std:: fs:: create_dir ( & path) . expect ( "create_dir" ) ;
860+
861+ watcher. watch_recursively ( & tmpdir) ;
862+ std:: fs:: remove_dir ( & path) . expect ( "remove" ) ;
863+
864+ rx. wait_unordered ( [ expected ( path) . remove_folder ( ) ] ) ;
865+ }
866+
867+ #[ test]
868+ fn rename_dir_twice ( ) {
869+ let tmpdir = testdir ( ) ;
870+ let ( mut watcher, mut rx) = watcher ( ) ;
871+
872+ let path = tmpdir. path ( ) . join ( "entry" ) ;
873+ let new_path = tmpdir. path ( ) . join ( "new_path" ) ;
874+ let new_path2 = tmpdir. path ( ) . join ( "new_path2" ) ;
875+ std:: fs:: create_dir ( & path) . expect ( "create_dir" ) ;
876+
877+ watcher. watch_recursively ( & tmpdir) ;
878+ std:: fs:: rename ( & path, & new_path) . expect ( "rename" ) ;
879+ std:: fs:: rename ( & new_path, & new_path2) . expect ( "rename2" ) ;
880+
881+ rx. wait_unordered ( [
882+ expected ( & path) . rename_any ( ) ,
883+ expected ( & new_path) . rename_any ( ) ,
884+ expected ( & new_path2) . rename_any ( ) ,
885+ ] ) ;
886+ }
887+
888+ #[ test]
889+ fn move_out_of_watched_dir ( ) {
890+ let tmpdir = testdir ( ) ;
891+ let subdir = tmpdir. path ( ) . join ( "subdir" ) ;
892+ let ( mut watcher, mut rx) = watcher ( ) ;
893+
894+ let path = subdir. join ( "entry" ) ;
895+ std:: fs:: create_dir_all ( & subdir) . expect ( "create_dir_all" ) ;
896+ std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
897+
898+ watcher. watch_recursively ( & subdir) ;
899+ let new_path = tmpdir. path ( ) . join ( "entry" ) ;
900+
901+ std:: fs:: rename ( & path, & new_path) . expect ( "rename" ) ;
902+
903+ rx. wait_unordered ( [ expected ( path) . rename_any ( ) ] ) ;
904+ }
905+
906+ #[ test]
907+ #[ ignore = "https://github.com/notify-rs/notify/issues/729" ]
908+ fn create_write_write_rename_write_remove ( ) {
909+ let tmpdir = testdir ( ) ;
910+ let ( mut watcher, mut rx) = watcher ( ) ;
911+
912+ let file1 = tmpdir. path ( ) . join ( "entry" ) ;
913+ let file2 = tmpdir. path ( ) . join ( "entry2" ) ;
914+ std:: fs:: File :: create_new ( & file2) . expect ( "create file2" ) ;
915+ let new_path = tmpdir. path ( ) . join ( "renamed" ) ;
916+
917+ watcher. watch_recursively ( & tmpdir) ;
918+ std:: fs:: write ( & file1, "123" ) . expect ( "write 1" ) ;
919+ std:: fs:: write ( & file2, "321" ) . expect ( "write 2" ) ;
920+ std:: fs:: rename ( & file1, & new_path) . expect ( "rename" ) ;
921+ std:: fs:: write ( & new_path, b"1" ) . expect ( "write 3" ) ;
922+ std:: fs:: remove_file ( & new_path) . expect ( "remove" ) ;
923+
924+ rx. wait_ordered ( [
925+ expected ( & file1) . create_file ( ) ,
926+ expected ( & file1) . modify_data_content ( ) ,
927+ expected ( & file2) . modify_data_content ( ) ,
928+ expected ( & file1) . rename_any ( ) ,
929+ expected ( & new_path) . rename_any ( ) ,
930+ expected ( & new_path) . modify_data_content ( ) ,
931+ expected ( & new_path) . remove_file ( ) ,
932+ ] ) ;
933+ }
934+
935+ #[ test]
936+ fn rename_twice ( ) {
937+ let tmpdir = testdir ( ) ;
938+ let ( mut watcher, mut rx) = watcher ( ) ;
939+
940+ let path = tmpdir. path ( ) . join ( "entry" ) ;
941+ std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
942+
943+ watcher. watch_recursively ( & tmpdir) ;
944+ let new_path1 = tmpdir. path ( ) . join ( "renamed1" ) ;
945+ let new_path2 = tmpdir. path ( ) . join ( "renamed2" ) ;
946+
947+ std:: fs:: rename ( & path, & new_path1) . expect ( "rename1" ) ;
948+ std:: fs:: rename ( & new_path1, & new_path2) . expect ( "rename2" ) ;
949+
950+ rx. wait_unordered ( [
951+ expected ( & path) . rename_any ( ) ,
952+ expected ( & new_path1) . rename_any ( ) ,
953+ expected ( & new_path2) . rename_any ( ) ,
954+ ] ) ;
955+ }
956+
957+ #[ test]
958+ fn set_file_mtime ( ) {
959+ let tmpdir = testdir ( ) ;
960+ let ( mut watcher, mut rx) = watcher ( ) ;
961+
962+ let path = tmpdir. path ( ) . join ( "entry" ) ;
963+ let file = std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
964+
965+ watcher. watch_recursively ( & tmpdir) ;
966+
967+ file. set_modified (
968+ std:: time:: SystemTime :: now ( )
969+ . checked_sub ( Duration :: from_secs ( 60 * 60 ) )
970+ . expect ( "time" ) ,
971+ )
972+ . expect ( "set_time" ) ;
973+
974+ rx. wait_unordered ( [ expected ( & path) . modify_meta_any ( ) ] ) ;
975+ }
976+
977+ #[ test]
978+ fn write_file_non_recursive_watch ( ) {
979+ let tmpdir = testdir ( ) ;
980+ let ( mut watcher, mut rx) = watcher ( ) ;
981+
982+ let path = tmpdir. path ( ) . join ( "entry" ) ;
983+ std:: fs:: File :: create_new ( & path) . expect ( "create" ) ;
984+
985+ watcher. watch_nonrecursively ( & path) ;
986+
987+ std:: fs:: write ( & path, b"123" ) . expect ( "write" ) ;
988+
989+ rx. wait_unordered ( [ expected ( path) . modify_data_content ( ) ] ) ;
990+ }
991+
992+ #[ test]
993+ fn write_to_a_hardlink_pointed_to_the_watched_file_triggers_an_event ( ) {
994+ let tmpdir = testdir ( ) ;
995+ let ( mut watcher, mut rx) = watcher ( ) ;
996+
997+ let subdir = tmpdir. path ( ) . join ( "subdir" ) ;
998+ let file = subdir. join ( "file" ) ;
999+ let hardlink = tmpdir. path ( ) . join ( "hardlink" ) ;
1000+
1001+ std:: fs:: create_dir ( & subdir) . expect ( "create" ) ;
1002+ std:: fs:: write ( & file, "" ) . expect ( "file" ) ;
1003+ std:: fs:: hard_link ( & file, & hardlink) . expect ( "hardlink" ) ;
1004+
1005+ watcher. watch_nonrecursively ( & file) ;
1006+
1007+ std:: fs:: write ( & hardlink, "123123" ) . expect ( "write to the hard link" ) ;
1008+
1009+ rx. wait_unordered ( [ expected ( file) . modify_data_content ( ) ] ) ;
1010+ }
1011+
1012+ #[ test]
1013+ fn recursive_creation ( ) {
1014+ let tmpdir = testdir ( ) ;
1015+ let nested1 = tmpdir. path ( ) . join ( "1" ) ;
1016+ let nested2 = tmpdir. path ( ) . join ( "1/2" ) ;
1017+ let nested3 = tmpdir. path ( ) . join ( "1/2/3" ) ;
1018+ let nested4 = tmpdir. path ( ) . join ( "1/2/3/4" ) ;
1019+ let nested5 = tmpdir. path ( ) . join ( "1/2/3/4/5" ) ;
1020+ let nested6 = tmpdir. path ( ) . join ( "1/2/3/4/5/6" ) ;
1021+ let nested7 = tmpdir. path ( ) . join ( "1/2/3/4/5/6/7" ) ;
1022+ let nested8 = tmpdir. path ( ) . join ( "1/2/3/4/5/6/7/8" ) ;
1023+ let nested9 = tmpdir. path ( ) . join ( "1/2/3/4/5/6/7/8/9" ) ;
1024+
1025+ let ( mut watcher, mut rx) = watcher ( ) ;
1026+
1027+ watcher. watch_recursively ( & tmpdir) ;
1028+
1029+ std:: fs:: create_dir_all ( & nested9) . expect ( "create_dir_all" ) ;
1030+
1031+ rx. wait_ordered ( [
1032+ expected ( & nested1) . create_folder ( ) ,
1033+ expected ( & nested2) . create_folder ( ) ,
1034+ expected ( & nested3) . create_folder ( ) ,
1035+ expected ( & nested4) . create_folder ( ) ,
1036+ expected ( & nested5) . create_folder ( ) ,
1037+ expected ( & nested6) . create_folder ( ) ,
1038+ expected ( & nested7) . create_folder ( ) ,
1039+ expected ( & nested8) . create_folder ( ) ,
1040+ expected ( & nested9) . create_folder ( ) ,
1041+ ] ) ;
1042+ }
6891043}
0 commit comments