]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fixing zones tests to actually spawn new node processes for each timezone
authorTim Wood <washwithcare@gmail.com>
Fri, 26 Oct 2012 19:02:11 +0000 (08:02 -1100)
committerTim Wood <washwithcare@gmail.com>
Fri, 26 Oct 2012 19:02:11 +0000 (08:02 -1100)
#481

tasks/zone.js [new file with mode: 0644]
tasks/zones.js

diff --git a/tasks/zone.js b/tasks/zone.js
new file mode 100644 (file)
index 0000000..3038634
--- /dev/null
@@ -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]);
+        }
+    }
+};
index cea674cfbe2b614186f1fdfdcd40c16b21026e50..2084e80569a076f23a0378a555c9f5b91888a461 100644 (file)
@@ -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
+};