diff --git a/src/fs.js b/src/fs.js index b563ffb..47a086e 100644 --- a/src/fs.js +++ b/src/fs.js @@ -167,8 +167,9 @@ define(function(require) { } function complete(error) { - // Broadcast this change to all fs instances, in all windows on this origin - context.intercom.emit('change', path); + // Broadcast this change to all fs instances, in all windows on this origin. + // Unlike node.js, we send the full path vs. basename/dirname only. + context.intercom.emit('change', 'change', path); callback(error); } @@ -1562,10 +1563,10 @@ define(function(require) { var recursive = false; var filename; - function onchange(path) { + function onchange(event, path) { // Watch for exact filename, or parent path when recursive is true - if(filename === path || (recursive && filename.indexOf(path + '/') === 0)) { - self.emit('change', filename); + if(filename === path || (recursive && path.indexOf(filename + '/') === 0)) { + self.emit('change', 'change', path); } } diff --git a/tests/spec/fs.watch.spec.js b/tests/spec/fs.watch.spec.js index 3ed77a6..3ee5d7e 100644 --- a/tests/spec/fs.watch.spec.js +++ b/tests/spec/fs.watch.spec.js @@ -12,8 +12,10 @@ define(["Filer", "util"], function(Filer, util) { it('should get a change event when writing a file', function(done) { var fs = util.fs(); - fs.watch('/myfile', function(filename) { + var watcher = fs.watch('/myfile', function(event, filename) { + expect(event).to.equal('change'); expect(filename).to.equal('/myfile'); + watcher.close(); done(); }); @@ -25,8 +27,10 @@ define(["Filer", "util"], function(Filer, util) { it('should get a change event when writing a file in a dir with recursive=true', function(done) { var fs = util.fs(); - fs.watch('/', { recursive: true }, function(filename) { + var watcher = fs.watch('/', { recursive: true }, function(event, filename) { + expect(event).to.equal('change'); expect(filename).to.equal('/'); + watcher.close(); done(); }); diff --git a/tests/spec/node-js/simple/test-fs-watch-recursive.js b/tests/spec/node-js/simple/test-fs-watch-recursive.js new file mode 100644 index 0000000..b3072fa --- /dev/null +++ b/tests/spec/node-js/simple/test-fs-watch-recursive.js @@ -0,0 +1,38 @@ +define(["Filer", "util"], function(Filer, util) { + + /** + * 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. + */ + + describe("node.js tests: https://github.com/joyent/node/blob/master/test/simple/test-fs-watch-recursive.js", function() { + + beforeEach(util.setup); + afterEach(util.cleanup); + + it('should get change event for writeFile() under a recursive watched dir', function(done) { + var fs = util.fs(); + + fs.mkdir('/test', function(error) { + if(error) throw error; + + fs.mkdir('/test/subdir', function(error) { + if(error) throw error; + + var watcher = fs.watch('/test', {recursive: true}); + watcher.on('change', function(event, filename) { + expect(event).to.equal('change'); + // Expect to see that a new file was created in /test/subdir + expect(filename).to.equal('/test/subdir'); + watcher.close(); + done(); + }); + + fs.writeFile('/test/subdir/watch.txt', 'world'); + }); + }); + }); + + }); +}); diff --git a/tests/spec/node-js/simple/test-fs-watch.js b/tests/spec/node-js/simple/test-fs-watch.js new file mode 100644 index 0000000..726511d --- /dev/null +++ b/tests/spec/node-js/simple/test-fs-watch.js @@ -0,0 +1,73 @@ +define(["Filer", "util"], function(Filer, util) { + + /** + * 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'; + + describe("node.js tests: https://github.com/joyent/node/blob/master/test/simple/test-fs-watch.js", function() { + + 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(); + }); + }); + + fs.writeFile(filenameOne, 'hello'); + }); + + it('should get change event for writeFile() using fs.watch() only', function(done) { + var fs = util.fs(); + var changes = 0; + + var watcher = fs.watch(filenameTwo, function(event, filename) { + expect(event).to.equal('change'); + expect(filename).to.equal(filenameTwo); + + watcher.close(); + done(); + }); + + fs.writeFile(filenameTwo, 'pardner'); + }); + + it('should allow watches on dirs', function(done) { + var fs = util.fs(); + fs.mkdir('/tmp', function(error) { + if(error) throw error; + + var watcher = fs.watch('/tmp', function(event, filename) { + expect(event).to.equal('rename'); + expect(filename).to.equal('/tmp/newfile.txt'); + watcher.close(); + done(); + }); + + fs.open('/tmp/newfile.txt', 'w', function(error, fd) { + if(error) throw error; + fs.close(fd); + }); + }); + }); + }); +}); diff --git a/tests/test-manifest.js b/tests/test-manifest.js index f08c55f..256024b 100644 --- a/tests/test-manifest.js +++ b/tests/test-manifest.js @@ -58,6 +58,8 @@ define([ // Ported node.js tests (filenames match names in https://github.com/joyent/node/tree/master/test) "spec/node-js/simple/test-fs-mkdir", "spec/node-js/simple/test-fs-null-bytes", + "spec/node-js/simple/test-fs-watch", + "spec/node-js/simple/test-fs-watch-recursive", // Regressions, Bugs "bugs/issue105",