1'use strict'; 2 3// This test is a bit more complicated than it ideally needs to be to work 4// around issues on OS X and SmartOS. 5// 6// On OS X, watch events are subject to peculiar timing oddities such that an 7// event might fire out of order. The synchronous refreshing of the tmp 8// directory might trigger an event on the watchers that are instantiated after 9// it! 10// 11// On SmartOS, the watch events fire but the filename is null. 12 13const common = require('../common'); 14 15// fs-watch on folders have limited capability in AIX. 16// The testcase makes use of folder watching, and causes 17// hang. This behavior is documented. Skip this for AIX. 18 19if (common.isAIX) 20 common.skip('folder watch capability is limited in AIX.'); 21 22const fs = require('fs'); 23const path = require('path'); 24 25const tmpdir = require('../common/tmpdir'); 26tmpdir.refresh(); 27 28const fn = '新建文夹件.txt'; 29const a = path.join(tmpdir.path, fn); 30 31const watchers = new Set(); 32 33function registerWatcher(watcher) { 34 watchers.add(watcher); 35} 36 37function unregisterWatcher(watcher) { 38 watcher.close(); 39 watchers.delete(watcher); 40 if (watchers.size === 0) { 41 clearInterval(interval); 42 } 43} 44 45{ 46 // Test that using the `encoding` option has the expected result. 47 const watcher = fs.watch( 48 tmpdir.path, 49 { encoding: 'hex' }, 50 (event, filename) => { 51 if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename)) 52 done(watcher); 53 } 54 ); 55 registerWatcher(watcher); 56} 57 58{ 59 // Test that in absence of `encoding` option has the expected result. 60 const watcher = fs.watch( 61 tmpdir.path, 62 (event, filename) => { 63 if ([fn, null].includes(filename)) 64 done(watcher); 65 } 66 ); 67 registerWatcher(watcher); 68} 69 70{ 71 // Test that using the `encoding` option has the expected result. 72 const watcher = fs.watch( 73 tmpdir.path, 74 { encoding: 'buffer' }, 75 (event, filename) => { 76 if (filename instanceof Buffer && filename.toString('utf8') === fn) 77 done(watcher); 78 else if (filename === null) 79 done(watcher); 80 } 81 ); 82 registerWatcher(watcher); 83} 84 85const done = common.mustCall(unregisterWatcher, watchers.size); 86 87// OS X and perhaps other systems can have surprising race conditions with 88// file events. So repeat the operation in case it is missed the first time. 89const interval = setInterval(() => { 90 const fd = fs.openSync(a, 'w+'); 91 fs.closeSync(fd); 92 fs.unlinkSync(a); 93}, common.platformTimeout(100)); 94