From: Alex Lam S.L Date: Fri, 11 May 2018 12:15:34 +0000 (+0800) Subject: replace `mocha` dependency (#3131) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=7b59b2f5b299893aaf7130af0498c8f477b25a4b;p=UglifyJS.git replace `mocha` dependency (#3131) --- diff --git a/package.json b/package.json index 96413a44..792428c2 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "devDependencies": { "acorn": "~5.5.3", - "mocha": "~3.5.1", + "colors": "~1.2.5", "semver": "~5.5.0" }, "scripts": { diff --git a/test/mocha.js b/test/mocha.js index fb8c3841..9902d653 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -1,24 +1,102 @@ +var colors = require("colors"); var fs = require("fs"); -var Mocha = require("mocha"); -var path = require("path"); -// Instantiate a Mocha instance -var mocha = new Mocha({ - timeout: 5000 -}); -var testDir = __dirname + "/mocha/"; +var config = { + limit: 5000, + timeout: function(limit) { + this.limit = limit; + } +}; +var tasks = []; +var titles = []; +describe = function(title, fn) { + config = Object.create(config); + titles.push(title); + fn.call(config); + titles.pop(); + config = Object.getPrototypeOf(config); +}; +it = function(title, fn) { + fn.limit = config.limit; + fn.titles = titles.slice(); + fn.titles.push(title); + tasks.push(fn); +}; -// Add each .js file to the Mocha instance -fs.readdirSync(testDir).filter(function(file) { +fs.readdirSync("test/mocha").filter(function(file) { return /\.js$/.test(file); }).forEach(function(file) { - mocha.addFile(path.join(testDir, file)); + require("./mocha/" + file); }); -module.exports = function() { - mocha.run(function(failures) { - if (failures) process.on("exit", function() { - process.exit(failures); +function log_titles(log, current, marker) { + var indent = ""; + var writing = false; + for (var i = 0; i < current.length; i++, indent += " ") { + if (titles[i] != current[i]) writing = true; + if (writing) log(indent + (i == current.length - 1 && marker || "") + current[i]); + } + titles = current; +} + +var errors = []; +var total = tasks.length; +titles = []; +process.nextTick(function run() { + var task = tasks.shift(); + if (task) try { + var elapsed = Date.now(); + var timer; + var done = function() { + clearTimeout(timer); + done = function() {}; + elapsed = Date.now() - elapsed; + if (elapsed > task.limit) { + throw new Error("Timed out: " + elapsed + "ms > " + task.limit + "ms"); + } + log_titles(console.log, task.titles, colors.green('\u221A ')); + process.nextTick(run); + }; + if (task.length) { + task.timeout = function(limit) { + clearTimeout(timer); + task.limit = limit; + timer = setTimeout(function() { + raise(new Error("Timed out: exceeds " + limit + "ms")); + }, limit); + }; + task.timeout(task.limit); + task.call(task, done); + } else { + task.timeout = config.timeout; + task.call(task); + done(); + } + } catch (err) { + raise(err); + } else if (errors.length) { + console.error(); + console.log(colors.red(errors.length + " test(s) failed!")); + titles = []; + errors.forEach(function(titles, index) { + console.error(); + log_titles(console.error, titles, (index + 1) + ") "); + var lines = titles.error.stack.split('\n'); + console.error(colors.red(lines[0])); + console.error(lines.slice(1).join("\n")); }); - }); -}; + process.exit(1); + } else { + console.log(); + console.log(colors.green(total + " test(s) passed.")); + } + + function raise(err) { + clearTimeout(timer); + done = function() {}; + task.titles.error = err; + errors.push(task.titles); + log_titles(console.log, task.titles, colors.red('\u00D7 ')); + process.nextTick(run); + } +}); diff --git a/test/run-tests.js b/test/run-tests.js index b31c33bc..6043f635 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -18,9 +18,8 @@ if (failures) { console.error("!!! " + Object.keys(failed_files).join(", ")); process.exit(1); } - -var mocha_tests = require("./mocha.js"); -mocha_tests(); +console.log(); +require("./mocha.js"); /* -----[ utils ]----- */