-
Notifications
You must be signed in to change notification settings - Fork 356
/
watch_test.js
192 lines (185 loc) · 7.27 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
'use strict';
var grunt = require('grunt');
var path = require('path');
var fs = require('fs');
var helper = require('./helper');
var _s = require('underscore.string');
var spawn = require('child_process').spawn;
var fixtures = helper.fixtures;
var useFixtures = ['multiTargets', 'oneTarget', 'atBegin', 'dateFormat'];
function cleanUp() {
useFixtures.forEach(function(fixture) {
helper.cleanUp(fixture + '/node_modules');
});
}
exports.watch = {
setUp: function(done) {
cleanUp();
useFixtures.forEach(function(fixture) {
fs.symlinkSync(path.join(__dirname, '../../node_modules'), path.join(fixtures, fixture, 'node_modules'));
});
done();
},
tearDown: function(done) {
cleanUp();
done();
},
atBegin: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'atBegin');
var assertWatch = helper.assertTask(['watch', '--debug'], {cwd: cwd});
assertWatch(function() {
// noop. Does not modify any watched files.
}, function(result) {
helper.verboseLog(result);
var firstIndex = result.indexOf('one has changed');
test.ok(firstIndex !== -1, 'Watch should have fired even though no file was changed.');
test.done();
});
},
dateFormat: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'dateFormat');
var assertWatch = helper.assertTask(['watch', '--debug'], {cwd: cwd});
assertWatch(function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
}, function(result) {
helper.verboseLog(result);
test.ok(result.indexOf('dateFormat has worked!') !== -1, 'Should have displayed a custom dateFormat.');
test.done();
});
},
oneTarget: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'oneTarget');
var assertWatch = helper.assertTask(['watch', '--debug'], {cwd: cwd});
assertWatch(function() {
var write = 'var test = true;';
grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
}, function(result) {
helper.verboseLog(result);
test.ok(result.indexOf('File "lib' + path.sep + 'one.js" changed') !== -1,
'Watch should have fired when oneTarget/lib/one.js has changed.');
test.ok(result.indexOf('I do absolutely nothing.') !== -1, 'echo task should have fired.');
test.done();
});
},
multiTargetsTriggerOneNotTwo: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch(function() {
var write = 'var test = true;';
grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
}, function(result) {
helper.verboseLog(result);
test.ok(result.indexOf('one has changed') !== -1, 'Only task echo:one should of emit.');
test.ok(result.indexOf('two has changed') === -1, 'Task echo:two should NOT emit.');
test.done();
});
},
multiTargetsSequentialFilesChangeTriggerBoth: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch([function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
}, function() {
grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var test = true;');
}], function(result) {
helper.verboseLog(result);
test.ok(result.indexOf('one has changed') !== -1, 'Task echo:one should of emit.');
test.ok(result.indexOf('two has changed') !== -1, 'Task echo:two should of emit.');
test.done();
});
},
multiTargetsSimultaneousFilesChangeTriggerBoth: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch([function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var test = true;');
}], function(result) {
helper.verboseLog(result);
test.ok(result.indexOf('one has changed') !== -1, 'Task echo:one should of emit.');
test.ok(result.indexOf('two has changed') !== -1, 'Task echo:two should of emit.');
test.done();
});
},
spawnOneAtATime: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch(function() {
grunt.file.write(path.join(cwd, 'lib', 'wait.js'), 'var wait = false;');
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib', 'wait.js'), 'var wait = true;');
}, 500);
}, function(result) {
helper.verboseLog(result);
test.ok(result.indexOf('I waited 2s') !== -1, 'Task should have waited 2s and only spawned once.');
test.done();
});
},
interrupt: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
function interrupt() {
grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = 1;');
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = 2;');
}, 1000);
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = 3;');
}, 2000);
}
var output = [];
var child = spawn(process.execPath, [process.argv[1], 'watch'], {cwd: cwd});
child.stdout.on('data', function(data) {
data = grunt.log.uncolor(data.toString());
output.push(data);
if (data.indexOf('Done') !== -1) {
child.kill('SIGINT');
} else if (data.indexOf('Waiting...') !== -1) {
setTimeout(interrupt, 1000);
}
});
child.on('exit', function() {
var result = output.join('\n');
helper.verboseLog(result);
var interruptMatches = result.match(/have been interrupted/g);
test.ok(interruptMatches && interruptMatches.length === 2, 'Task should have been interrupted 2 times.');
var unInterruptMatches = result.match(/I want to be interrupted/g);
test.ok(unInterruptMatches && unInterruptMatches.length === 1,
'Only the last time should be working (without interruption)');
test.done();
});
},
failingTask: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch(function() {
grunt.file.write(path.join(cwd, 'lib', 'fail.js'), 'var fail = false;');
}, function(result) {
helper.verboseLog(result);
test.ok(result.toLowerCase().indexOf('fatal') !== -1, 'Task should have been fatal.');
test.equal(_s.count(result, 'Waiting...'), 2, 'Should have displayed "Wating..." twice');
test.done();
});
},
cwd: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask('watch:cwd', {cwd: cwd});
assertWatch(function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
}, function(result) {
helper.verboseLog(result);
test.ok(result.toLowerCase().indexOf('cwd works') !== -1, 'Task should have shown "cwd works".');
test.equal(_s.count(result, 'Waiting...'), 2, 'Should have displayed "Wating..." twice');
test.done();
});
},
};