|
21 | 21 |
|
22 | 22 | 'use strict';
|
23 | 23 | const common = require('../common');
|
| 24 | + |
24 | 25 | const assert = require('assert');
|
25 |
| -const path = require('path'); |
26 | 26 | const fs = require('fs');
|
| 27 | +const path = require('path'); |
27 | 28 |
|
28 | 29 | const expectFilePath = common.isWindows ||
|
29 | 30 | common.isLinux ||
|
30 | 31 | common.isOSX ||
|
31 | 32 | common.isAIX;
|
32 | 33 |
|
33 |
| -let watchSeenOne = 0; |
34 |
| -let watchSeenTwo = 0; |
35 |
| -let watchSeenThree = 0; |
36 |
| - |
37 | 34 | const testDir = common.tmpDir;
|
38 | 35 |
|
39 |
| -const filenameOne = 'watch.txt'; |
40 |
| -const filepathOne = path.join(testDir, filenameOne); |
41 |
| - |
42 |
| -const filenameTwo = 'hasOwnProperty'; |
43 |
| -const filepathTwo = filenameTwo; |
44 |
| -const filepathTwoAbs = path.join(testDir, filenameTwo); |
45 |
| - |
46 |
| -process.on('exit', function() { |
47 |
| - assert.ok(watchSeenOne > 0); |
48 |
| - assert.ok(watchSeenTwo > 0); |
49 |
| - assert.ok(watchSeenThree > 0); |
50 |
| -}); |
51 |
| - |
52 | 36 | common.refreshTmpDir();
|
53 | 37 |
|
54 |
| -fs.writeFileSync(filepathOne, 'hello'); |
55 |
| - |
56 |
| -assert.doesNotThrow( |
57 |
| - function() { |
58 |
| - const watcher = fs.watch(filepathOne); |
59 |
| - watcher.on('change', function(event, filename) { |
60 |
| - assert.strictEqual(event, 'change'); |
61 |
| - |
62 |
| - if (expectFilePath) { |
63 |
| - assert.strictEqual(filename, 'watch.txt'); |
64 |
| - } |
65 |
| - watcher.close(); |
66 |
| - ++watchSeenOne; |
67 |
| - }); |
68 |
| - } |
69 |
| -); |
70 |
| - |
71 |
| -setImmediate(function() { |
72 |
| - fs.writeFileSync(filepathOne, 'world'); |
73 |
| -}); |
74 |
| - |
75 |
| - |
76 |
| -process.chdir(testDir); |
77 |
| - |
78 |
| -fs.writeFileSync(filepathTwoAbs, 'howdy'); |
79 |
| - |
80 |
| -assert.doesNotThrow( |
81 |
| - function() { |
82 |
| - const watcher = fs.watch(filepathTwo, function(event, filename) { |
83 |
| - assert.strictEqual(event, 'change'); |
84 |
| - |
85 |
| - if (expectFilePath) { |
86 |
| - assert.strictEqual(filename, 'hasOwnProperty'); |
87 |
| - } |
88 |
| - watcher.close(); |
89 |
| - ++watchSeenTwo; |
90 |
| - }); |
91 |
| - } |
92 |
| -); |
93 |
| - |
94 |
| -setImmediate(function() { |
95 |
| - fs.writeFileSync(filepathTwoAbs, 'pardner'); |
96 |
| -}); |
97 |
| - |
98 |
| -const filenameThree = 'newfile.txt'; |
99 |
| -const testsubdir = fs.mkdtempSync(testDir + path.sep); |
100 |
| -const filepathThree = path.join(testsubdir, filenameThree); |
101 |
| - |
102 |
| -assert.doesNotThrow( |
103 |
| - function() { |
104 |
| - const watcher = fs.watch(testsubdir, function(event, filename) { |
105 |
| - const renameEv = common.isSunOS || common.isAIX ? 'change' : 'rename'; |
106 |
| - assert.strictEqual(event, renameEv); |
107 |
| - if (expectFilePath) { |
108 |
| - assert.strictEqual(filename, 'newfile.txt'); |
109 |
| - } else { |
110 |
| - assert.strictEqual(filename, null); |
111 |
| - } |
112 |
| - watcher.close(); |
113 |
| - ++watchSeenThree; |
114 |
| - }); |
115 |
| - } |
116 |
| -); |
117 |
| - |
118 |
| -setImmediate(function() { |
119 |
| - const fd = fs.openSync(filepathThree, 'w'); |
120 |
| - fs.closeSync(fd); |
121 |
| -}); |
| 38 | +{ |
| 39 | + const filepath = path.join(testDir, 'watch.txt'); |
| 40 | + |
| 41 | + fs.writeFileSync(filepath, 'hello'); |
| 42 | + |
| 43 | + assert.doesNotThrow( |
| 44 | + function() { |
| 45 | + const watcher = fs.watch(filepath); |
| 46 | + watcher.on('change', common.mustCall(function(event, filename) { |
| 47 | + assert.strictEqual(event, 'change'); |
| 48 | + |
| 49 | + if (expectFilePath) { |
| 50 | + assert.strictEqual(filename, 'watch.txt'); |
| 51 | + } |
| 52 | + watcher.close(); |
| 53 | + })); |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + setImmediate(function() { |
| 58 | + fs.writeFileSync(filepath, 'world'); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +{ |
| 63 | + const filepathAbs = path.join(testDir, 'hasOwnProperty'); |
| 64 | + |
| 65 | + process.chdir(testDir); |
| 66 | + |
| 67 | + fs.writeFileSync(filepathAbs, 'howdy'); |
| 68 | + |
| 69 | + assert.doesNotThrow( |
| 70 | + function() { |
| 71 | + const watcher = |
| 72 | + fs.watch('hasOwnProperty', common.mustCall(function(event, filename) { |
| 73 | + assert.strictEqual(event, 'change'); |
| 74 | + |
| 75 | + if (expectFilePath) { |
| 76 | + assert.strictEqual(filename, 'hasOwnProperty'); |
| 77 | + } |
| 78 | + watcher.close(); |
| 79 | + })); |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + setImmediate(function() { |
| 84 | + fs.writeFileSync(filepathAbs, 'pardner'); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +{ |
| 89 | + const testsubdir = fs.mkdtempSync(testDir + path.sep); |
| 90 | + const filepath = path.join(testsubdir, 'newfile.txt'); |
| 91 | + |
| 92 | + assert.doesNotThrow( |
| 93 | + function() { |
| 94 | + const watcher = |
| 95 | + fs.watch(testsubdir, common.mustCall(function(event, filename) { |
| 96 | + const renameEv = common.isSunOS || common.isAIX ? 'change' : 'rename'; |
| 97 | + assert.strictEqual(event, renameEv); |
| 98 | + if (expectFilePath) { |
| 99 | + assert.strictEqual(filename, 'newfile.txt'); |
| 100 | + } else { |
| 101 | + assert.strictEqual(filename, null); |
| 102 | + } |
| 103 | + watcher.close(); |
| 104 | + })); |
| 105 | + } |
| 106 | + ); |
| 107 | + |
| 108 | + setImmediate(function() { |
| 109 | + const fd = fs.openSync(filepath, 'w'); |
| 110 | + fs.closeSync(fd); |
| 111 | + }); |
| 112 | + |
| 113 | +} |
122 | 114 |
|
123 | 115 | // https://github.com/joyent/node/issues/2293 - non-persistent watcher should
|
124 | 116 | // not block the event loop
|
125 |
| -fs.watch(__filename, {persistent: false}, function() { |
126 |
| - assert(0); |
127 |
| -}); |
| 117 | +{ |
| 118 | + fs.watch(__filename, { persistent: false }, common.mustNotCall()); |
| 119 | +} |
128 | 120 |
|
129 | 121 | // whitebox test to ensure that wrapped FSEvent is safe
|
130 | 122 | // https://github.com/joyent/node/issues/6690
|
131 |
| -let oldhandle; |
132 |
| -assert.throws(function() { |
133 |
| - const w = fs.watch(__filename, common.mustNotCall()); |
134 |
| - oldhandle = w._handle; |
135 |
| - w._handle = { close: w._handle.close }; |
136 |
| - w.close(); |
137 |
| -}, /^TypeError: Illegal invocation$/); |
138 |
| -oldhandle.close(); // clean up |
139 |
| - |
140 |
| -assert.throws(function() { |
141 |
| - const w = fs.watchFile(__filename, {persistent: false}, common.mustNotCall()); |
142 |
| - oldhandle = w._handle; |
143 |
| - w._handle = { stop: w._handle.stop }; |
144 |
| - w.stop(); |
145 |
| -}, /^TypeError: Illegal invocation$/); |
146 |
| -oldhandle.stop(); // clean up |
| 123 | +{ |
| 124 | + let oldhandle; |
| 125 | + assert.throws(function() { |
| 126 | + const w = fs.watch(__filename, common.mustNotCall()); |
| 127 | + oldhandle = w._handle; |
| 128 | + w._handle = { close: w._handle.close }; |
| 129 | + w.close(); |
| 130 | + }, /^TypeError: Illegal invocation$/); |
| 131 | + oldhandle.close(); // clean up |
| 132 | + |
| 133 | + assert.throws(function() { |
| 134 | + const w = fs.watchFile(__filename, { persistent: false }, |
| 135 | + common.mustNotCall()); |
| 136 | + oldhandle = w._handle; |
| 137 | + w._handle = { stop: w._handle.stop }; |
| 138 | + w.stop(); |
| 139 | + }, /^TypeError: Illegal invocation$/); |
| 140 | + oldhandle.stop(); // clean up |
| 141 | +} |
0 commit comments