-
Notifications
You must be signed in to change notification settings - Fork 167
/
watch_test.js
342 lines (323 loc) · 11.5 KB
/
watch_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'use strict';
var gaze = require('../lib/gaze.js');
var grunt = require('grunt');
var path = require('path');
var fs = require('fs');
// Clean up helper to call in setUp and tearDown
function cleanUp (done) {
[
'sub/tmp.js',
'sub/tmp',
'sub/renamed.js',
'added.js',
'nested/added.js',
'nested/.tmp',
'nested/sub/added.js',
].forEach(function (d) {
try {
var p = path.resolve(__dirname, 'fixtures', d);
if (fs.existsSync(p)) { fs.unlinkSync(p); }
} catch (err) {
console.log('Caught an error at the end of tests we dont care about:', err.message);
}
});
try {
grunt.file.delete(path.resolve(__dirname, 'fixtures', 'new_dir'));
} catch (err) {
console.log('Caught an error at the end of tests we dont care about:', err.message);
}
done();
}
exports.watch = {
setUp: function (done) {
process.chdir(path.resolve(__dirname, 'fixtures'));
cleanUp(done);
},
tearDown: cleanUp,
remove: function (test) {
test.expect(2);
gaze('**/*', function () {
this.remove(path.resolve(__dirname, 'fixtures', 'sub', 'two.js'));
this.remove(path.resolve(__dirname, 'fixtures'));
var result = this.relative(null, true);
test.deepEqual(result['sub/'], ['one.js']);
test.notDeepEqual(result['.'], ['one.js']);
this.on('end', test.done);
this.close();
});
},
changed: function (test) {
test.expect(1);
gaze('**/*', function (err, watcher) {
watcher.on('changed', function (filepath) {
var expected = path.relative(process.cwd(), filepath);
test.equal(path.join('sub', 'one.js'), expected);
watcher.close();
});
this.on('added', function () { test.ok(false, 'added event should not have emitted.'); });
this.on('deleted', function () { test.ok(false, 'deleted event should not have emitted.'); });
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
watcher.on('end', test.done);
});
},
added: function (test) {
test.expect(1);
gaze('**/*', function (err, watcher) {
watcher.on('added', function (filepath) {
var expected = path.relative(process.cwd(), filepath);
test.equal(path.join('sub', 'tmp.js'), expected);
watcher.close();
});
this.on('changed', function () { test.ok(false, 'changed event should not have emitted.'); });
this.on('deleted', function () { test.ok(false, 'deleted event should not have emitted.'); });
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;');
watcher.on('end', test.done);
});
},
dontAddUnmatchedFiles: function (test) {
test.expect(2);
gaze('**/*.js', function (err, watcher) {
setTimeout(function () {
test.ok(true, 'Ended without adding a file.');
watcher.close();
}, 1000);
this.on('added', function (filepath) {
test.equal(path.relative(process.cwd(), filepath), path.join('sub', 'tmp.js'));
});
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp'), 'Dont add me!');
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'add me!');
watcher.on('end', test.done);
});
},
dontAddMatchedDirectoriesThatArentReallyAdded: function (test) {
// This is a regression test for a bug I ran into where a matching directory would be reported
// added when a non-matching file was created along side it. This only happens if the
// directory name doesn't occur in $PWD.
test.expect(1);
gaze('**/*', function (err, watcher) {
setTimeout(function () {
test.ok(true, 'Ended without adding a file.');
watcher.close();
}, 1000);
this.on('added', function (filepath) {
test.notEqual(path.relative(process.cwd(), filepath), path.join('nested', 'sub2'));
});
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'nested', '.tmp'), 'Wake up!');
watcher.on('end', test.done);
});
},
deleted: function (test) {
test.expect(1);
var tmpfile = path.resolve(__dirname, 'fixtures', 'sub', 'deleted.js');
fs.writeFileSync(tmpfile, 'var tmp = true;');
gaze('**/*', function (err, watcher) {
watcher.on('deleted', function (filepath) {
test.equal(path.join('sub', 'deleted.js'), path.relative(process.cwd(), filepath));
watcher.close();
});
this.on('changed', function () { test.ok(false, 'changed event should not have emitted.'); });
this.on('added', function () { test.ok(false, 'added event should not have emitted.'); });
fs.unlinkSync(tmpfile);
watcher.on('end', test.done);
});
},
dontEmitTwice: function (test) {
test.expect(2);
gaze('**/*', function (err, watcher) {
watcher.on('all', function (status, filepath) {
var expected = path.relative(process.cwd(), filepath);
test.equal(path.join('sub', 'one.js'), expected);
test.equal(status, 'changed');
fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'));
setTimeout(function () {
fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'));
}, 1000);
// Give some time to accidentally emit before we close
setTimeout(function () { watcher.close(); }, 5000);
});
setTimeout(function () {
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
}, 1000);
watcher.on('end', test.done);
});
},
emitTwice: function (test) {
test.expect(2);
var times = 0;
gaze('**/*', function (err, watcher) {
watcher.on('all', function (status, filepath) {
test.equal(status, 'changed');
times++;
setTimeout(function () {
if (times < 2) {
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
} else {
watcher.close();
}
}, 1000);
});
setTimeout(function () {
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
}, 1000);
watcher.on('end', test.done);
});
},
nonExistent: function (test) {
test.expect(1);
gaze('non/existent/**/*', function (err, watcher) {
test.ok(true);
watcher.on('end', test.done);
watcher.close();
});
},
differentCWD: function (test) {
test.expect(1);
var cwd = path.resolve(__dirname, 'fixtures', 'sub');
gaze('two.js', {
cwd: cwd
}, function (err, watcher) {
watcher.on('changed', function (filepath) {
test.deepEqual(this.relative(), {'.': ['two.js']});
watcher.close();
});
fs.writeFileSync(path.resolve(cwd, 'two.js'), 'var two = true;');
watcher.on('end', test.done);
});
},
addedEmitInSubFolders: function (test) {
test.expect(4);
var adds = [
{ pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') },
{ pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'added.js') },
{ pattern: 'nested/**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'added.js') },
{ pattern: 'nested/sub/*.js', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') },
];
grunt.util.async.forEachSeries(adds, function (add, next) {
new gaze.Gaze(add.pattern, function (err, watcher) {
watcher.on('added', function (filepath) {
test.equal('added.js', path.basename(filepath));
fs.unlinkSync(filepath);
watcher.close();
next();
});
watcher.on('changed', function () { test.ok(false, 'changed event should not have emitted.'); });
watcher.on('deleted', function () { test.ok(false, 'deleted event should not have emitted.'); });
fs.writeFileSync(add.file, 'var added = true;');
});
}, function () {
test.done();
});
},
multipleWatchersSimultaneously: function (test) {
test.expect(2);
var did = 0;
var ready = 0;
var cwd = path.resolve(__dirname, 'fixtures', 'sub');
var watchers = [];
var timeout = setTimeout(function () {
test.ok(false, 'Only ' + did + ' of ' + ready + ' watchers fired.');
test.done();
watchers.forEach(function (watcher) {
watcher.close();
});
}, 1000);
function isReady () {
ready++;
if (ready > 1) {
fs.writeFileSync(path.resolve(cwd, 'one.js'), 'var one = true;');
}
}
function isDone () {
did++;
if (did > 1) {
clearTimeout(timeout);
watchers.forEach(function (watcher) {
watcher.close();
});
test.done();
}
}
function changed (filepath) {
test.equal(path.join('sub', 'one.js'), path.relative(process.cwd(), filepath));
isDone();
}
for (var i = 0; i < 2; i++) {
watchers[i] = new gaze.Gaze('sub/one.js');
watchers[i].on('changed', changed);
watchers[i].on('ready', isReady);
}
},
mkdirThenAddFile: function (test) {
test.expect(2);
var expected = [
'new_dir',
'new_dir/other.js',
];
gaze('**/*.js', function (err, watcher) {
watcher.on('all', function (status, filepath) {
var expect = expected.shift();
test.equal(path.relative(process.cwd(), filepath), expect);
if (expected.length === 1) {
// Ensure the new folder is being watched correctly after initial add
setTimeout(function () {
fs.writeFileSync('new_dir/dontmatch.txt', '');
setTimeout(function () {
fs.writeFileSync('new_dir/other.js', '');
}, 1000);
}, 1000);
}
if (expected.length < 1) { watcher.close(); }
});
fs.mkdirSync('new_dir'); // fs.mkdirSync([folder]) seems to behave differently than grunt.file.write('[folder]/[file]')
watcher.on('end', test.done);
});
},
mkdirThenAddFileWithGruntFileWrite: function (test) {
test.expect(3);
var expected = [
'new_dir',
'new_dir/tmp.js',
'new_dir/other.js',
];
gaze('**/*.js', function (err, watcher) {
watcher.on('all', function (status, filepath) {
var expect = expected.shift();
test.equal(path.relative(process.cwd(), filepath), expect);
if (expected.length === 1) {
// Ensure the new folder is being watched correctly after initial add
setTimeout(function () {
fs.writeFileSync('new_dir/dontmatch.txt', '');
setTimeout(function () {
fs.writeFileSync('new_dir/other.js', '');
}, 1000);
}, 1000);
}
if (expected.length < 1) { watcher.close(); }
});
grunt.file.write('new_dir/tmp.js', '');
watcher.on('end', test.done);
});
},
enoentSymlink: function (test) {
test.expect(1);
fs.mkdirSync(path.resolve(__dirname, 'fixtures', 'new_dir'));
try {
fs.symlinkSync(path.resolve(__dirname, 'fixtures', 'not-exists.js'), path.resolve(__dirname, 'fixtures', 'new_dir', 'not-exists-symlink.js'));
} catch (err) {
// If we cant create symlinks, just ignore this tests (likely needs admin on win)
test.ok(true);
return test.done();
}
gaze('**/*', function () {
test.ok(true);
this.on('end', test.done);
this.close();
});
},
};
// These tests on Windows trigger the folder of a file added to that folder
// When they shouldn't. So skipping them for now ;-;
if (process.platform === 'win32') {
delete exports.watch['mkdirThenAddFile'];
delete exports.watch['mkdirThenAddFileWithGruntFileWrite'];
}