grunt-contrib-watch/test/tasks/watch_test.js

130 lines
4.9 KiB
JavaScript
Raw Normal View History

2012-10-16 07:16:16 +08:00
'use strict';
var grunt = require('grunt');
var path = require('path');
2012-12-14 11:18:37 +08:00
var fs = require('fs');
2013-02-13 11:36:16 +08:00
var helper = require('./helper');
2012-10-16 07:16:16 +08:00
2013-02-13 11:36:16 +08:00
var fixtures = helper.fixtures;
var useFixtures = ['multiTargets', 'oneTarget'];
2012-10-16 07:16:16 +08:00
2012-12-14 11:18:37 +08:00
function cleanUp() {
useFixtures.forEach(function(fixture) {
helper.cleanUp(fixture + '/node_modules');
});
2012-12-14 11:18:37 +08:00
}
2012-10-16 07:16:16 +08:00
exports.watchConfig = {
setUp: function(done) {
2012-12-14 11:18:37 +08:00
cleanUp();
useFixtures.forEach(function(fixture) {
fs.symlinkSync(path.join(__dirname, '../../node_modules'), path.join(fixtures, fixture, 'node_modules'));
});
done();
},
tearDown: function(done) {
2012-12-14 11:18:37 +08:00
cleanUp();
done();
},
2012-10-16 07:16:16 +08:00
oneTarget: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'oneTarget');
2013-02-13 11:36:16 +08:00
var assertWatch = helper.assertTask('watch', {cwd:cwd});
2012-10-16 07:16:16 +08:00
assertWatch(function() {
var write = 'var test = true;';
grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
2012-10-16 07:16:16 +08:00
}, function(result) {
2013-02-13 11:36:16 +08:00
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.');
2012-10-16 07:16:16 +08:00
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');
2013-02-13 11:36:16 +08:00
var assertWatch = helper.assertTask('watch', {cwd:cwd});
2012-10-16 07:16:16 +08:00
assertWatch(function() {
var write = 'var test = true;';
grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
2012-10-16 07:16:16 +08:00
}, function(result) {
2013-02-13 11:36:16 +08:00
helper.verboseLog(result);
2012-10-16 07:16:16 +08:00
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) {
2012-10-16 07:16:16 +08:00
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
2013-02-13 11:36:16 +08:00
var assertWatch = helper.assertTask('watch', {cwd:cwd});
2012-10-17 10:08:24 +08:00
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) {
2013-02-13 11:36:16 +08:00
helper.verboseLog(result);
2012-10-16 07:16:16 +08:00
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');
2013-02-13 11:36:16 +08:00
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) {
2013-02-13 11:36:16 +08:00
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(1);
var cwd = path.resolve(fixtures, 'multiTargets');
2013-02-13 11:36:16 +08:00
var assertWatch = helper.assertTask('watch', {cwd:cwd});
assertWatch(function() {
grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = false;');
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = true;');
}, 1000);
}, function(result) {
2013-02-13 11:36:16 +08:00
helper.verboseLog(result);
2013-02-22 07:28:42 +08:00
test.ok(result.indexOf('have been interrupted') !== -1, 'Task should have been interrupted.');
test.done();
});
},
failingTask: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'multiTargets');
2013-02-13 11:36:16 +08:00
var assertWatch = helper.assertTask('watch', {cwd:cwd});
assertWatch(function() {
grunt.file.write(path.join(cwd, 'lib', 'fail.js'), 'var fail = false;');
}, function(result) {
2013-02-13 11:36:16 +08:00
helper.verboseLog(result);
2012-11-01 08:20:13 +08:00
test.ok(result.toLowerCase().indexOf('fatal') !== -1, 'Task should have been fatal.');
test.equal(grunt.util._(result).count('Waiting...'), 2, 'Should have displayed "Wating..." twice');
test.done();
});
}
2012-10-16 07:16:16 +08:00
};