X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=node_modules%2Fhooker%2Fchild.js;fp=node_modules%2Fhooker%2Fchild.js;h=ae7dcf2fa5ace60337b6362da2626ea9445bc328;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/node_modules/hooker/child.js b/node_modules/hooker/child.js new file mode 100644 index 000000000..ae7dcf2fa --- /dev/null +++ b/node_modules/hooker/child.js @@ -0,0 +1,101 @@ +var path = require('path'); +var fs = require('fs'); +var nodeunit = require('nodeunit'); + +var filepaths = fs.readdirSync('test').map(function(filename) { + return path.join('test', filename); +}); + +var unfinished = {}; +var currentModule; +function sendMessage(message) { + process.stdout.write(JSON.stringify(message) + '\n'); +} + +// If an exception is thrown, let the parent process know and exit. +process.on('uncaughtException', function (e) { + sendMessage({error: [e.name, e.message, e.stack]}); + process.exit(); +}); + +// If Nodeunit explodes because a test was missing test.done(), handle it. +var unfinished = {}; +process.on('exit', function (e) { + var len = Object.keys(unfinished).length + if (len > 0) { + sendMessage({exit: ['UNFINISHED']}); + // process.reallyExit(len); + } else { + sendMessage({exit: ['finished']}); + } + // process.exit(); +}); + +nodeunit.reporters.test = { + run: function(files, options, callback) { + // Nodeunit needs absolute paths. + var paths = files.map(function (filepath) { + return path.resolve(filepath); + }); + nodeunit.runFiles(paths, { + // No idea. + testspec: undefined, + // Executed when the first test in a file is run. If no tests exist in + // the file, this doesn't execute. + moduleStart: function(name) { + // Keep track of this so that moduleDone output can be suppressed in + // cases where a test file contains no tests. + currentModule = name; + // Send back to the parent process. + sendMessage({moduleStart: [name.toString()]}); + }, + // Executed after a file is done being processed. This executes whether + // tests exist in the file or not. + moduleDone: function(name) { + // Abort if no tests actually ran. + if (name !== currentModule) { return; } + // Send back to the parent process. + sendMessage({moduleDone: [name.toString()]}); + }, + // Executed before each test is run. + testStart: function(name) { + // Keep track of the current test, in case test.done() was omitted + // and Nodeunit explodes. + unfinished[name] = name; + // Send back to the parent process. + sendMessage({testStart: [name.toString()]}); + }, + // Executed after each test and all its assertions are run. + testDone: function(name, assertions) { + delete unfinished[name]; + // Send back to the parent process. + sendMessage({testDone: [ + name.toString(), + assertions.failures(), + assertions.map(function(assertion) { + var e = assertion.error; + if (e) { + assertion.error = { + name: e.name, + message: e.message, + stack: e.stack + }; + } + return assertion; + }) + ]}); + }, + // Executed when everything is all done. + done: function (assertions) { + // Send back to the parent process. + sendMessage({done: [ + assertions.failures(), + assertions.duration, + assertions + ]}); + } + }); + } +} + +nodeunit.reporters.test.run(filepaths, {});