2013-08-20 06:15:41 +02:00
|
|
|
var fs = require('fs');
|
2013-08-21 20:27:14 +02:00
|
|
|
var _ = require('third/underscore/underscore.js');
|
2013-11-26 16:39:58 +01:00
|
|
|
var Handlebars = require('handlebars');
|
2013-08-20 06:15:41 +02:00
|
|
|
|
2013-08-20 18:34:56 +02:00
|
|
|
// Run all the JS scripts in our test directory. Tests do NOT run
|
|
|
|
// in isolation.
|
2013-08-20 06:15:41 +02:00
|
|
|
|
|
|
|
var tests = fs.readdirSync(__dirname)
|
|
|
|
.filter(function (filename) { return (/\.js$/i).test(filename); })
|
|
|
|
.map(function (filename) { return filename.replace(/\.js$/i, ''); });
|
|
|
|
|
2013-08-21 23:53:00 +02:00
|
|
|
|
|
|
|
tests.sort();
|
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
var dependencies = [];
|
|
|
|
|
|
|
|
global.set_global = function (name, val) {
|
|
|
|
global[name] = val;
|
|
|
|
dependencies.push(name);
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
global.add_dependencies = function (dct) {
|
|
|
|
_.each(dct, function (fn, name) {
|
|
|
|
var obj = require(fn);
|
|
|
|
set_global(name, obj);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-17 20:13:25 +01:00
|
|
|
function template_dir() {
|
|
|
|
return __dirname + '/../../../../static/templates/';
|
|
|
|
}
|
|
|
|
|
|
|
|
global.make_sure_all_templates_have_been_compiled = function () {
|
|
|
|
var dir = template_dir();
|
|
|
|
var fns = fs.readdirSync(dir).filter(function (fn) {
|
|
|
|
return (/\.handlebars/).test(fn);
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(fns, function (fn) {
|
|
|
|
var name = fn.split('.')[0];
|
|
|
|
if (!Handlebars.templates[name]) {
|
|
|
|
throw "The file " + fn + " has no test coverage.";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-11-26 16:39:58 +01:00
|
|
|
global.use_template = function (name) {
|
|
|
|
if (Handlebars.templates === undefined) {
|
|
|
|
Handlebars.templates = {};
|
|
|
|
}
|
2014-01-17 20:13:25 +01:00
|
|
|
var data = fs.readFileSync(template_dir() + name + '.handlebars').toString();
|
2013-11-26 16:39:58 +01:00
|
|
|
Handlebars.templates[name] = Handlebars.compile(data);
|
|
|
|
};
|
|
|
|
|
2013-11-27 17:23:13 +01:00
|
|
|
var output_fn = '.test-js-with-node.html';
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
var data = '';
|
|
|
|
|
|
|
|
data += '<link href="./static/styles/zulip.css" rel="stylesheet">\n';
|
2013-12-11 18:53:05 +01:00
|
|
|
data += '<link href="./static/styles/thirdparty-fonts.css" rel="stylesheet">\n';
|
2013-11-27 17:23:13 +01:00
|
|
|
data += '<link href="./static/third/bootstrap/css/bootstrap.css" rel="stylesheet">\n';
|
2013-12-11 18:53:05 +01:00
|
|
|
data += '<style type="text/css">.collapse {height: inherit}</style>\n';
|
|
|
|
data += '<style type="text/css">body {width: 500px; margin: auto}</style>\n';
|
|
|
|
data += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
|
2013-11-27 17:23:13 +01:00
|
|
|
data += '<h1>Output of node unit tests</h1>\n';
|
|
|
|
fs.writeFileSync(output_fn, data);
|
|
|
|
}());
|
|
|
|
|
|
|
|
global.write_test_output = function (label, output) {
|
|
|
|
var data = '';
|
|
|
|
|
|
|
|
data += '<hr>';
|
|
|
|
data += '<h3>' + label + '</h3>';
|
|
|
|
data += output;
|
|
|
|
data += '\n';
|
|
|
|
fs.appendFileSync(output_fn, data);
|
|
|
|
};
|
|
|
|
|
2014-01-17 18:23:39 +01:00
|
|
|
global.append_test_output = function (output) {
|
|
|
|
fs.appendFileSync(output_fn, output);
|
|
|
|
};
|
|
|
|
|
2013-08-20 06:15:41 +02:00
|
|
|
tests.forEach(function (filename) {
|
2013-08-21 20:27:14 +02:00
|
|
|
if (filename === 'index') {
|
2013-08-20 06:15:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.info('running tests for ' + filename);
|
|
|
|
require('./' + filename);
|
2013-08-21 20:27:14 +02:00
|
|
|
|
|
|
|
dependencies.forEach(function (name) {
|
|
|
|
delete global[name];
|
|
|
|
});
|
|
|
|
dependencies = [];
|
2013-08-20 06:15:41 +02:00
|
|
|
});
|
2013-11-27 17:23:13 +01:00
|
|
|
|
|
|
|
console.info("To see more output, open " + output_fn);
|