globwatcher
Version:
watch a set of files for changes (including create/delete) by glob patterns
176 lines (160 loc) • 5.74 kB
JavaScript
;
var fs = require("fs");
var mocha_sprinkles = require("mocha-sprinkles");
var path = require("path");
var Promise = require("bluebird");
var shell = require("shelljs");
var should = require("should");
var touch = require("touch");
var util = require("util");
require("source-map-support").install();
var future = mocha_sprinkles.future;
var withTempFolder = mocha_sprinkles.withTempFolder;
var FileWatcher = require("../../lib/globwatcher/filewatcher").FileWatcher;
function makeFixtures(folder) {
var past = Date.now() - 1000;
["" + folder + "/one.x", "" + folder + "/sub/one.x", "" + folder + "/sub/two.x", "" + folder + "/nested/three.x", "" + folder + "/nested/weird.jpg"].map(function (file) {
shell.mkdir("-p", path.dirname(file));
touch.sync(file, { mtime: past });
});
}
function fixtures(f) {
return future(withTempFolder(function (folder) {
makeFixtures(folder);
return f(folder);
}));
}
function withFileWatcher(f) {
return function () {
for (var _len = arguments.length, x = Array(_len), _key = 0; _key < _len; _key++) {
x[_key] = arguments[_key];
}
var watcher = new FileWatcher();
return f.apply(undefined, [watcher].concat(x))["finally"](function () {
return watcher.close();
});
};
}
describe("FileWatcher", function () {
it("creates a watch", fixtures(withFileWatcher(function (watcher, folder) {
(watcher.timer == null).should.eql(true);
var watch = watcher.watch("" + folder + "/one.x");
(watch != null).should.eql(true);
(watcher.timer != null).should.eql(true);
return Promise.resolve();
})));
it("reuses the same watch for the same filename", fixtures(withFileWatcher(function (watcher, folder) {
var watch1 = watcher.watch("" + folder + "/one.x");
var watch2 = watcher.watch("" + folder + "/one.x");
watch1.should.equal(watch2);
return Promise.resolve();
})));
it("notices a change", fixtures(withFileWatcher(function (watcher, folder) {
var watch = watcher.watch("" + folder + "/one.x");
var count = 0;
watch.on("changed", function () {
return count += 1;
});
touch.sync("" + folder + "/one.x");
count.should.eql(0);
return watch.check().then(function () {
count.should.eql(1);
});
})));
it("notices several changes at once", fixtures(withFileWatcher(function (watcher, folder) {
var countOne = 0;
var countTwo = 0;
watcher.watch("" + folder + "/sub/one.x").on("changed", function () {
return countOne += 1;
});
watcher.watch("" + folder + "/sub/two.x").on("changed", function () {
return countTwo += 1;
});
touch.sync("" + folder + "/sub/one.x");
touch.sync("" + folder + "/sub/two.x");
countOne.should.eql(0);
countTwo.should.eql(0);
return watcher.check().then(function () {
countOne.should.eql(1);
countTwo.should.eql(1);
});
})));
it("notices changes on a timer", fixtures(withFileWatcher(function (watcher, folder) {
var countOne = 0;
var countTwo = 0;
watcher.watch("" + folder + "/sub/one.x").on("changed", function () {
return countOne += 1;
});
watcher.watch("" + folder + "/sub/two.x").on("changed", function () {
return countTwo += 1;
});
touch.sync("" + folder + "/sub/one.x");
touch.sync("" + folder + "/sub/two.x");
countOne.should.eql(0);
countTwo.should.eql(0);
return Promise.delay(watcher.period + 10).then(function () {
countOne.should.eql(1);
countTwo.should.eql(1);
});
})));
it("queues stacked check() calls", fixtures(withFileWatcher(function (watcher, folder) {
var count = 0;
watcher.watch("" + folder + "/one.x").on("changed", function () {
return count += 1;
});
touch.sync("" + folder + "/one.x", { mtime: Date.now() + 1000 });
var visited = [false, false];
var x1 = watcher.check().then(function () {
count.should.eql(1);
touch.sync("" + folder + "/one.x", { mtime: Date.now() + 2000 });
visited[1].should.eql(false);
visited[0] = true;
});
var x2 = watcher.check().then(function () {
visited[0].should.eql(true);
visited[1] = true;
count.should.eql(2);
});
visited[0].should.eql(false);
visited[1].should.eql(false);
return Promise.all([x1, x2]);
})));
it("detects size changes", future(withTempFolder(withFileWatcher(function (watcher, folder) {
var now = Date.now() - 15000;
var write = function (data) {
fs.writeFileSync("" + folder + "/shifty.x", data);
touch.sync("" + folder + "/shifty.x", { mtime: now });
};
write("abcdefghij");
var count = 0;
watcher.watch("" + folder + "/shifty.x").on("changed", function () {
return count += 1;
});
write("klmnopqrst");
return watcher.check().then(function () {
count.should.eql(0);
write("abcdef");
return watcher.check();
}).then(function () {
count.should.eql(1);
});
}))));
it("can unwatch", future(withTempFolder(withFileWatcher(function (watcher, folder) {
touch.sync("" + folder + "/changes.x", { mtime: Date.now() - 15000 });
var watch = watcher.watch("" + folder + "/changes.x");
var count = 0;
watch.on("changed", function () {
return count += 1;
});
touch.sync("" + folder + "/changes.x", { mtime: Date.now() - 11000 });
return watcher.check().then(function () {
count.should.eql(1);
watcher.unwatch("" + folder + "/changes.x");
touch.sync("" + folder + "/changes.x", { mtime: Date.now() - 6000 });
return watcher.check();
}).then(function () {
count.should.eql(1);
});
}))));
});
//# sourceMappingURL=test_filewatcher.js.map