2013-05-01 04:08:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var grunt = require('grunt');
|
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var http = require('http');
|
|
|
|
var helper = require('./helper');
|
|
|
|
|
|
|
|
var fixtures = helper.fixtures;
|
|
|
|
|
|
|
|
function cleanUp() {
|
|
|
|
helper.cleanUp([
|
2016-06-06 14:25:10 +08:00
|
|
|
'livereload/node_modules'
|
2013-05-01 04:08:10 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for requesting the live reload server
|
|
|
|
function request(port, done) {
|
|
|
|
var data = '';
|
|
|
|
var req = http.request({
|
|
|
|
hostname: 'localhost',
|
2016-06-06 14:25:10 +08:00
|
|
|
port: port
|
2013-05-01 04:08:10 +08:00
|
|
|
}, function(res) {
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
res.on('data', function(chunk) {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
res.on('end', function() {
|
|
|
|
done(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.livereload = {
|
|
|
|
setUp: function(done) {
|
|
|
|
cleanUp();
|
|
|
|
fs.symlinkSync(path.join(__dirname, '../../node_modules'), path.join(fixtures, 'livereload', 'node_modules'));
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
tearDown: function(done) {
|
|
|
|
cleanUp();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
basic: function(test) {
|
|
|
|
test.expect(4);
|
|
|
|
var resultData = '';
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:basic', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
|
|
|
request(35729, function(data) {
|
|
|
|
resultData += data;
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
2013-05-02 11:25:09 +08:00
|
|
|
result = helper.unixify(result);
|
2013-05-01 04:08:10 +08:00
|
|
|
helper.verboseLog(result);
|
|
|
|
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
|
2015-02-21 12:48:30 +08:00
|
|
|
test.ok(result.indexOf('Live reload server started on *:35729') !== -1,
|
2014-11-04 09:38:53 +08:00
|
|
|
'live reload server should have been started on port 35729.');
|
2013-05-01 04:08:10 +08:00
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
|
|
|
|
resultData = JSON.parse(resultData);
|
|
|
|
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
2014-11-07 11:04:17 +08:00
|
|
|
customhost: function(test) {
|
2013-05-01 04:08:10 +08:00
|
|
|
test.expect(4);
|
|
|
|
var resultData = '';
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
2014-11-07 11:04:17 +08:00
|
|
|
var assertWatch = helper.assertTask(['watch:customhost', '-v'], {cwd: cwd});
|
2013-05-01 04:08:10 +08:00
|
|
|
assertWatch([function() {
|
|
|
|
request(8675, function(data) {
|
|
|
|
resultData += data;
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
2013-05-02 11:25:09 +08:00
|
|
|
result = helper.unixify(result);
|
2013-05-01 04:08:10 +08:00
|
|
|
helper.verboseLog(result);
|
|
|
|
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
|
2015-02-21 12:44:23 +08:00
|
|
|
test.ok(result.indexOf('Live reload server started on localhost:8675') !== -1,
|
|
|
|
'live reload server should have been started on localhost:8675.');
|
2013-05-01 04:08:10 +08:00
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
|
|
|
|
resultData = JSON.parse(resultData);
|
|
|
|
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
2013-08-27 06:45:19 +08:00
|
|
|
differentfiles: function(test) {
|
|
|
|
test.expect(3);
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:differentfiles', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
2013-08-28 07:39:29 +08:00
|
|
|
}, function() {
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var two = true;');
|
2013-08-27 06:45:19 +08:00
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
|
|
|
|
test.ok(result.indexOf('Live reloading lib/two.js...') !== -1, 'live reload should have triggered on lib/two.js');
|
2014-11-04 09:38:53 +08:00
|
|
|
test.ok(!/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result),
|
|
|
|
'live reload should have cleared js file that was already reloaded');
|
2013-08-27 06:45:19 +08:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
2013-08-24 06:39:26 +08:00
|
|
|
multiplefiles: function(test) {
|
|
|
|
test.expect(4);
|
|
|
|
var resultData = '';
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:multiplefiles', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
|
|
|
request(9876, function(data) {
|
|
|
|
resultData += data;
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var two = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
|
|
|
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
|
2015-02-21 12:48:30 +08:00
|
|
|
test.ok(result.indexOf('Live reload server started on *:9876') !== -1,
|
2014-11-04 09:38:53 +08:00
|
|
|
'live reload server should have been started on port 9876.');
|
|
|
|
test.ok(/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result),
|
|
|
|
'live reload should have triggered on lib/one.js and lib/two.js');
|
2013-08-24 06:39:26 +08:00
|
|
|
resultData = JSON.parse(resultData);
|
|
|
|
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
2013-05-01 04:08:10 +08:00
|
|
|
nospawn: function(test) {
|
|
|
|
test.expect(4);
|
|
|
|
var resultData = '';
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:nospawn', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
2013-05-01 04:44:21 +08:00
|
|
|
request(1337, function(data) {
|
2013-05-01 04:08:10 +08:00
|
|
|
resultData += data;
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
2013-05-02 11:25:09 +08:00
|
|
|
result = helper.unixify(result);
|
2013-05-01 04:08:10 +08:00
|
|
|
helper.verboseLog(result);
|
|
|
|
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
|
2015-02-21 12:48:30 +08:00
|
|
|
test.ok(result.indexOf('Live reload server started on *:1337') !== -1,
|
2014-11-04 09:38:53 +08:00
|
|
|
'live reload server should have been started on port 1337.');
|
2013-05-08 03:17:21 +08:00
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
|
|
|
|
resultData = JSON.parse(resultData);
|
|
|
|
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
notasks: function(test) {
|
|
|
|
test.expect(3);
|
|
|
|
var resultData = '';
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:notasks', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
|
|
|
request(35729, function(data) {
|
|
|
|
resultData += data;
|
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
2015-02-21 12:48:30 +08:00
|
|
|
test.ok(result.indexOf('Live reload server started on *:35729') !== -1,
|
2014-11-04 09:38:53 +08:00
|
|
|
'live reload server should have been started on port 35729.');
|
2013-05-01 04:08:10 +08:00
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
|
|
|
|
resultData = JSON.parse(resultData);
|
|
|
|
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
2013-05-12 12:52:19 +08:00
|
|
|
onlytriggeron: function(test) {
|
|
|
|
test.expect(2);
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
2015-10-11 18:26:55 +08:00
|
|
|
request(35729, function() {
|
2013-05-12 12:52:19 +08:00
|
|
|
grunt.file.write(path.join(cwd, 'sass', 'one.scss'), '#one {}');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
2014-11-04 09:38:53 +08:00
|
|
|
test.ok(result.indexOf('Live reloading sass/one.scss') === -1,
|
|
|
|
'Should not trigger live reload on non livereload targets.');
|
|
|
|
test.ok(result.indexOf('Live reloading css/one.css') !== -1,
|
|
|
|
'Should trigger live reload when other tasks trigger livereload targets.');
|
2013-05-12 12:52:19 +08:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
2014-02-24 23:54:59 +08:00
|
|
|
livereloadOnErrorTrue: function(test) {
|
|
|
|
test.expect(1);
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:livereloadOnErrorTrue', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
2015-10-11 18:26:55 +08:00
|
|
|
request(35729, function() {
|
2014-02-24 23:54:59 +08:00
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'Should livereload when a task errors w/o flag');
|
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
livereloadOnErrorFalse: function(test) {
|
|
|
|
test.expect(1);
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:livereloadOnErrorFalse', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
2015-10-11 18:26:55 +08:00
|
|
|
request(35729, function() {
|
2014-02-24 23:54:59 +08:00
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
2014-11-04 09:38:53 +08:00
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') === -1,
|
|
|
|
'Should not livereload when a task errors with flag');
|
2014-02-24 23:54:59 +08:00
|
|
|
test.done();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
livereloadOnErrorFalseNoSpawn: function(test) {
|
|
|
|
test.expect(1);
|
|
|
|
var cwd = path.resolve(fixtures, 'livereload');
|
|
|
|
var assertWatch = helper.assertTask(['watch:livereloadOnErrorFalseNoSpawn', '-v'], {cwd: cwd});
|
|
|
|
assertWatch([function() {
|
2015-10-11 18:26:55 +08:00
|
|
|
request(35729, function() {
|
2014-02-24 23:54:59 +08:00
|
|
|
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var one = true;');
|
|
|
|
});
|
|
|
|
}], function(result) {
|
|
|
|
result = helper.unixify(result);
|
|
|
|
helper.verboseLog(result);
|
2014-11-04 09:38:53 +08:00
|
|
|
test.ok(result.indexOf('Live reloading lib/one.js...') === -1,
|
|
|
|
'Should not livereload when a task errors with flag and spawn=false');
|
2014-02-24 23:54:59 +08:00
|
|
|
test.done();
|
|
|
|
});
|
2016-06-06 14:25:10 +08:00
|
|
|
}
|
2013-05-01 04:08:10 +08:00
|
|
|
};
|