470736d594ffb896c2cf48476e696d9ffaa0a230
[platform/upstream/nodejs.git] / test / sequential / test-fs-watch.js
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 var common = require('../common');
23 var assert = require('assert');
24 var path = require('path');
25 var fs = require('fs');
26
27 var expectFilePath = process.platform === 'win32' ||
28                      process.platform === 'linux' ||
29                      process.platform === 'darwin';
30
31 var watchSeenOne = 0;
32 var watchSeenTwo = 0;
33 var watchSeenThree = 0;
34
35 var testDir = common.tmpDir;
36
37 var filenameOne = 'watch.txt';
38 var filepathOne = path.join(testDir, filenameOne);
39
40 var filenameTwo = 'hasOwnProperty';
41 var filepathTwo = filenameTwo;
42 var filepathTwoAbs = path.join(testDir, filenameTwo);
43
44 var filenameThree = 'newfile.txt';
45 var testsubdir = path.join(testDir, 'testsubdir');
46 var filepathThree = path.join(testsubdir, filenameThree);
47
48
49 process.on('exit', function() {
50   assert.ok(watchSeenOne > 0);
51   assert.ok(watchSeenTwo > 0);
52   assert.ok(watchSeenThree > 0);
53 });
54
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) { }
60
61 fs.writeFileSync(filepathOne, 'hello');
62
63 assert.doesNotThrow(
64     function() {
65       var watcher = fs.watch(filepathOne)
66       watcher.on('change', function(event, filename) {
67         assert.equal('change', event);
68
69         if (expectFilePath) {
70           assert.equal('watch.txt', filename);
71         }
72         watcher.close();
73         ++watchSeenOne;
74       });
75     }
76 );
77
78 setTimeout(function() {
79   fs.writeFileSync(filepathOne, 'world');
80 }, 10);
81
82
83 process.chdir(testDir);
84
85 fs.writeFileSync(filepathTwoAbs, 'howdy');
86
87 assert.doesNotThrow(
88     function() {
89       var watcher = fs.watch(filepathTwo, function(event, filename) {
90         assert.equal('change', event);
91
92         if (expectFilePath) {
93           assert.equal('hasOwnProperty', filename);
94         }
95         watcher.close();
96         ++watchSeenTwo;
97       });
98     }
99 );
100
101 setTimeout(function() {
102   fs.writeFileSync(filepathTwoAbs, 'pardner');
103 }, 10);
104
105 try { fs.unlinkSync(filepathThree); } catch (e) {}
106 try { fs.mkdirSync(testsubdir, 0700); } catch (e) {}
107
108 assert.doesNotThrow(
109     function() {
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);
115         } else {
116           assert.equal(null, filename);
117         }
118         watcher.close();
119         ++watchSeenThree;
120       });
121     }
122 );
123
124 setTimeout(function() {
125   var fd = fs.openSync(filepathThree, 'w');
126   fs.closeSync(fd);
127 }, 10);
128
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() {
132   assert(0);
133 });
134
135 // whitebox test to ensure that wrapped FSEvent is safe
136 // https://github.com/joyent/node/issues/6690
137 var oldhandle;
138 assert.throws(function() {
139   var w = fs.watch(__filename, function(event, filename) { });
140   oldhandle = w._handle;
141   w._handle = { close: w._handle.close };
142   w.close();
143 }, TypeError);
144 oldhandle.close(); // clean up
145
146 assert.throws(function() {
147   var w = fs.watchFile(__filename, {persistent:false}, function(){});
148   oldhandle = w._handle;
149   w._handle = { stop: w._handle.stop };
150   w.stop();
151 }, TypeError);
152 oldhandle.stop(); // clean up