1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
23 var assert = require('assert');
24 var path = require('path');
25 var fs = require('fs');
27 var expectFilePath = process.platform === 'win32' ||
28 process.platform === 'linux' ||
29 process.platform === 'darwin';
33 var watchSeenThree = 0;
35 var testDir = common.tmpDir;
37 var filenameOne = 'watch.txt';
38 var filepathOne = path.join(testDir, filenameOne);
40 var filenameTwo = 'hasOwnProperty';
41 var filepathTwo = filenameTwo;
42 var filepathTwoAbs = path.join(testDir, filenameTwo);
44 var filenameThree = 'newfile.txt';
45 var testsubdir = path.join(testDir, 'testsubdir');
46 var filepathThree = path.join(testsubdir, filenameThree);
49 process.on('exit', function() {
50 assert.ok(watchSeenOne > 0);
51 assert.ok(watchSeenTwo > 0);
52 assert.ok(watchSeenThree > 0);
55 // Clean up stale files (if any) from previous run.
56 try { fs.unlinkSync(filepathOne); } catch (e) { }
57 try { fs.unlinkSync(filepathTwoAbs); } catch (e) { }
58 try { fs.unlinkSync(filepathThree); } catch (e) { }
59 try { fs.rmdirSync(testsubdir); } catch (e) { }
61 fs.writeFileSync(filepathOne, 'hello');
65 var watcher = fs.watch(filepathOne)
66 watcher.on('change', function(event, filename) {
67 assert.equal('change', event);
70 assert.equal('watch.txt', filename);
78 setTimeout(function() {
79 fs.writeFileSync(filepathOne, 'world');
83 process.chdir(testDir);
85 fs.writeFileSync(filepathTwoAbs, 'howdy');
89 var watcher = fs.watch(filepathTwo, function(event, filename) {
90 assert.equal('change', event);
93 assert.equal('hasOwnProperty', filename);
101 setTimeout(function() {
102 fs.writeFileSync(filepathTwoAbs, 'pardner');
105 try { fs.unlinkSync(filepathThree); } catch (e) {}
106 try { fs.mkdirSync(testsubdir, 0700); } catch (e) {}
110 var watcher = fs.watch(testsubdir, function(event, filename) {
111 var renameEv = process.platform === 'sunos' ? 'change' : 'rename';
112 assert.equal(renameEv, event);
113 if (expectFilePath) {
114 assert.equal('newfile.txt', filename);
116 assert.equal(null, filename);
124 setTimeout(function() {
125 var fd = fs.openSync(filepathThree, 'w');
129 // https://github.com/joyent/node/issues/2293 - non-persistent watcher should
130 // not block the event loop
131 fs.watch(__filename, {persistent: false}, function() {
135 // whitebox test to ensure that wrapped FSEvent is safe
136 // https://github.com/joyent/node/issues/6690
138 assert.throws(function() {
139 var w = fs.watch(__filename, function(event, filename) { });
140 oldhandle = w._handle;
141 w._handle = { close: w._handle.close };
144 oldhandle.close(); // clean up
146 assert.throws(function() {
147 var w = fs.watchFile(__filename, {persistent:false}, function(){});
148 oldhandle = w._handle;
149 w._handle = { stop: w._handle.stop };
152 oldhandle.stop(); // clean up