11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This verifies the error thrown by fs.watch.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (common.isIBMi)
91cb0ef41Sopenharmony_ci  common.skip('IBMi does not support `fs.watch()`');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
141cb0ef41Sopenharmony_ciconst path = require('path');
151cb0ef41Sopenharmony_ciconst nonexistentFile = path.join(tmpdir.path, 'non-existent');
161cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
171cb0ef41Sopenharmony_ciconst {
181cb0ef41Sopenharmony_ci  UV_ENODEV,
191cb0ef41Sopenharmony_ci  UV_ENOENT
201cb0ef41Sopenharmony_ci} = internalBinding('uv');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_citmpdir.refresh();
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci{
251cb0ef41Sopenharmony_ci  const validateError = (err) => {
261cb0ef41Sopenharmony_ci    assert.strictEqual(err.path, nonexistentFile);
271cb0ef41Sopenharmony_ci    assert.strictEqual(err.filename, nonexistentFile);
281cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'watch');
291cb0ef41Sopenharmony_ci    if (err.code === 'ENOENT') {
301cb0ef41Sopenharmony_ci      assert.strictEqual(
311cb0ef41Sopenharmony_ci        err.message,
321cb0ef41Sopenharmony_ci        `ENOENT: no such file or directory, watch '${nonexistentFile}'`);
331cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_ENOENT);
341cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ENOENT');
351cb0ef41Sopenharmony_ci    } else {  // AIX
361cb0ef41Sopenharmony_ci      assert.strictEqual(
371cb0ef41Sopenharmony_ci        err.message,
381cb0ef41Sopenharmony_ci        `ENODEV: no such device, watch '${nonexistentFile}'`);
391cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_ENODEV);
401cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ENODEV');
411cb0ef41Sopenharmony_ci    }
421cb0ef41Sopenharmony_ci    return true;
431cb0ef41Sopenharmony_ci  };
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  assert.throws(
461cb0ef41Sopenharmony_ci    () => fs.watch(nonexistentFile, common.mustNotCall()),
471cb0ef41Sopenharmony_ci    validateError
481cb0ef41Sopenharmony_ci  );
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci  const file = path.join(tmpdir.path, 'file-to-watch');
531cb0ef41Sopenharmony_ci  fs.writeFileSync(file, 'test');
541cb0ef41Sopenharmony_ci  const watcher = fs.watch(file, common.mustNotCall());
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  const validateError = (err) => {
571cb0ef41Sopenharmony_ci    assert.strictEqual(err.path, nonexistentFile);
581cb0ef41Sopenharmony_ci    assert.strictEqual(err.filename, nonexistentFile);
591cb0ef41Sopenharmony_ci    assert.strictEqual(
601cb0ef41Sopenharmony_ci      err.message,
611cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, watch '${nonexistentFile}'`);
621cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
631cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
641cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'watch');
651cb0ef41Sopenharmony_ci    fs.unlinkSync(file);
661cb0ef41Sopenharmony_ci    return true;
671cb0ef41Sopenharmony_ci  };
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  watcher.on('error', common.mustCall(validateError));
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // Simulate the invocation from the binding
721cb0ef41Sopenharmony_ci  watcher._handle.onchange(UV_ENOENT, 'ENOENT', nonexistentFile);
731cb0ef41Sopenharmony_ci}
74