cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject js commit: CB-8129 Adds 'cover' grunt task to generate tests coverage report (close #95)
Date Tue, 23 Dec 2014 18:58:34 GMT
Repository: cordova-js
Updated Branches:
  refs/heads/master 2cc3d2909 -> c37d78d19


CB-8129 Adds 'cover' grunt task to generate tests coverage report (close #95)


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/c37d78d1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/c37d78d1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/c37d78d1

Branch: refs/heads/master
Commit: c37d78d191a5ab9d1457a344bb17c949600a429e
Parents: 2cc3d29
Author: Vladimir Kotikov <v-vlkoti@microsoft.com>
Authored: Fri Dec 5 17:10:50 2014 +0300
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue Dec 23 13:58:03 2014 -0500

----------------------------------------------------------------------
 .gitignore                       |   1 +
 Gruntfile.js                     |   1 +
 README.md                        |   4 ++
 package.json                     | 128 +++++++++++++++++-----------------
 tasks/cover.js                   |  34 +++++++++
 tasks/lib/test-jsdom-coverage.js | 119 +++++++++++++++++++++++++++++++
 tasks/test.js                    |   2 +-
 7 files changed, 225 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 2291753..a76a5ff 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ tags
 .DS_Store
 node_modules/
 .gitkeep
+coverage/

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/Gruntfile.js
----------------------------------------------------------------------
diff --git a/Gruntfile.js b/Gruntfile.js
index 754bad2..26290ec 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -68,4 +68,5 @@ module.exports = function(grunt) {
     grunt.registerTask('build', ['compile', 'jshint', 'whitespace-check']);
     grunt.registerTask('test', ['compile', '_test']);
     grunt.registerTask('btest', ['compile', '_btest']);
+    grunt.registerTask('cover', ['compile', '_cover']);
 };

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index b597e3c..4bc1e8a 100644
--- a/README.md
+++ b/README.md
@@ -104,6 +104,10 @@ To run them in the browser:
 
 Final testing should always be done with the [Mobile Spec test application](https://github.com/apache/cordova-mobile-spec).
 
+To get current tests coverage:
+
+    grunt cover --platformVersion=3.6.0
+
 # Integration
 
 ## Cordova

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index ab61fed..b804604 100644
--- a/package.json
+++ b/package.json
@@ -1,69 +1,71 @@
 {
-    "author": "Gord Tanner <gtanner@gmail.com> (http://github.com/gtanner)",
-    "name": "cordova-js",
-    "description": "Cordova JavaScript: a unified JavaScript layer for the Cordova suite
of projects enabling cross-platform native mobile development of applications using HTML,
CSS and JavaScript.",
-    "version": "3.7.3-dev",
-    "homepage": "http://cordova.apache.org",
-    "repository": {
-        "type": "git",
-        "url": "http://git-wip-us.apache.org/repos/asf/cordova-js.git"
+  "author": "Gord Tanner <gtanner@gmail.com> (http://github.com/gtanner)",
+  "name": "cordova-js",
+  "description": "Cordova JavaScript: a unified JavaScript layer for the Cordova suite of
projects enabling cross-platform native mobile development of applications using HTML, CSS
and JavaScript.",
+  "version": "3.7.3-dev",
+  "homepage": "http://cordova.apache.org",
+  "repository": {
+    "type": "git",
+    "url": "http://git-wip-us.apache.org/repos/asf/cordova-js.git"
+  },
+  "engines": {
+    "node": "~0.10.x"
+  },
+  "contributors": [
+    {
+      "name": "Fil Maj",
+      "email": "filmaj@apache.org"
     },
-    "engines": {
-        "node": "~0.10.x"
+    {
+      "name": "Jesse MacFadyen",
+      "email": "purplecabbage@apache.org"
     },
-    "contributors": [
-        {
-            "name": "Fil Maj",
-            "email": "filmaj@apache.org"
-        },
-        {
-            "name": "Jesse MacFadyen",
-            "email": "purplecabbage@apache.org"
-        },
-        {
-            "name": "Bryce Curtis",
-            "email": ""
-        },
-        {
-            "name": "Drew Walters",
-            "email": ""
-        },
-        {
-            "name": "Patrick Mueller",
-            "email": ""
-        },
-        {
-            "name": "Simon MacDonald",
-            "email": ""
-        },
-        {
-            "name": "Becky Gibson",
-            "email": ""
-        },
-        {
-            "name": "Anis Kadri",
-            "email": "anis@apache.org"
-        },
-        {
-            "name": "Dan Silivestru",
-            "email": "dansilivestru@apache.org"
-        },
-        {
-            "name": "Shazron Abdullah",
-            "email": "shazron@apache.org"
-        }
-    ],
-    "devDependencies": {
-        "jasmine-node": "1.14.5",
-        "jsdom-nogyp": "0.8.3",
-        "connect": "1.8.5",
-        "grunt": "0.4.1",
-        "grunt-contrib-clean": "0.6.0",
-        "grunt-contrib-jshint": "0.10.0"
+    {
+      "name": "Bryce Curtis",
+      "email": ""
     },
-    "dependencies": {
-        "uglify-js": "2.4.15",
-        "browserify": "7.x.x",
-        "through": "2.3.4"
+    {
+      "name": "Drew Walters",
+      "email": ""
+    },
+    {
+      "name": "Patrick Mueller",
+      "email": ""
+    },
+    {
+      "name": "Simon MacDonald",
+      "email": ""
+    },
+    {
+      "name": "Becky Gibson",
+      "email": ""
+    },
+    {
+      "name": "Anis Kadri",
+      "email": "anis@apache.org"
+    },
+    {
+      "name": "Dan Silivestru",
+      "email": "dansilivestru@apache.org"
+    },
+    {
+      "name": "Shazron Abdullah",
+      "email": "shazron@apache.org"
     }
+  ],
+  "devDependencies": {
+    "connect": "1.8.5",
+    "grunt": "0.4.1",
+    "grunt-contrib-clean": "0.6.0",
+    "grunt-contrib-jshint": "0.10.0",
+    "istanbul": "^0.3.4",
+    "jasmine-node": "1.14.5",
+    "jsdom-nogyp": "0.8.3",
+    "mkdirp": "^0.5.0"
+  },
+  "dependencies": {
+    "browserify": "3.46.0",
+    "through": "2.3.4",
+    "uglify-js": "^2.4.15"
+  }
 }

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/tasks/cover.js
----------------------------------------------------------------------
diff --git a/tasks/cover.js b/tasks/cover.js
new file mode 100644
index 0000000..59dc22c
--- /dev/null
+++ b/tasks/cover.js
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+
+try {
+    require('jasmine-node');
+    require('istanbul');
+} catch (e) {
+    console.error("\none of jasmine-node or istanbul packages is not installed, you need
to:\n" +
+        "\trun `npm install` from " + require('path').dirname(__dirname)+"\n");
+    process.exit(1);
+}
+
+module.exports = function(grunt) {
+    grunt.registerTask('_cover', 'measures test coverage using istanbul', function() {
+        var done = this.async();
+        require('./lib/test-jsdom-coverage')(done);
+    });
+};

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/tasks/lib/test-jsdom-coverage.js
----------------------------------------------------------------------
diff --git a/tasks/lib/test-jsdom-coverage.js b/tasks/lib/test-jsdom-coverage.js
new file mode 100644
index 0000000..f2fd047
--- /dev/null
+++ b/tasks/lib/test-jsdom-coverage.js
@@ -0,0 +1,119 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+/*global __coverage__: true*/
+
+var path             = require('path');
+var fs               = require('fs');
+var collect          = require('./collect');
+var istanbul         = require('istanbul');
+var mkdirp           = require('mkdirp');
+var jas              = require('jasmine-node');
+var testLibName      = path.join(__dirname, '..', '..', 'pkg', 'cordova.test.js');
+var testLib          = fs.readFileSync(testLibName, 'utf8');
+
+var jsdom    = require("jsdom-nogyp").jsdom;
+var document = jsdom(null, null, { url: 'file:///jsdomtest.info/a?b#c' });
+var window   = document.createWindow();
+
+module.exports = function(callback) {
+
+    console.log('starting node-based tests');
+
+    // put jasmine in scope
+    Object.keys(jas).forEach(function (key) {
+        this[key] = window[key] = global[key] = jas[key];
+    });
+
+    // Hack to fix jsdom with node v0.11.13+
+    delete String.prototype.normalize;
+
+    try {
+        // instrument loaded cordova.js before it will be loaded into DOM
+        var instrumenter = new istanbul.Instrumenter();
+        var generatedCode = instrumenter.instrumentSync(testLib);
+
+        eval(generatedCode);
+    }
+    catch (e) {
+        console.log("error eval()ing " + testLibName + ": " + e);
+        console.log(e.stack);
+        throw e;
+    }
+
+    // hijack require
+    require = window.cordova.require;
+    define  = window.cordova.define;
+
+    // load in our tests
+    var tests = [];
+    collect(path.join(__dirname, '..', '..', 'test'), tests);
+    for (var x in tests) {
+        eval(fs.readFileSync(tests[x], "utf-8"));
+    }
+
+    var env = jasmine.getEnv();
+    env.addReporter(new jas.TerminalReporter({
+        color: true,
+        onComplete: function(runner) {
+
+            // Check that global __coverage__ variable
+            // defined by istanbul is defined and not empty
+            if (__coverage__ || Object.keys(__coverage__).length === 0) {
+
+                var config = istanbul.config.loadObject(istanbul.config.defaultConfig());
+                var reportingDir = config.reporting.dir();
+                
+                mkdirp.sync(reportingDir);
+
+                var file = path.resolve(reportingDir, 'coverage.json'),
+                    collector = new istanbul.Collector(config),
+                    reporter = new istanbul.Reporter(config);
+
+                console.error('=============================================================================');
+                console.error('Writing coverage object [' + file + ']');
+
+                // regular expression is necessary to replace filename in report object
+                // generated by istanbul (smth. like "14843508345.js") with real filename
+                var correctFilenameRe = new RegExp(Object.keys(__coverage__)[0], "g");
+                var report = JSON.stringify(__coverage__).replace(correctFilenameRe, testLibName.replace(/\\/g,
'\\\\'));
+
+                fs.writeFileSync(file, report, 'utf8');
+                collector.add(JSON.parse(report));
+
+                console.error('Writing coverage reports at [' + path.resolve(reportingDir)
+ ']');
+                console.error('=============================================================================');
+                
+                reporter.addAll(["lcov", "text"]);
+                reporter.write(collector, true, function (win) {
+                    callback(runner.results().passed());
+                });
+            } else {
+                console.error('No coverage information was collected, exit without writing
coverage information');
+                callback(runner.results().passed());
+            }
+        }
+    }));
+
+    console.log("------------");
+    console.log("Unit Tests:");
+    env.execute();
+};
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/c37d78d1/tasks/test.js
----------------------------------------------------------------------
diff --git a/tasks/test.js b/tasks/test.js
index b1df54e..5e8306b 100644
--- a/tasks/test.js
+++ b/tasks/test.js
@@ -33,4 +33,4 @@ module.exports = function(grunt) {
         require('./lib/test-browser')();
         this.async(); // never finish.
     });
-}
+};


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message