• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4// This verifies the error thrown by fs.watch.
5
6const common = require('../common');
7
8if (common.isIBMi)
9  common.skip('IBMi does not support `fs.watch()`');
10
11const assert = require('assert');
12const fs = require('fs');
13const tmpdir = require('../common/tmpdir');
14const path = require('path');
15const nonexistentFile = path.join(tmpdir.path, 'non-existent');
16const { internalBinding } = require('internal/test/binding');
17const {
18  UV_ENODEV,
19  UV_ENOENT
20} = internalBinding('uv');
21
22tmpdir.refresh();
23
24{
25  const validateError = (err) => {
26    assert.strictEqual(err.path, nonexistentFile);
27    assert.strictEqual(err.filename, nonexistentFile);
28    assert.strictEqual(err.syscall, 'watch');
29    if (err.code === 'ENOENT') {
30      assert.strictEqual(
31        err.message,
32        `ENOENT: no such file or directory, watch '${nonexistentFile}'`);
33      assert.strictEqual(err.errno, UV_ENOENT);
34      assert.strictEqual(err.code, 'ENOENT');
35    } else {  // AIX
36      assert.strictEqual(
37        err.message,
38        `ENODEV: no such device, watch '${nonexistentFile}'`);
39      assert.strictEqual(err.errno, UV_ENODEV);
40      assert.strictEqual(err.code, 'ENODEV');
41    }
42    return true;
43  };
44
45  assert.throws(
46    () => fs.watch(nonexistentFile, common.mustNotCall()),
47    validateError
48  );
49}
50
51{
52  const file = path.join(tmpdir.path, 'file-to-watch');
53  fs.writeFileSync(file, 'test');
54  const watcher = fs.watch(file, common.mustNotCall());
55
56  const validateError = (err) => {
57    assert.strictEqual(err.path, nonexistentFile);
58    assert.strictEqual(err.filename, nonexistentFile);
59    assert.strictEqual(
60      err.message,
61      `ENOENT: no such file or directory, watch '${nonexistentFile}'`);
62    assert.strictEqual(err.errno, UV_ENOENT);
63    assert.strictEqual(err.code, 'ENOENT');
64    assert.strictEqual(err.syscall, 'watch');
65    fs.unlinkSync(file);
66    return true;
67  };
68
69  watcher.on('error', common.mustCall(validateError));
70
71  // Simulate the invocation from the binding
72  watcher._handle.onchange(UV_ENOENT, 'ENOENT', nonexistentFile);
73}
74