2014-05-23 20:53:50 +00:00
|
|
|
var util = require('../../../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: unlike node.js, which either doesn't give filenames (e.g., in case of
|
|
|
|
* fd vs. path) for events, or gives only a portion thereof (e.g., basname),
|
|
|
|
* we give full, abs paths always.
|
|
|
|
*/
|
|
|
|
var filenameOne = '/watch.txt';
|
|
|
|
var filenameTwo = '/hasOwnProperty';
|
|
|
|
|
2018-07-15 17:25:35 +00:00
|
|
|
describe('node.js tests: https://github.com/joyent/node/blob/master/test/simple/test-fs-watch.js', function() {
|
2014-05-23 20:53:50 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
|
|
|
it('should get change event for writeFile() using FSWatcher object', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var changes = 0;
|
|
|
|
|
|
|
|
var watcher = fs.watch(filenameOne);
|
|
|
|
watcher.on('change', function(event, filename) {
|
|
|
|
expect(event).to.equal('change');
|
|
|
|
expect(filename).to.equal(filenameOne);
|
|
|
|
|
|
|
|
// Make sure only one change event comes in (i.e., close() works)
|
|
|
|
changes++;
|
|
|
|
watcher.close();
|
|
|
|
|
|
|
|
fs.writeFile(filenameOne, 'hello again', function(error) {
|
|
|
|
expect(changes).to.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-03-07 16:54:14 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile(filenameOne, 'hello');
|
|
|
|
});
|
2014-03-07 16:54:14 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should get change event for writeFile() using fs.watch() only', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
var changes = 0;
|
2014-03-07 16:54:14 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
var watcher = fs.watch(filenameTwo, function(event, filename) {
|
|
|
|
expect(event).to.equal('change');
|
|
|
|
expect(filename).to.equal(filenameTwo);
|
2014-03-07 16:54:14 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
watcher.close();
|
|
|
|
done();
|
2014-03-07 16:54:14 +00:00
|
|
|
});
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile(filenameTwo, 'pardner');
|
|
|
|
});
|
2014-03-07 16:54:14 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should allow watches on dirs', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
fs.mkdir('/tmp', function(error) {
|
|
|
|
if(error) throw error;
|
2014-03-07 16:54:14 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
var watcher = fs.watch('/tmp', function(event, filename) {
|
2018-07-15 17:25:35 +00:00
|
|
|
// TODO: node thinks this should be 'rename', need to add rename along with change.
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(event).to.equal('change');
|
|
|
|
expect(filename).to.equal('/tmp');
|
2014-03-07 16:54:14 +00:00
|
|
|
watcher.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/tmp/newfile.txt', 'w', function(error, fd) {
|
2014-03-07 16:54:14 +00:00
|
|
|
if(error) throw error;
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.close(fd);
|
2014-03-07 16:54:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|