1 | var should = require("should");
|
2 | var path = require("path");
|
3 | var TestHelper = require("./helpers/TestHelper");
|
4 | var OrgDirectoryWatcher = require("../lib/DirectoryWatcher");
|
5 |
|
6 | var fixtures = path.join(__dirname, "fixtures");
|
7 | var testHelper = new TestHelper(fixtures);
|
8 |
|
9 | var openWatchers = [];
|
10 |
|
11 | DirectoryWatcher = function(p) {
|
12 | var d = new OrgDirectoryWatcher(p);
|
13 | openWatchers.push(d);
|
14 | var orgClose = d.close;
|
15 | d.close = function() {
|
16 | orgClose.call(this);
|
17 | var idx = openWatchers.indexOf(d);
|
18 | if(idx < 0)
|
19 | throw new Error("DirectoryWatcher was already closed");
|
20 | openWatchers.splice(idx, 1);
|
21 | }
|
22 | return d;
|
23 | }
|
24 |
|
25 | describe("DirectoryWatcher", function() {
|
26 | this.timeout(10000);
|
27 | beforeEach(testHelper.before);
|
28 | afterEach(testHelper.after);
|
29 | afterEach(function() {
|
30 | openWatchers.forEach(function(d) {
|
31 | console.log("DirectoryWatcher (" + d.path + ") was not closed.");
|
32 | d.close();
|
33 | })
|
34 | })
|
35 |
|
36 | it("should detect a file creation", function(done) {
|
37 | var d = new DirectoryWatcher(fixtures);
|
38 | var a = d.watch(path.join(fixtures, "a"));
|
39 | a.on("change", function(mtime) {
|
40 | mtime.should.be.type("number");
|
41 | a.close();
|
42 | done();
|
43 | });
|
44 | testHelper.tick(function() {
|
45 | testHelper.file("a");
|
46 | });
|
47 | });
|
48 |
|
49 | it("should detect a file change", function(done) {
|
50 | var d = new DirectoryWatcher(fixtures);
|
51 | testHelper.file("a");
|
52 | var a = d.watch(path.join(fixtures, "a"));
|
53 | a.on("change", function(mtime) {
|
54 | mtime.should.be.type("number");
|
55 | a.close();
|
56 | done();
|
57 | });
|
58 | testHelper.tick(function() {
|
59 | testHelper.file("a");
|
60 | });
|
61 | });
|
62 |
|
63 | it("should not detect a file change in initial scan", function(done) {
|
64 | testHelper.file("a");
|
65 | testHelper.tick(function() {
|
66 | var d = new DirectoryWatcher(fixtures);
|
67 | var a = d.watch(path.join(fixtures, "a"));
|
68 | a.on("change", function(mtime) {
|
69 | throw new Error("should not be detected");
|
70 | });
|
71 | testHelper.tick(function() {
|
72 | a.close();
|
73 | done();
|
74 | });
|
75 | });
|
76 | });
|
77 |
|
78 | it("should detect a file change in initial scan with start date", function(done) {
|
79 | var start = new Date();
|
80 | testHelper.file("a");
|
81 | testHelper.tick(function() {
|
82 | var d = new DirectoryWatcher(fixtures);
|
83 | var a = d.watch(path.join(fixtures, "a"), start);
|
84 | a.on("change", function(mtime) {
|
85 | a.close();
|
86 | done();
|
87 | });
|
88 | });
|
89 | });
|
90 |
|
91 | it("should detect a file change in initial scan without start date", function(done) {
|
92 | testHelper.file("a");
|
93 | testHelper.tick(function() {
|
94 | var d = new DirectoryWatcher(fixtures);
|
95 | var a = d.watch(path.join(fixtures, "a"));
|
96 | a.on("change", function(mtime) {
|
97 | throw new Error("should not be detected");
|
98 | });
|
99 | testHelper.tick(function() {
|
100 | a.close();
|
101 | done();
|
102 | });
|
103 | });
|
104 | });
|
105 |
|
106 | var timings = {
|
107 | slow: 300,
|
108 | fast: 50,
|
109 |
|
110 | };
|
111 | Object.keys(timings).forEach(function(name) {
|
112 | var time = timings[name];
|
113 | it("should detect multiple file changes (" + name + ")", function(done) {
|
114 | var d = new DirectoryWatcher(fixtures);
|
115 | testHelper.file("a");
|
116 | testHelper.tick(function() {
|
117 | var a = d.watch(path.join(fixtures, "a"));
|
118 | var count = 20;
|
119 | var wasChanged = false;
|
120 | a.on("change", function(mtime) {
|
121 | mtime.should.be.type("number");
|
122 | if(!wasChanged) return;
|
123 | wasChanged = false;
|
124 | if(count-- <= 0) {
|
125 | a.close();
|
126 | done();
|
127 | } else {
|
128 | testHelper.tick(time, function() {
|
129 | wasChanged = true;
|
130 | testHelper.file("a");
|
131 | });
|
132 | }
|
133 | });
|
134 | testHelper.tick(function() {
|
135 | wasChanged = true;
|
136 | testHelper.file("a");
|
137 | });
|
138 | });
|
139 | });
|
140 | });
|
141 | });
|
142 |
|
\ | No newline at end of file |