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