UNPKG

8.7 kBJavaScriptView Raw
1'use strict';
2
3var gaze = require('../lib/gaze.js');
4var grunt = require('grunt');
5var path = require('path');
6var fs = require('fs');
7
8// Clean up helper to call in setUp and tearDown
9function cleanUp(done) {
10 [
11 'sub/tmp.js',
12 'sub/tmp',
13 'sub/renamed.js',
14 'added.js',
15 'nested/added.js',
16 'nested/.tmp',
17 'nested/sub/added.js'
18 ].forEach(function(d) {
19 var p = path.resolve(__dirname, 'fixtures', d);
20 if (fs.existsSync(p)) { fs.unlinkSync(p); }
21 });
22 done();
23}
24
25exports.watch = {
26 setUp: function(done) {
27 process.chdir(path.resolve(__dirname, 'fixtures'));
28 cleanUp(done);
29 },
30 tearDown: cleanUp,
31 remove: function(test) {
32 test.expect(2);
33 gaze('**/*', function() {
34 this.remove(path.resolve(__dirname, 'fixtures', 'sub', 'two.js'));
35 this.remove(path.resolve(__dirname, 'fixtures'));
36 var result = this.relative(null, true);
37 test.deepEqual(result['sub/'], ['one.js']);
38 test.notDeepEqual(result['.'], ['one.js']);
39 this.close();
40 test.done();
41 });
42 },
43 changed: function(test) {
44 test.expect(1);
45 gaze('**/*', function(err, watcher) {
46 watcher.on('changed', function(filepath) {
47 var expected = path.relative(process.cwd(), filepath);
48 test.equal(path.join('sub', 'one.js'), expected);
49 watcher.close();
50 });
51 this.on('added', function() { test.ok(false, 'added event should not have emitted.'); });
52 this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); });
53 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
54 watcher.on('end', test.done);
55 });
56 },
57 added: function(test) {
58 test.expect(1);
59 gaze('**/*', function(err, watcher) {
60 watcher.on('added', function(filepath) {
61 var expected = path.relative(process.cwd(), filepath);
62 test.equal(path.join('sub', 'tmp.js'), expected);
63 watcher.close();
64 });
65 this.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); });
66 this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); });
67 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;');
68 watcher.on('end', test.done);
69 });
70 },
71 dontAddUnmatchedFiles: function(test) {
72 test.expect(2);
73 gaze('**/*.js', function(err, watcher) {
74 setTimeout(function() {
75 test.ok(true, 'Ended without adding a file.');
76 watcher.close();
77 }, 1000);
78 this.on('added', function(filepath) {
79 test.equal(path.relative(process.cwd(), filepath), path.join('sub', 'tmp.js'));
80 });
81 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp'), 'Dont add me!');
82 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'add me!');
83 watcher.on('end', test.done);
84 });
85 },
86 dontAddMatchedDirectoriesThatArentReallyAdded: function(test) {
87 // This is a regression test for a bug I ran into where a matching directory would be reported
88 // added when a non-matching file was created along side it. This only happens if the
89 // directory name doesn't occur in $PWD.
90 test.expect(1);
91 gaze('**/*', function(err, watcher) {
92 setTimeout(function() {
93 test.ok(true, 'Ended without adding a file.');
94 watcher.close();
95 }, 1000);
96 this.on('added', function(filepath) {
97 test.notEqual(path.relative(process.cwd(), filepath), path.join('nested', 'sub2'));
98 });
99 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'nested', '.tmp'), 'Wake up!');
100 watcher.on('end', test.done);
101 });
102 },
103 deleted: function(test) {
104 test.expect(1);
105 var tmpfile = path.resolve(__dirname, 'fixtures', 'sub', 'deleted.js');
106 fs.writeFileSync(tmpfile, 'var tmp = true;');
107 gaze('**/*', function(err, watcher) {
108 watcher.on('deleted', function(filepath) {
109 test.equal(path.join('sub', 'deleted.js'), path.relative(process.cwd(), filepath));
110 watcher.close();
111 });
112 this.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); });
113 this.on('added', function() { test.ok(false, 'added event should not have emitted.'); });
114 fs.unlinkSync(tmpfile);
115 watcher.on('end', test.done);
116 });
117 },
118 dontEmitTwice: function(test) {
119 test.expect(2);
120 gaze('**/*', function(err, watcher) {
121 watcher.on('all', function(status, filepath) {
122 var expected = path.relative(process.cwd(), filepath);
123 test.equal(path.join('sub', 'one.js'), expected);
124 test.equal(status, 'changed');
125 fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'));
126 setTimeout(function() {
127 fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'));
128 }, 1000);
129 // Give some time to accidentally emit before we close
130 setTimeout(function() { watcher.close(); }, 5000);
131 });
132 setTimeout(function() {
133 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
134 }, 1000);
135 watcher.on('end', test.done);
136 });
137 },
138 emitTwice: function(test) {
139 test.expect(2);
140 var times = 0;
141 gaze('**/*', function(err, watcher) {
142 watcher.on('all', function(status, filepath) {
143 test.equal(status, 'changed');
144 times++;
145 setTimeout(function() {
146 if (times < 2) {
147 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
148 } else {
149 watcher.close();
150 }
151 }, 1000);
152 });
153 setTimeout(function() {
154 fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
155 }, 1000);
156 watcher.on('end', test.done);
157 });
158 },
159 nonExistent: function(test) {
160 test.expect(1);
161 gaze('non/existent/**/*', function(err, watcher) {
162 test.ok(true);
163 test.done();
164 });
165 },
166 differentCWD: function(test) {
167 test.expect(1);
168 var cwd = path.resolve(__dirname, 'fixtures', 'sub');
169 gaze('two.js', {
170 cwd: cwd
171 }, function(err, watcher) {
172 watcher.on('changed', function(filepath) {
173 test.deepEqual(this.relative(), {'.':['two.js']});
174 watcher.close();
175 });
176 fs.writeFileSync(path.resolve(cwd, 'two.js'), 'var two = true;');
177 watcher.on('end', test.done);
178 });
179 },
180 addedEmitInSubFolders: function(test) {
181 test.expect(4);
182 var adds = [
183 { pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') },
184 { pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'added.js') },
185 { pattern: 'nested/**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'added.js') },
186 { pattern: 'nested/sub/*.js', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') },
187 ];
188 grunt.util.async.forEachSeries(adds, function(add, next) {
189 new gaze.Gaze(add.pattern, function(err, watcher) {
190 watcher.on('added', function(filepath) {
191 test.equal('added.js', path.basename(filepath));
192 fs.unlinkSync(filepath);
193 watcher.close();
194 next();
195 });
196 watcher.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); });
197 watcher.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); });
198 fs.writeFileSync(add.file, 'var added = true;');
199 });
200 }, function() {
201 test.done();
202 });
203 },
204 multipleWatchersSimultaneously: function(test) {
205 test.expect(2);
206 var did = 0;
207 var ready = 0;
208 var cwd = path.resolve(__dirname, 'fixtures', 'sub');
209 var watchers = [];
210 var timeout = setTimeout(function() {
211 test.ok(false, "Only " + did + " of " + ready + " watchers fired.");
212 test.done();
213 watchers.forEach(function(watcher) {
214 watcher.close();
215 });
216 }, 1000);
217
218 function isReady() {
219 ready++;
220 if (ready > 1) {
221 fs.writeFileSync(path.resolve(cwd, 'one.js'), 'var one = true;');
222 }
223 }
224 function isDone() {
225 did++;
226 if (did > 1) {
227 clearTimeout(timeout);
228 watchers.forEach(function(watcher) {
229 watcher.close();
230 });
231 test.done();
232 }
233 }
234 function changed(filepath) {
235 test.equal(path.join('sub', 'one.js'), path.relative(process.cwd(), filepath));
236 isDone();
237 }
238 for (var i = 0; i < 2; i++) {
239 watchers[i] = new gaze.Gaze('sub/one.js');
240 watchers[i].on('changed', changed);
241 watchers[i].on('ready', isReady);
242 }
243 },
244};