@@ -11,6 +11,8 @@ use crate::{WatcherKind, event::*};
1111use std:: alloc;
1212use std:: cell:: RefCell ;
1313use std:: collections:: HashMap ;
14+ #[ cfg( test) ]
15+ use std:: collections:: HashSet ;
1416use std:: env;
1517use std:: ffi:: OsString ;
1618use std:: os:: raw:: c_void;
@@ -68,6 +70,8 @@ enum Action {
6870 Unwatch ( PathBuf ) ,
6971 Stop ,
7072 Configure ( Config , BoundSender < Result < bool > > ) ,
73+ #[ cfg( test) ]
74+ GetWatchHandles ( BoundSender < HashSet < PathBuf > > ) ,
7175}
7276
7377struct WatchState {
@@ -137,6 +141,11 @@ impl ReadDirectoryChangesServer {
137141 Action :: Configure ( config, tx) => {
138142 self . configure_raw_mode ( config, tx) ;
139143 }
144+ #[ cfg( test) ]
145+ Action :: GetWatchHandles ( tx) => {
146+ let handles = self . watch_handles . keys ( ) . cloned ( ) . collect ( ) ;
147+ tx. send ( handles) . unwrap ( ) ;
148+ }
140149 }
141150 }
142151
@@ -612,6 +621,13 @@ impl Watcher for ReadDirectoryChangesWatcher {
612621 fn kind ( ) -> crate :: WatcherKind {
613622 WatcherKind :: ReadDirectoryChangesWatcher
614623 }
624+
625+ #[ cfg( test) ]
626+ fn get_watch_handles ( & self ) -> HashSet < PathBuf > {
627+ let ( tx, rx) = bounded ( 1 ) ;
628+ self . tx . send ( Action :: GetWatchHandles ( tx) ) . unwrap ( ) ;
629+ rx. recv ( ) . unwrap ( )
630+ }
615631}
616632
617633impl Drop for ReadDirectoryChangesWatcher {
@@ -630,30 +646,36 @@ unsafe impl Sync for ReadDirectoryChangesWatcher {}
630646
631647#[ cfg( test) ]
632648pub mod tests {
633- use tempfile:: tempdir;
634-
635649 use crate :: { ReadDirectoryChangesWatcher , RecursiveMode , Watcher , test:: * } ;
636650
637- use std:: time:: Duration ;
651+ use std:: { collections :: HashSet , time:: Duration } ;
638652
639653 fn watcher ( ) -> ( TestWatcher < ReadDirectoryChangesWatcher > , Receiver ) {
640654 channel ( )
641655 }
642656
643657 #[ test]
644658 fn trash_dir ( ) -> std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > {
645- let dir = tempdir ( ) ? ;
659+ let dir = testdir ( ) ;
646660 let child_dir = dir. path ( ) . join ( "child" ) ;
647661 std:: fs:: create_dir ( & child_dir) ?;
648662
649663 let mut watcher = crate :: recommended_watcher ( |_| {
650664 // Do something with the event
651665 } ) ?;
652666 watcher. watch ( & child_dir, RecursiveMode :: NonRecursive ) ?;
667+ assert_eq ! (
668+ watcher. get_watch_handles( ) ,
669+ HashSet :: from( [ child_dir. clone( ) ] )
670+ ) ;
653671
654672 trash:: delete ( & child_dir) ?;
655673
656674 watcher. watch ( dir. path ( ) , RecursiveMode :: NonRecursive ) ?;
675+ assert_eq ! (
676+ watcher. get_watch_handles( ) ,
677+ HashSet :: from( [ dir. to_path_buf( ) ] )
678+ ) ;
657679
658680 Ok ( ( ) )
659681 }
@@ -675,6 +697,10 @@ pub mod tests {
675697
676698 rx. wait_ordered_exact ( [ expected ( & path) . create_any ( ) ] )
677699 . ensure_no_tail ( ) ;
700+ assert_eq ! (
701+ watcher. get_watch_handles( ) ,
702+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
703+ ) ;
678704 }
679705
680706 #[ test]
@@ -690,6 +716,10 @@ pub mod tests {
690716
691717 rx. wait_ordered_exact ( [ expected ( & path) . modify_any ( ) . multiple ( ) ] )
692718 . ensure_no_tail ( ) ;
719+ assert_eq ! (
720+ watcher. get_watch_handles( ) ,
721+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
722+ ) ;
693723 }
694724
695725 #[ test]
@@ -707,6 +737,10 @@ pub mod tests {
707737
708738 rx. wait_ordered_exact ( [ expected ( & path) . modify_any ( ) ] )
709739 . ensure_no_tail ( ) ;
740+ assert_eq ! (
741+ watcher. get_watch_handles( ) ,
742+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
743+ ) ;
710744 }
711745
712746 #[ test]
@@ -727,6 +761,10 @@ pub mod tests {
727761 expected ( & new_path) . rename_to ( ) ,
728762 ] )
729763 . ensure_no_tail ( ) ;
764+ assert_eq ! (
765+ watcher. get_watch_handles( ) ,
766+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
767+ ) ;
730768 }
731769
732770 #[ test]
@@ -742,6 +780,10 @@ pub mod tests {
742780
743781 rx. wait_ordered_exact ( [ expected ( & file) . remove_any ( ) ] )
744782 . ensure_no_tail ( ) ;
783+ assert_eq ! (
784+ watcher. get_watch_handles( ) ,
785+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
786+ ) ;
745787 }
746788
747789 #[ test]
@@ -756,6 +798,10 @@ pub mod tests {
756798 std:: fs:: remove_file ( & file) . expect ( "remove" ) ;
757799
758800 rx. wait_ordered_exact ( [ expected ( & file) . remove_any ( ) ] ) ;
801+ assert_eq ! (
802+ watcher. get_watch_handles( ) ,
803+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
804+ ) ;
759805 }
760806
761807 #[ test]
@@ -780,6 +826,10 @@ pub mod tests {
780826 expected ( & overwritten_file) . rename_to ( ) ,
781827 ] )
782828 . ensure_no_tail ( ) ;
829+ assert_eq ! (
830+ watcher. get_watch_handles( ) ,
831+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
832+ ) ;
783833 }
784834
785835 #[ test]
@@ -793,6 +843,10 @@ pub mod tests {
793843
794844 rx. wait_ordered_exact ( [ expected ( & path) . create_any ( ) ] )
795845 . ensure_no_tail ( ) ;
846+ assert_eq ! (
847+ watcher. get_watch_handles( ) ,
848+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
849+ ) ;
796850 }
797851
798852 #[ test]
@@ -810,6 +864,10 @@ pub mod tests {
810864
811865 rx. wait_ordered_exact ( [ expected ( & path) . modify_any ( ) ] )
812866 . ensure_no_tail ( ) ;
867+ assert_eq ! (
868+ watcher. get_watch_handles( ) ,
869+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
870+ ) ;
813871 }
814872
815873 #[ test]
@@ -830,6 +888,10 @@ pub mod tests {
830888 expected ( & new_path) . rename_to ( ) ,
831889 ] )
832890 . ensure_no_tail ( ) ;
891+ assert_eq ! (
892+ watcher. get_watch_handles( ) ,
893+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
894+ ) ;
833895 }
834896
835897 #[ test]
@@ -845,6 +907,10 @@ pub mod tests {
845907
846908 rx. wait_ordered_exact ( [ expected ( & path) . remove_any ( ) ] )
847909 . ensure_no_tail ( ) ;
910+ assert_eq ! (
911+ watcher. get_watch_handles( ) ,
912+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
913+ ) ;
848914 }
849915
850916 #[ test]
@@ -868,6 +934,10 @@ pub mod tests {
868934 expected ( & new_path2) . rename_to ( ) ,
869935 ] )
870936 . ensure_no_tail ( ) ;
937+ assert_eq ! (
938+ watcher. get_watch_handles( ) ,
939+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
940+ ) ;
871941 }
872942
873943 #[ test]
@@ -888,6 +958,7 @@ pub mod tests {
888958 let event = rx. recv ( ) ;
889959 assert_eq ! ( event, expected( path) . remove_any( ) ) ;
890960 rx. ensure_empty ( ) ;
961+ assert_eq ! ( watcher. get_watch_handles( ) , HashSet :: from( [ subdir] ) ) ;
891962 }
892963
893964 #[ test]
@@ -916,6 +987,10 @@ pub mod tests {
916987 expected ( & new_path) . modify_any ( ) . multiple ( ) ,
917988 expected ( & new_path) . remove_any ( ) ,
918989 ] ) ;
990+ assert_eq ! (
991+ watcher. get_watch_handles( ) ,
992+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
993+ ) ;
919994 }
920995
921996 #[ test]
@@ -940,6 +1015,10 @@ pub mod tests {
9401015 expected ( & new_path2) . rename_to ( ) ,
9411016 ] )
9421017 . ensure_no_tail ( ) ;
1018+ assert_eq ! (
1019+ watcher. get_watch_handles( ) ,
1020+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
1021+ ) ;
9431022 }
9441023
9451024 #[ test]
@@ -977,6 +1056,10 @@ pub mod tests {
9771056
9781057 rx. wait_ordered_exact ( [ expected ( & path) . modify_any ( ) . multiple ( ) ] )
9791058 . ensure_no_tail ( ) ;
1059+ assert_eq ! (
1060+ watcher. get_watch_handles( ) ,
1061+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
1062+ ) ;
9801063 }
9811064
9821065 #[ test]
@@ -998,6 +1081,10 @@ pub mod tests {
9981081
9991082 let events = rx. iter ( ) . collect :: < Vec < _ > > ( ) ;
10001083 assert ! ( events. is_empty( ) , "unexpected events: {events:#?}" ) ;
1084+ assert_eq ! (
1085+ watcher. get_watch_handles( ) ,
1086+ HashSet :: from( [ subdir. to_path_buf( ) ] )
1087+ ) ;
10011088 }
10021089
10031090 #[ test]
@@ -1030,6 +1117,10 @@ pub mod tests {
10301117 expected ( & nested9) . create_any ( ) ,
10311118 ] )
10321119 . ensure_no_tail ( ) ;
1120+ assert_eq ! (
1121+ watcher. get_watch_handles( ) ,
1122+ HashSet :: from( [ tmpdir. to_path_buf( ) ] )
1123+ ) ;
10331124 }
10341125
10351126 #[ test]
@@ -1051,5 +1142,6 @@ pub mod tests {
10511142
10521143 rx. wait_ordered_exact ( [ expected ( & deep) . modify_any ( ) , expected ( & file) . create_any ( ) ] )
10531144 . ensure_no_tail ( ) ;
1145+ assert_eq ! ( watcher. get_watch_handles( ) , HashSet :: from( [ path] ) ) ;
10541146 }
10551147}
0 commit comments