Most things passing for change events now, ported node.js watch tests

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-03-07 11:54:14 -05:00
parent 683c548f73
commit 96836f355c
5 changed files with 125 additions and 7 deletions

View File

@ -167,8 +167,9 @@ define(function(require) {
} }
function complete(error) { function complete(error) {
// Broadcast this change to all fs instances, in all windows on this origin // Broadcast this change to all fs instances, in all windows on this origin.
context.intercom.emit('change', path); // Unlike node.js, we send the full path vs. basename/dirname only.
context.intercom.emit('change', 'change', path);
callback(error); callback(error);
} }
@ -1562,10 +1563,10 @@ define(function(require) {
var recursive = false; var recursive = false;
var filename; var filename;
function onchange(path) { function onchange(event, path) {
// Watch for exact filename, or parent path when recursive is true // Watch for exact filename, or parent path when recursive is true
if(filename === path || (recursive && filename.indexOf(path + '/') === 0)) { if(filename === path || (recursive && path.indexOf(filename + '/') === 0)) {
self.emit('change', filename); self.emit('change', 'change', path);
} }
} }

View File

@ -12,8 +12,10 @@ define(["Filer", "util"], function(Filer, util) {
it('should get a change event when writing a file', function(done) { it('should get a change event when writing a file', function(done) {
var fs = util.fs(); 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'); expect(filename).to.equal('/myfile');
watcher.close();
done(); 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) { it('should get a change event when writing a file in a dir with recursive=true', function(done) {
var fs = util.fs(); 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('/'); expect(filename).to.equal('/');
watcher.close();
done(); done();
}); });

View File

@ -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');
});
});
});
});
});

View File

@ -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);
});
});
});
});
});

View File

@ -58,6 +58,8 @@ define([
// Ported node.js tests (filenames match names in https://github.com/joyent/node/tree/master/test) // 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-mkdir",
"spec/node-js/simple/test-fs-null-bytes", "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 // Regressions, Bugs
"bugs/issue105", "bugs/issue105",