|
| 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 FILENAME = path.join(common.tmpDir, 'watch-me'); |
| 28 | +var TIMEOUT = 1300; |
| 29 | + |
| 30 | +var nevents = 0; |
| 31 | + |
| 32 | +try { |
| 33 | + fs.unlinkSync(FILENAME); |
| 34 | +} |
| 35 | +catch (e) { |
| 36 | + // swallow |
| 37 | +} |
| 38 | + |
| 39 | +fs.watchFile(FILENAME, {interval:TIMEOUT - 250}, function(curr, prev) { |
| 40 | + console.log([curr, prev]); |
| 41 | + switch (++nevents) { |
| 42 | + case 1: |
| 43 | + case 2: |
| 44 | + assert.equal(fs.existsSync(FILENAME), true); |
| 45 | + break; |
| 46 | + case 3: |
| 47 | + assert.equal(fs.existsSync(FILENAME), false); |
| 48 | + fs.unwatchFile(FILENAME); |
| 49 | + break; |
| 50 | + default: |
| 51 | + assert(0); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +process.on('exit', function() { |
| 56 | + assert.equal(nevents, 3); |
| 57 | +}); |
| 58 | + |
| 59 | +setTimeout(createFile, TIMEOUT); |
| 60 | + |
| 61 | +function createFile() { |
| 62 | + fs.writeFileSync(FILENAME, "test"); |
| 63 | + setTimeout(touchFile, TIMEOUT); |
| 64 | +} |
| 65 | + |
| 66 | +function touchFile() { |
| 67 | + fs.writeFileSync(FILENAME, "test"); |
| 68 | + setTimeout(removeFile, TIMEOUT); |
| 69 | +} |
| 70 | + |
| 71 | +function removeFile() { |
| 72 | + fs.unlinkSync(FILENAME); |
| 73 | +} |
0 commit comments