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'; 23require('../common'); 24const assert = require('assert'); 25const path = require('path'); 26const fs = require('fs'); 27 28const tmpdir = require('../common/tmpdir'); 29 30tmpdir.refresh(); 31const FILENAME = path.join(tmpdir.path, 'watch-me'); 32const TIMEOUT = 1300; 33 34let nevents = 0; 35 36try { 37 fs.unlinkSync(FILENAME); 38} catch { 39 // swallow 40} 41 42fs.watchFile(FILENAME, { interval: TIMEOUT - 250 }, function(curr, prev) { 43 console.log([curr, prev]); 44 switch (++nevents) { 45 case 1: 46 assert.strictEqual(fs.existsSync(FILENAME), false); 47 break; 48 case 2: 49 case 3: 50 assert.strictEqual(fs.existsSync(FILENAME), true); 51 break; 52 case 4: 53 assert.strictEqual(fs.existsSync(FILENAME), false); 54 fs.unwatchFile(FILENAME); 55 break; 56 default: 57 assert(0); 58 } 59}); 60 61process.on('exit', function() { 62 assert.strictEqual(nevents, 4); 63}); 64 65setTimeout(createFile, TIMEOUT); 66 67function createFile() { 68 console.log('creating file'); 69 fs.writeFileSync(FILENAME, 'test'); 70 setTimeout(touchFile, TIMEOUT); 71} 72 73function touchFile() { 74 console.log('touch file'); 75 fs.writeFileSync(FILENAME, 'test'); 76 setTimeout(removeFile, TIMEOUT); 77} 78 79function removeFile() { 80 console.log('remove file'); 81 fs.unlinkSync(FILENAME); 82} 83