@@ -44,27 +44,30 @@ tmpdir.refresh();
4444// time, the callback should be invoked again with proper values in stat object
4545let fileExists = false ;
4646
47- fs . watchFile ( enoentFile , { interval : 0 } , common . mustCall ( function ( curr , prev ) {
48- if ( ! fileExists ) {
49- // If the file does not exist, all the fields should be zero and the date
50- // fields should be UNIX EPOCH time
51- assert . deepStrictEqual ( curr , expectedStatObject ) ;
52- assert . deepStrictEqual ( prev , expectedStatObject ) ;
53- // Create the file now, so that the callback will be called back once the
54- // event loop notices it.
55- fs . closeSync ( fs . openSync ( enoentFile , 'w' ) ) ;
56- fileExists = true ;
57- } else {
58- // If the ino (inode) value is greater than zero, it means that the file is
59- // present in the filesystem and it has a valid inode number.
60- assert ( curr . ino > 0 ) ;
61- // As the file just got created, previous ino value should be lesser than
62- // or equal to zero (non-existent file).
63- assert ( prev . ino <= 0 ) ;
64- // Stop watching the file
65- fs . unwatchFile ( enoentFile ) ;
66- }
67- } , 2 ) ) ;
47+ const watcher =
48+ fs . watchFile ( enoentFile , { interval : 0 } , common . mustCall ( ( curr , prev ) => {
49+ if ( ! fileExists ) {
50+ // If the file does not exist, all the fields should be zero and the date
51+ // fields should be UNIX EPOCH time
52+ assert . deepStrictEqual ( curr , expectedStatObject ) ;
53+ assert . deepStrictEqual ( prev , expectedStatObject ) ;
54+ // Create the file now, so that the callback will be called back once the
55+ // event loop notices it.
56+ fs . closeSync ( fs . openSync ( enoentFile , 'w' ) ) ;
57+ fileExists = true ;
58+ } else {
59+ // If the ino (inode) value is greater than zero, it means that the file
60+ // is present in the filesystem and it has a valid inode number.
61+ assert ( curr . ino > 0 ) ;
62+ // As the file just got created, previous ino value should be lesser than
63+ // or equal to zero (non-existent file).
64+ assert ( prev . ino <= 0 ) ;
65+ // Stop watching the file
66+ fs . unwatchFile ( enoentFile ) ;
67+ }
68+ } , 2 ) ) ;
69+
70+ watcher . start ( ) ; // should not crash
6871
6972// Watch events should callback with a filename on supported systems.
7073// Omitting AIX. It works but not reliably.
0 commit comments