summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-watch-recursive-add-file-with-url.js
blob: ee726961c41e9ec28ef4288306a529fa38710802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
  common.skip('IBMi does not support `fs.watch()`');

// fs-watch on folders have limited capability in AIX.
// The testcase makes use of folder watching, and causes
// hang. This behavior is documented. Skip this for AIX.

if (common.isAIX)
  common.skip('folder watch capability is limited in AIX.');

const assert = require('assert');
const path = require('path');
const fs = require('fs');
const { pathToFileURL } = require('url');

const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;
tmpdir.refresh();

(async () => {
  // Add a file to already watching folder, and use URL as the path

  const rootDirectory = fs.mkdtempSync(testDir + path.sep);
  const testDirectory = path.join(rootDirectory, 'test-5');
  fs.mkdirSync(testDirectory);

  const filePath = path.join(testDirectory, 'file-8.txt');
  const url = pathToFileURL(testDirectory);

  const watcher = fs.watch(url, { recursive: true });
  let watcherClosed = false;
  watcher.on('change', function(event, filename) {
    assert.strictEqual(event, 'rename');

    if (filename === path.basename(filePath)) {
      watcher.close();
      watcherClosed = true;
    }
  });

  await setTimeout(common.platformTimeout(100));
  fs.writeFileSync(filePath, 'world');

  process.on('exit', function() {
    assert(watcherClosed, 'watcher Object was not closed');
  });
})().then(common.mustCall());