11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This test is a bit more complicated than it ideally needs to be to work
41cb0ef41Sopenharmony_ci// around issues on OS X and SmartOS.
51cb0ef41Sopenharmony_ci//
61cb0ef41Sopenharmony_ci// On OS X, watch events are subject to peculiar timing oddities such that an
71cb0ef41Sopenharmony_ci// event might fire out of order. The synchronous refreshing of the tmp
81cb0ef41Sopenharmony_ci// directory might trigger an event on the watchers that are instantiated after
91cb0ef41Sopenharmony_ci// it!
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// On SmartOS, the watch events fire but the filename is null.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst common = require('../common');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// fs-watch on folders have limited capability in AIX.
161cb0ef41Sopenharmony_ci// The testcase makes use of folder watching, and causes
171cb0ef41Sopenharmony_ci// hang. This behavior is documented. Skip this for AIX.
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciif (common.isAIX)
201cb0ef41Sopenharmony_ci  common.skip('folder watch capability is limited in AIX.');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst fs = require('fs');
231cb0ef41Sopenharmony_ciconst path = require('path');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
261cb0ef41Sopenharmony_citmpdir.refresh();
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst fn = '新建文夹件.txt';
291cb0ef41Sopenharmony_ciconst a = path.join(tmpdir.path, fn);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst watchers = new Set();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cifunction registerWatcher(watcher) {
341cb0ef41Sopenharmony_ci  watchers.add(watcher);
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cifunction unregisterWatcher(watcher) {
381cb0ef41Sopenharmony_ci  watcher.close();
391cb0ef41Sopenharmony_ci  watchers.delete(watcher);
401cb0ef41Sopenharmony_ci  if (watchers.size === 0) {
411cb0ef41Sopenharmony_ci    clearInterval(interval);
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci  // Test that using the `encoding` option has the expected result.
471cb0ef41Sopenharmony_ci  const watcher = fs.watch(
481cb0ef41Sopenharmony_ci    tmpdir.path,
491cb0ef41Sopenharmony_ci    { encoding: 'hex' },
501cb0ef41Sopenharmony_ci    (event, filename) => {
511cb0ef41Sopenharmony_ci      if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename))
521cb0ef41Sopenharmony_ci        done(watcher);
531cb0ef41Sopenharmony_ci    }
541cb0ef41Sopenharmony_ci  );
551cb0ef41Sopenharmony_ci  registerWatcher(watcher);
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  // Test that in absence of `encoding` option has the expected result.
601cb0ef41Sopenharmony_ci  const watcher = fs.watch(
611cb0ef41Sopenharmony_ci    tmpdir.path,
621cb0ef41Sopenharmony_ci    (event, filename) => {
631cb0ef41Sopenharmony_ci      if ([fn, null].includes(filename))
641cb0ef41Sopenharmony_ci        done(watcher);
651cb0ef41Sopenharmony_ci    }
661cb0ef41Sopenharmony_ci  );
671cb0ef41Sopenharmony_ci  registerWatcher(watcher);
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci{
711cb0ef41Sopenharmony_ci  // Test that using the `encoding` option has the expected result.
721cb0ef41Sopenharmony_ci  const watcher = fs.watch(
731cb0ef41Sopenharmony_ci    tmpdir.path,
741cb0ef41Sopenharmony_ci    { encoding: 'buffer' },
751cb0ef41Sopenharmony_ci    (event, filename) => {
761cb0ef41Sopenharmony_ci      if (filename instanceof Buffer && filename.toString('utf8') === fn)
771cb0ef41Sopenharmony_ci        done(watcher);
781cb0ef41Sopenharmony_ci      else if (filename === null)
791cb0ef41Sopenharmony_ci        done(watcher);
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci  );
821cb0ef41Sopenharmony_ci  registerWatcher(watcher);
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciconst done = common.mustCall(unregisterWatcher, watchers.size);
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci// OS X and perhaps other systems can have surprising race conditions with
881cb0ef41Sopenharmony_ci// file events. So repeat the operation in case it is missed the first time.
891cb0ef41Sopenharmony_ciconst interval = setInterval(() => {
901cb0ef41Sopenharmony_ci  const fd = fs.openSync(a, 'w+');
911cb0ef41Sopenharmony_ci  fs.closeSync(fd);
921cb0ef41Sopenharmony_ci  fs.unlinkSync(a);
931cb0ef41Sopenharmony_ci}, common.platformTimeout(100));
94