Extracted zjsunit/finder.js

This introduces a very minor different in behavior if you specify
an invalid filename as a command line argument.  We now show
warnings for those *before* running the rest of the tests.
This commit is contained in:
Steve Howell 2016-07-30 09:14:42 -07:00 committed by Tim Abbott
parent 643f2e03e0
commit 2e254547b2
2 changed files with 64 additions and 41 deletions

View File

@ -0,0 +1,53 @@
var finder = (function () {
var exports = {};
var _ = require('third/underscore/underscore.js');
var fs = require('fs');
var path = require('path');
exports.find_files_to_run = function () {
var oneFileFilter = [];
var testsDifference = [];
if (process.argv[2] ) {
oneFileFilter = process.argv
.slice(2)
.map(function (filename) {return filename.replace(/\.js$/i, '');});
}
// 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)
.filter(function (filename) {return (/\.js$/i).test(filename);})
.map(function (filename) {return filename.replace(/\.js$/i, '');});
if (oneFileFilter.length > 0) {
tests = tests.filter(function (filename) {
return oneFileFilter.indexOf(filename) !== -1;
});
testsDifference = _.difference(oneFileFilter, tests);
}
testsDifference.forEach(function (filename) {
console.log(filename + " does not exist");
});
tests.sort();
var files = tests.map(function (fn) {
var obj = {};
obj.name = fn;
obj.full_name = path.join(tests_dir, fn);
return obj;
});
return files;
};
return exports;
}());
module.exports = finder;

View File

@ -1,12 +1,17 @@
global.assert = require('assert'); global.assert = require('assert');
var fs = require('fs');
var path = require('path');
require('third/string-prototype-codepointat/codepointat.js'); require('third/string-prototype-codepointat/codepointat.js');
global.Dict = require('js/dict'); global.Dict = require('js/dict');
global._ = require('third/underscore/underscore.js'); global._ = require('third/underscore/underscore.js');
var _ = global._; var _ = global._;
// Find the files we need to run.
var finder = require('./finder.js');
var files = finder.find_files_to_run(); // may write to console
if (_.isEmpty(files)) {
throw "No tests found";
}
// Set up our namespace helpers. // Set up our namespace helpers.
var namespace = require('./namespace.js'); var namespace = require('./namespace.js');
global.set_global = namespace.set_global; global.set_global = namespace.set_global;
@ -24,48 +29,13 @@ global.write_handlebars_output = output.write_handlebars_output;
global.write_test_output = output.write_test_output; global.write_test_output = output.write_test_output;
global.append_test_output = output.append_test_output; global.append_test_output = output.append_test_output;
// Run all the JS scripts in our test directory. Tests do NOT run
// in isolation.
var oneFileFilter = [];
var testsDifference = [];
if (process.argv[2] ) {
oneFileFilter = process.argv
.slice(2)
.map(function (filename) {return filename.replace(/\.js$/i, '');});
}
// 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)
.filter(function (filename) {return (/\.js$/i).test(filename);})
.map(function (filename) {return filename.replace(/\.js$/i, '');});
if (oneFileFilter.length > 0) {
tests = tests.filter(function (filename) {
return oneFileFilter.indexOf(filename) !== -1;
});
testsDifference = _.difference(oneFileFilter, tests);
}
tests.sort();
output.start_writing(); output.start_writing();
tests.forEach(function (filename) { files.forEach(function (file) {
console.info('running tests for ' + filename); console.info('running tests for ' + file.name);
require(path.join(tests_dir, filename)); require(file.full_name);
namespace.restore(); namespace.restore();
}); });
if (oneFileFilter.length > 0 && testsDifference.length > 0) { console.info("To see more output, open " + output.index_fn);
testsDifference.forEach(function (filename) {
console.log(filename + " does not exist");
});
if (oneFileFilter.length > testsDifference.length) {
console.info("To see more output, open " + output.index_fn);
}
} else {
console.info("To see more output, open " + output.index_fn);
}