From: Tim Wood Date: Fri, 26 Oct 2012 19:02:11 +0000 (-1100) Subject: Fixing zones tests to actually spawn new node processes for each timezone X-Git-Tag: 2.0.0~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=034e63314ea35bbcea7c9a6996f60baff5e62de0;p=thirdparty%2Fmoment.git Fixing zones tests to actually spawn new node processes for each timezone #481 --- diff --git a/tasks/zone.js b/tasks/zone.js new file mode 100644 index 000000000..30386341e --- /dev/null +++ b/tasks/zone.js @@ -0,0 +1,123 @@ +var path = require('path'), + nodeunit = require('nodeunit'), + moment = require('../moment'); + + +module.exports = function (grunt) { + // placeholder for an array of timezones + var ALL_ZONES, + INITIAL_ZONE, + + failedZones = [], + failedTests = [], + + logTableWidths = [4, 6, 46, 12, 12], + + failedZoneCount = 0, + passedZoneCount = 0; + + /****************************** + Grunt task + ******************************/ + + grunt.registerTask('zone', 'Run the unit tests in the current timezone.', function () { + var done = this.async(); + getCurrentTimezone(function (zone) { + testZone(zone, function() { + logFinalOutput(); + done(); + }); + }); + }); + + /****************************** + Timezones + ******************************/ + + function getCurrentTimezone(cb) { + grunt.utils.spawn({ + cmd: "systemsetup", + args: ["gettimezone"] + }, function (err, result, code) { + cb(result.stdout.replace('Time Zone: ', '')); + }); + } + + /****************************** + Tests + ******************************/ + + function testZone(zone, cb) { + nodeunit.runFiles([path.join(process.cwd(), "test/moment"), path.join(process.cwd(), "test/lang")], { + testDone: function (name, assertions) { + if (assertions.failures()) { + failedTests.push([zone, name, assertions]); + } + }, + done: function (assertions) { + logZone(zone, assertions); + cb(); + } + }); + } + + /****************************** + Logging + ******************************/ + + function setupLoggingTable() { + var i, + longestZone = 0; + for (i = 0; i < ALL_ZONES.length; i++) { + longestZone = Math.max(longestZone, ALL_ZONES[i].length + 2); + } + logTableWidths[1] = longestZone; + grunt.log.writetableln(logTableWidths, ['', 'Zone', 'Offset', 'Pass', 'Fail']); + } + + function logFailedTest(zone, name, assertions) { + grunt.log.writeln(""); + grunt.log.error(zone + ' failed: ' + name); + assertions.forEach(function (a) { + var e = a.error; + if (a.failed()) { + if (a.message) { + grunt.log.error(a.message); + } + if (e && e.actual && e.expected && e.operator) { + grunt.log.error([e.actual, e.operator, e.expected].join(' ')); + } + } + }); + } + + function logZone(zone, assertions) { + var failed = assertions.failures(), + passed = assertions.length - failed, + status = failed ? "XX".red : "OK".green, + passMsg = passed + ' passed', + failMsg = failed ? (failed + ' failed').red : failed + ' failed', + offset = "" + (-moment().zone() / 60); + + grunt.log.writetableln(logTableWidths, [status, offset, zone, passMsg, failMsg]); + + if (failed) { + failedZoneCount++; + } else { + passedZoneCount++; + } + } + + function logFinalOutput() { + var i; + + if (!failedZoneCount) { + return; + } + + grunt.log.writeln(failedZoneCount + " failures"); + for (i = 0; i < failedTests.length; i++) { + logFailedTest.apply(null, failedTests[i]); + } + } +}; diff --git a/tasks/zones.js b/tasks/zones.js index cea674cfb..2084e8056 100644 --- a/tasks/zones.js +++ b/tasks/zones.js @@ -1,7 +1,6 @@ -var terminal = require('child_process').spawn('bash'), - path = require('path'), - util = require('util'), - nodeunit = require('nodeunit'); +var path = require('path'), + nodeunit = require('nodeunit'), + moment = require('../moment'); module.exports = function (grunt) { @@ -9,38 +8,24 @@ module.exports = function (grunt) { var ALL_ZONES, INITIAL_ZONE, - failedZones = [], - failedTests = [], - - logTableWidths = [4, 0, 12, 12], - - failedZoneCount = 0, - passedZoneCount = 0; + done; /****************************** Grunt task ******************************/ grunt.registerTask('zones', 'Run the unit tests in different timezones.', function () { - var done = this.async(); + done = this.async(); getCurrentTimezone(function (zone) { // save the initial timezone so we dont break our computers INITIAL_ZONE = zone; getAllTimezones(function (zones) { // store all the timezones ALL_ZONES = zones; - setupLoggingTable(); - // start running the tests nextTest(function () { - // log the total output - logFinalOutput(); - // reset the timezone like nothing ever happened - setTimezone(INITIAL_ZONE, function () { - grunt.log.writeln("Resetting timezone back to " + INITIAL_ZONE); - done(); - }); + resetTimezone(); }); }); }); @@ -50,6 +35,13 @@ module.exports = function (grunt) { Timezones ******************************/ + function resetTimezone() { + setTimezone(INITIAL_ZONE, function () { + grunt.log.writeln("Resetting timezone back to " + INITIAL_ZONE); + done(); + }); + } + function getCurrentTimezone(cb) { grunt.utils.spawn({ cmd: "systemsetup", @@ -97,70 +89,16 @@ module.exports = function (grunt) { } function testZone(zone, cb) { - nodeunit.runFiles([path.join(process.cwd(), "test/moment")], { - testDone: function (name, assertions) { - if (assertions.failures()) { - failedTests.push([zone, name, assertions]); - } - }, - done: function (assertions) { - logZone(zone, assertions); - cb(); - } - }); - } - - /****************************** - Logging - ******************************/ - - function setupLoggingTable() { - var i, - longestZone = 0; - for (i = 0; i < ALL_ZONES.length; i++) { - longestZone = Math.max(longestZone, ALL_ZONES[i].length + 2); - } - logTableWidths[1] = longestZone; - grunt.log.writetableln(logTableWidths, ['', 'Zone', 'Pass', 'Fail']); - } - - function logFailedTest(zone, name, assertions) { - grunt.log.writeln(""); - grunt.log.error(zone + ' failed: ' + name); - assertions.forEach(function (a) { - var e = a.error; - if (a.failed()) { - if (a.message) { - grunt.log.error(a.message); - } - if (e && e.actual && e.expected && e.operator) { - grunt.log.error([e.actual, e.operator, e.expected].join(' ')); - } + grunt.utils.spawn({ + cmd: "grunt", + args: ["zone"] + }, function (err, result, code) { + if (err) { + resetTimezone(); + throw err; } + console.log(result.stdout); + cb(); }); } - - function logZone(zone, assertions) { - var failed = assertions.failures(), - passed = assertions.length - failed, - status = failed ? "XX".red : "OK".green, - passMsg = passed + ' passed', - failMsg = failed ? (failed + ' failed').red : failed + ' failed'; - - grunt.log.writetableln(logTableWidths, [status, zone, passMsg, failMsg]); - - if (failed) { - failedZoneCount++; - } else { - passedZoneCount++; - } - } - - function logFinalOutput() { - var i; - grunt.log.writeln(failedZoneCount + " failures"); - for (i = 0; i < failedTests.length; i++) { - logFailedTest.apply(null, failedTests[i]); - } - } -}; \ No newline at end of file +};