2014-02-03 16:48:04 +01:00
|
|
|
global.assert = require('assert');
|
2013-08-20 06:15:41 +02:00
|
|
|
var fs = require('fs');
|
2016-07-30 03:44:11 +02:00
|
|
|
var path = require('path');
|
2013-11-26 16:39:58 +01:00
|
|
|
var Handlebars = require('handlebars');
|
2016-06-29 20:23:26 +02:00
|
|
|
require('third/string-prototype-codepointat/codepointat.js');
|
2013-08-20 06:15:41 +02:00
|
|
|
|
2014-02-01 15:44:52 +01:00
|
|
|
global.Dict = require('js/dict');
|
|
|
|
global._ = require('third/underscore/underscore.js');
|
|
|
|
var _ = global._;
|
|
|
|
|
2016-07-30 17:00:12 +02:00
|
|
|
// Set up our namespace helpers.
|
|
|
|
var namespace = require('./namespace.js');
|
|
|
|
global.set_global = namespace.set_global;
|
|
|
|
global.patch_builtin = namespace.patch_builtin;
|
|
|
|
global.add_dependencies = namespace.add_dependencies;
|
|
|
|
|
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
|
|
|
|
2016-07-06 19:57:17 +02:00
|
|
|
var oneFileFilter = [];
|
|
|
|
var testsDifference = [];
|
|
|
|
if (process.argv[2] ) {
|
|
|
|
oneFileFilter = process.argv
|
|
|
|
.slice(2)
|
|
|
|
.map(function (filename) {return filename.replace(/\.js$/i, '');});
|
|
|
|
}
|
2013-08-20 06:15:41 +02:00
|
|
|
|
2016-07-30 16:43:28 +02:00
|
|
|
// tests_dir is where we find our specific unit tests (as opposed
|
|
|
|
// to framework code)
|
|
|
|
var tests_dir = __dirname.replace(/zjsunit/, 'node_tests');
|
|
|
|
|
|
|
|
var tests = fs.readdirSync(tests_dir)
|
2016-07-06 19:57:17 +02:00
|
|
|
.filter(function (filename) {return (/\.js$/i).test(filename);})
|
|
|
|
.map(function (filename) {return filename.replace(/\.js$/i, '');});
|
2013-08-21 23:53:00 +02:00
|
|
|
|
2016-07-06 19:57:17 +02:00
|
|
|
if (oneFileFilter.length > 0) {
|
|
|
|
tests = tests.filter(function (filename) {
|
|
|
|
return oneFileFilter.indexOf(filename) !== -1;
|
|
|
|
});
|
|
|
|
testsDifference = _.difference(oneFileFilter, tests);
|
|
|
|
}
|
2013-08-21 23:53:00 +02:00
|
|
|
tests.sort();
|
|
|
|
|
2014-01-17 20:13:25 +01:00
|
|
|
function template_dir() {
|
2015-10-13 23:34:50 +02:00
|
|
|
return __dirname + '/../../static/templates/';
|
2014-01-17 20:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2016-07-30 04:49:48 +02:00
|
|
|
function stylesheets() {
|
|
|
|
// TODO: Automatically get all relevant styles.
|
|
|
|
var data = '';
|
|
|
|
data += '<link href="../../static/styles/fonts.css" rel="stylesheet">\n';
|
|
|
|
data += '<link href="../../static/styles/portico.css" rel="stylesheet">\n';
|
|
|
|
data += '<link href="../../static/styles/thirdparty-fonts.css" rel="stylesheet">\n';
|
|
|
|
data += '<link href="../../static/styles/zulip.css" rel="stylesheet">\n';
|
|
|
|
data += '<link href="../../static/third/bootstrap/css/bootstrap.css" rel="stylesheet">\n';
|
|
|
|
data += '<link href="../../static/third/bootstrap-notify/css/bootstrap-notify.css" rel="stylesheet">\n';
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-07-30 03:44:11 +02:00
|
|
|
var mkdir_p = function (path) {
|
|
|
|
// This works like mkdir -p in Unix.
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(path);
|
|
|
|
} catch(e) {
|
|
|
|
if ( e.code !== 'EEXIST' ) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
};
|
|
|
|
|
|
|
|
var output_dir = (function () {
|
|
|
|
mkdir_p('var');
|
|
|
|
var dir = mkdir_p('var/test-js-with-node');
|
|
|
|
return dir;
|
|
|
|
}());
|
|
|
|
|
|
|
|
var output_fn = path.join(output_dir, 'output.html');
|
2016-07-30 04:49:48 +02:00
|
|
|
var index_fn = path.join(output_dir, 'index.html');
|
2013-11-27 17:23:13 +01:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
var data = '';
|
|
|
|
|
2016-07-30 04:49:48 +02:00
|
|
|
data += stylesheets();
|
2013-12-11 18:53:05 +01:00
|
|
|
data += '<style type="text/css">.collapse {height: inherit}</style>\n';
|
2014-02-01 17:09:06 +01:00
|
|
|
data += '<style type="text/css">body {width: 500px; margin: auto; overflow: scroll}</style>\n';
|
2013-12-11 18:53:05 +01:00
|
|
|
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);
|
2016-07-30 04:49:48 +02:00
|
|
|
|
|
|
|
data = '';
|
|
|
|
data += '<style type="text/css">body {width: 500px; margin: auto; overflow: scroll}</style>\n';
|
|
|
|
data += '<h2>Regular output</h2>\n';
|
|
|
|
data += '<p>Any test can output HTML to be viewed here:</p>\n';
|
|
|
|
data += '<a href="output.html">Output of non-template.js tests</a><br />';
|
|
|
|
data += '<hr />\n';
|
|
|
|
data += '<h2>Handlebar output</h2>\n';
|
|
|
|
data += '<p>These are specifically from templates.js</p>\n';
|
|
|
|
fs.writeFileSync(index_fn, data);
|
2013-11-27 17:23:13 +01:00
|
|
|
}());
|
|
|
|
|
|
|
|
global.write_test_output = function (label, output) {
|
|
|
|
var data = '';
|
|
|
|
|
|
|
|
data += '<hr>';
|
|
|
|
data += '<h3>' + label + '</h3>';
|
|
|
|
data += output;
|
|
|
|
data += '\n';
|
|
|
|
fs.appendFileSync(output_fn, data);
|
|
|
|
};
|
|
|
|
|
2016-07-30 04:49:48 +02:00
|
|
|
global.write_handlebars_output = (function () {
|
|
|
|
var last_label = '';
|
|
|
|
|
|
|
|
return function (label, output) {
|
|
|
|
if (last_label && (last_label >= label)) {
|
|
|
|
// This is kind of an odd requirement, but it allows us
|
|
|
|
// to render output on the fly in alphabetical order, and
|
|
|
|
// it has a nice side effect of making our source code
|
|
|
|
// easier to scan.
|
|
|
|
|
|
|
|
console.info(last_label);
|
|
|
|
console.info(label);
|
|
|
|
throw "Make sure your template tests are alphabetical in templates.js";
|
|
|
|
}
|
|
|
|
last_label = label;
|
|
|
|
|
|
|
|
var href = label + '.handlebars.html';
|
|
|
|
var fn = path.join(output_dir, href);
|
|
|
|
|
|
|
|
// Update the index
|
|
|
|
var a = '<a href="' + href + '">' + label + '</a><br />';
|
|
|
|
fs.appendFileSync(index_fn, a);
|
|
|
|
|
|
|
|
// Write out own HTML file.
|
|
|
|
var data = '';
|
|
|
|
data += stylesheets();
|
|
|
|
data += '<style type="text/css">body {width: 500px; margin: auto; overflow: scroll}</style>\n';
|
|
|
|
data += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
|
|
|
|
data += '<b>' + href + '</b><hr />\n';
|
|
|
|
data += output;
|
|
|
|
fs.writeFileSync(fn, data);
|
|
|
|
};
|
|
|
|
}());
|
2016-07-30 04:02:09 +02:00
|
|
|
|
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) {
|
|
|
|
console.info('running tests for ' + filename);
|
2016-07-30 16:43:28 +02:00
|
|
|
require(path.join(tests_dir, filename));
|
2016-07-30 17:00:12 +02:00
|
|
|
namespace.restore();
|
2013-08-20 06:15:41 +02:00
|
|
|
});
|
2013-11-27 17:23:13 +01:00
|
|
|
|
2016-07-06 19:57:17 +02:00
|
|
|
if (oneFileFilter.length > 0 && testsDifference.length > 0) {
|
|
|
|
testsDifference.forEach(function (filename) {
|
|
|
|
console.log(filename + " does not exist");
|
|
|
|
});
|
|
|
|
if (oneFileFilter.length > testsDifference.length) {
|
2016-07-30 04:49:48 +02:00
|
|
|
console.info("To see more output, open " + index_fn);
|
2016-07-06 19:57:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-30 04:49:48 +02:00
|
|
|
console.info("To see more output, open " + index_fn);
|
2016-07-06 19:57:17 +02:00
|
|
|
}
|