• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5
6const assert = require('assert');
7const path = require('path');
8const fs = require('fs');
9
10const tmpdir = require('../common/tmpdir');
11
12const testDir = tmpdir.path;
13const filenameOne = 'watch.txt';
14
15tmpdir.refresh();
16
17const testsubdir = fs.mkdtempSync(testDir + path.sep);
18const relativePathOne = path.join(path.basename(testsubdir), filenameOne);
19const filepathOne = path.join(testsubdir, filenameOne);
20
21if (!common.isOSX && !common.isWindows) {
22  assert.throws(() => { fs.watch(testDir, { recursive: true }); },
23                { code: 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM' });
24  return;
25}
26const watcher = fs.watch(testDir, { recursive: true });
27
28let watcherClosed = false;
29watcher.on('change', function(event, filename) {
30  assert.ok(event === 'change' || event === 'rename');
31
32  // Ignore stale events generated by mkdir and other tests
33  if (filename !== relativePathOne)
34    return;
35
36  if (common.isOSX) {
37    clearInterval(interval);
38  }
39  watcher.close();
40  watcherClosed = true;
41});
42
43let interval;
44if (common.isOSX) {
45  interval = setInterval(function() {
46    fs.writeFileSync(filepathOne, 'world');
47  }, 10);
48} else {
49  fs.writeFileSync(filepathOne, 'world');
50}
51
52process.on('exit', function() {
53  assert(watcherClosed, 'watcher Object was not closed');
54});
55