• 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  fs.unlinkSync(filepathOne);
50  fs.unlinkSync(filepathTwoAbs);
51  fs.unlinkSync(filenameThree);
52  fs.unlinkSync(filenameFour);
53  assert.strictEqual(watchSeenOne, 1);
54  assert.strictEqual(watchSeenTwo, 2);
55  assert.strictEqual(watchSeenThree, 1);
56  assert.strictEqual(watchSeenFour, 1);
57});
58
59
60fs.writeFileSync(filepathOne, 'hello');
61
62assert.throws(
63  () => { fs.watchFile(filepathOne); },
64  { code: 'ERR_INVALID_ARG_TYPE' }
65);
66
67// Does not throw.
68fs.watchFile(filepathOne, function() {
69  fs.unwatchFile(filepathOne);
70  ++watchSeenOne;
71});
72
73setTimeout(function() {
74  fs.writeFileSync(filepathOne, 'world');
75}, 1000);
76
77
78process.chdir(testDir);
79
80fs.writeFileSync(filepathTwoAbs, 'howdy');
81
82assert.throws(
83  () => { fs.watchFile(filepathTwo); },
84  { code: 'ERR_INVALID_ARG_TYPE' }
85);
86
87{ // Does not throw.
88  function a() {
89    fs.unwatchFile(filepathTwo, a);
90    ++watchSeenTwo;
91  }
92
93  function b() {
94    fs.unwatchFile(filepathTwo, b);
95    ++watchSeenTwo;
96  }
97  fs.watchFile(filepathTwo, a);
98  fs.watchFile(filepathTwo, b);
99}
100
101setTimeout(function() {
102  fs.writeFileSync(filepathTwoAbs, 'pardner');
103}, 1000);
104
105{ // Does not throw.
106  function b() {
107    fs.unwatchFile(filenameThree, b);
108    ++watchSeenThree;
109  }
110  const uncalledListener = common.mustNotCall();
111  fs.watchFile(filenameThree, uncalledListener);
112  fs.watchFile(filenameThree, b);
113  fs.unwatchFile(filenameThree, uncalledListener);
114}
115
116setTimeout(function() {
117  fs.writeFileSync(filenameThree, 'pardner');
118}, 1000);
119
120setTimeout(function() {
121  fs.writeFileSync(filenameFour, 'hey');
122}, 200);
123
124setTimeout(function() {
125  fs.writeFileSync(filenameFour, 'hey');
126}, 500);
127
128{ // Does not throw.
129  function a() {
130    ++watchSeenFour;
131    assert.strictEqual(watchSeenFour, 1);
132    fs.unwatchFile(`.${path.sep}${filenameFour}`, a);
133  }
134  fs.watchFile(filenameFour, a);
135}
136