• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const path = require('path');
26const fs = require('fs');
27
28const tmpdir = require('../common/tmpdir');
29
30let watchSeenOne = 0;
31let watchSeenTwo = 0;
32let watchSeenThree = 0;
33let watchSeenFour = 0;
34
35const testDir = tmpdir.path;
36
37const filenameOne = 'watch.txt';
38const filepathOne = path.join(testDir, filenameOne);
39
40const filenameTwo = 'hasOwnProperty';
41const filepathTwo = filenameTwo;
42const filepathTwoAbs = path.join(testDir, filenameTwo);
43
44const filenameThree = 'charm'; // Because the third time is
45
46const filenameFour = 'get';
47
48process.on('exit', function() {
49  assert.strictEqual(watchSeenOne, 1);
50  assert.strictEqual(watchSeenTwo, 2);
51  assert.strictEqual(watchSeenThree, 1);
52  assert.strictEqual(watchSeenFour, 1);
53});
54
55
56tmpdir.refresh();
57fs.writeFileSync(filepathOne, 'hello');
58
59assert.throws(
60  () => { fs.watchFile(filepathOne); },
61  { code: 'ERR_INVALID_ARG_TYPE' },
62);
63
64// Does not throw.
65fs.watchFile(filepathOne, function() {
66  fs.unwatchFile(filepathOne);
67  ++watchSeenOne;
68});
69
70setTimeout(function() {
71  fs.writeFileSync(filepathOne, 'world');
72}, 1000);
73
74
75process.chdir(testDir);
76
77fs.writeFileSync(filepathTwoAbs, 'howdy');
78
79assert.throws(
80  () => { fs.watchFile(filepathTwo); },
81  { code: 'ERR_INVALID_ARG_TYPE' },
82);
83
84{ // Does not throw.
85  function a() {
86    fs.unwatchFile(filepathTwo, a);
87    ++watchSeenTwo;
88  }
89
90  function b() {
91    fs.unwatchFile(filepathTwo, b);
92    ++watchSeenTwo;
93  }
94  fs.watchFile(filepathTwo, a);
95  fs.watchFile(filepathTwo, b);
96}
97
98setTimeout(function() {
99  fs.writeFileSync(filepathTwoAbs, 'pardner');
100}, 1000);
101
102{ // Does not throw.
103  function b() {
104    fs.unwatchFile(filenameThree, b);
105    ++watchSeenThree;
106  }
107  const uncalledListener = common.mustNotCall();
108  fs.watchFile(filenameThree, uncalledListener);
109  fs.watchFile(filenameThree, b);
110  fs.unwatchFile(filenameThree, uncalledListener);
111}
112
113setTimeout(function() {
114  fs.writeFileSync(filenameThree, 'pardner');
115}, 1000);
116
117setTimeout(function() {
118  fs.writeFileSync(filenameFour, 'hey');
119}, 200);
120
121setTimeout(function() {
122  fs.writeFileSync(filenameFour, 'hey');
123}, 500);
124
125{ // Does not throw.
126  function a() {
127    ++watchSeenFour;
128    assert.strictEqual(watchSeenFour, 1);
129    fs.unwatchFile(`.${path.sep}${filenameFour}`, a);
130  }
131  fs.watchFile(filenameFour, a);
132}
133