2014-05-23 20:53:50 +00:00
|
|
|
var Filer = require('../..');
|
|
|
|
var util = require('../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
describe('fs.watch', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should be a function', function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
expect(typeof fs.watch).to.equal('function');
|
|
|
|
});
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should get a change event when writing a file', function(done) {
|
|
|
|
var fs = util.fs();
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
var watcher = fs.watch('/myfile', function(event, filename) {
|
|
|
|
expect(event).to.equal('change');
|
|
|
|
expect(filename).to.equal('/myfile');
|
|
|
|
watcher.close();
|
|
|
|
done();
|
|
|
|
});
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', 'data', function(error) {
|
|
|
|
if(error) throw error;
|
2014-03-07 03:27:10 +00:00
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-07-14 19:51:40 +00:00
|
|
|
it('should get a change event when writing a file beneath root dir with recursive=true', function(done) {
|
2014-05-23 20:53:50 +00:00
|
|
|
var fs = util.fs();
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
var watcher = fs.watch('/', { recursive: true }, function(event, filename) {
|
|
|
|
expect(event).to.equal('change');
|
|
|
|
expect(filename).to.equal('/');
|
|
|
|
watcher.close();
|
|
|
|
done();
|
|
|
|
});
|
2014-03-07 03:27:10 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.writeFile('/myfile', 'data', function(error) {
|
|
|
|
if(error) throw error;
|
2014-03-07 03:27:10 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-14 19:51:40 +00:00
|
|
|
|
|
|
|
it('should get a change event when writing a file in a dir with recursive=true', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
fs.mkdir('/foo', function(err) {
|
|
|
|
if(err) throw err;
|
|
|
|
|
|
|
|
var watcher = fs.watch('/foo', { recursive: true }, function(event, filename) {
|
|
|
|
expect(event).to.equal('change');
|
|
|
|
expect(filename).to.equal('/foo');
|
|
|
|
watcher.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
// This shouldn't produce a change event
|
|
|
|
fs.writeFile('/myfile-not-in-foo', 'data', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
// This should
|
|
|
|
fs.writeFile('/foo/myfile', 'data', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-03-07 03:27:10 +00:00
|
|
|
});
|