forked from gulpjs/gulp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.js
179 lines (147 loc) · 5.08 KB
/
watch.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
'use strict';
var gulp = require('../');
var fs = require('graceful-fs');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var path = require('path');
var should = require('should');
require('mocha');
var outpath = path.join(__dirname, './out-fixtures');
describe('gulp', function() {
describe('watch()', function() {
beforeEach(rimraf.bind(null, outpath));
beforeEach(mkdirp.bind(null, outpath));
afterEach(rimraf.bind(null, outpath));
var tempFileContent = 'A test generated this file and it is safe to delete';
var writeTimeout = 125; // Wait for it to get to the filesystem
var writeFileWait = function(name, content, cb) {
if (!cb) {
cb = function() {};
}
setTimeout(function() {
fs.writeFile(name, content, cb);
}, writeTimeout);
};
it('should call the function when file changes: no options', function(done) {
// arrange
var tempFile = path.join(outpath, 'watch-func.txt');
fs.writeFile(tempFile, tempFileContent, function() {
// assert: it works if it calls done
var watcher = gulp.watch(tempFile, function(evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('changed');
evt.path.should.equal(path.resolve(tempFile));
watcher.end();
done();
});
// act: change file
writeFileWait(tempFile, tempFileContent + ' changed');
});
});
it('should call the function when file changes: w/ options', function(done) {
// arrange
var tempFile = path.join(outpath, 'watch-func-options.txt');
fs.writeFile(tempFile, tempFileContent, function() {
// assert: it works if it calls done
var watcher = gulp.watch(tempFile, {debounceDelay: 5}, function(evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('changed');
evt.path.should.equal(path.resolve(tempFile));
watcher.end();
done();
});
// act: change file
writeFileWait(tempFile, tempFileContent + ' changed');
});
});
it('should not drop options when no callback specified', function(done) {
// arrange
var tempFile = path.join(outpath, 'watch-func-nodrop-options.txt');
// by passing a cwd option, ensure options are not lost to gaze
var relFile = '../watch-func-nodrop-options.txt';
var cwd = outpath + '/subdir';
fs.writeFile(tempFile, tempFileContent, function() {
// assert: it works if it calls done
var watcher = gulp.watch(relFile, {debounceDelay: 5, cwd: cwd})
.on('change', function(evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('changed');
evt.path.should.equal(path.resolve(tempFile));
watcher.end();
done();
});
// act: change file
writeFileWait(tempFile, tempFileContent + ' changed');
});
});
it('should run many tasks: w/ options', function(done) {
// arrange
var tempFile = path.join(outpath, 'watch-task-options.txt');
var task1 = 'task1';
var task2 = 'task2';
var task3 = 'task3';
var a = 0;
var timeout = writeTimeout * 2.5;
fs.writeFile(tempFile, tempFileContent, function() {
gulp.task(task1, function() {
a++;
});
gulp.task(task2, function() {
a += 10;
});
gulp.task(task3, function() {
throw new Error('task3 called!');
});
// assert
setTimeout(function() {
a.should.equal(11); // task1 and task2
gulp.reset();
watcher.end();
done();
}, timeout);
// it works if it calls the task
var config = {debounceDelay: timeout / 2};
var watcher = gulp.watch(tempFile, config, [task1, task2]);
// act: change file
writeFileWait(tempFile, tempFileContent + ' changed');
});
});
it('should run many tasks: no options', function(done) {
// arrange
var tempFile = path.join(outpath, 'watch-many-tasks-no-options.txt');
var task1 = 'task1';
var task2 = 'task2';
var task3 = 'task3';
var a = 0;
var timeout = writeTimeout * 2.5;
fs.writeFile(tempFile, tempFileContent, function() {
gulp.task(task1, function() {
a++;
});
gulp.task(task2, function() {
a += 10;
});
gulp.task(task3, function() {
throw new Error('task3 called!');
});
// assert
setTimeout(function() {
a.should.equal(11); // task1 and task2
gulp.reset();
watcher.end();
done();
}, timeout);
// it works if it calls the task
var watcher = gulp.watch(tempFile, [task1, task2]);
// act: change file
writeFileWait(tempFile, tempFileContent + ' changed');
});
});
});
});