11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst path = require('path');
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst testDir = tmpdir.path;
131cb0ef41Sopenharmony_ciconst filenameOne = 'watch.txt';
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citmpdir.refresh();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst testsubdir = fs.mkdtempSync(testDir + path.sep);
181cb0ef41Sopenharmony_ciconst relativePathOne = path.join(path.basename(testsubdir), filenameOne);
191cb0ef41Sopenharmony_ciconst filepathOne = path.join(testsubdir, filenameOne);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciif (!common.isOSX && !common.isWindows) {
221cb0ef41Sopenharmony_ci  assert.throws(() => { fs.watch(testDir, { recursive: true }); },
231cb0ef41Sopenharmony_ci                { code: 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM' });
241cb0ef41Sopenharmony_ci  return;
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ciconst watcher = fs.watch(testDir, { recursive: true });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cilet watcherClosed = false;
291cb0ef41Sopenharmony_ciwatcher.on('change', function(event, filename) {
301cb0ef41Sopenharmony_ci  assert.ok(event === 'change' || event === 'rename');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  // Ignore stale events generated by mkdir and other tests
331cb0ef41Sopenharmony_ci  if (filename !== relativePathOne)
341cb0ef41Sopenharmony_ci    return;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  if (common.isOSX) {
371cb0ef41Sopenharmony_ci    clearInterval(interval);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci  watcher.close();
401cb0ef41Sopenharmony_ci  watcherClosed = true;
411cb0ef41Sopenharmony_ci});
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cilet interval;
441cb0ef41Sopenharmony_ciif (common.isOSX) {
451cb0ef41Sopenharmony_ci  interval = setInterval(function() {
461cb0ef41Sopenharmony_ci    fs.writeFileSync(filepathOne, 'world');
471cb0ef41Sopenharmony_ci  }, 10);
481cb0ef41Sopenharmony_ci} else {
491cb0ef41Sopenharmony_ci  fs.writeFileSync(filepathOne, 'world');
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciprocess.on('exit', function() {
531cb0ef41Sopenharmony_ci  assert(watcherClosed, 'watcher Object was not closed');
541cb0ef41Sopenharmony_ci});
55