aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorcainus <[email protected]>2013-03-27 23:48:04 -0700
committercainus <[email protected]>2013-03-27 23:48:04 -0700
commit112119e43cb048cfa0dbd98d6e03833b8ca4b619 (patch)
tree1bdb90068b87229dfa04a7a217e907d856ed0981 /lib
parent86f733351d4e920a44e9682f105034628f6b0b4d (diff)
downloadnode-coveralls-112119e43cb048cfa0dbd98d6e03833b8ca4b619.tar.xz
node-coveralls-112119e43cb048cfa0dbd98d6e03833b8ca4b619.zip
changed to use lcov input format only.
Diffstat (limited to 'lib')
-rw-r--r--lib/convertLcovToCoveralls.js90
-rw-r--r--lib/parser.js131
-rw-r--r--lib/sendToCoveralls.js11
3 files changed, 232 insertions, 0 deletions
diff --git a/lib/convertLcovToCoveralls.js b/lib/convertLcovToCoveralls.js
new file mode 100644
index 0000000..cd3ee6d
--- /dev/null
+++ b/lib/convertLcovToCoveralls.js
@@ -0,0 +1,90 @@
+var TRAVIS_JOB_ID = process.env.TRAVIS_JOB_ID || 'unknown';
+var fs = require('fs');
+var lcovParse = require('./parser');
+
+var detailsToCoverage = function(length, details){
+ var coverage = new Array(length);
+ details.forEach(function(obj){
+ coverage[obj.line] = obj.hit;
+ });
+ return coverage;
+};
+
+var convertLcovFileObject = function(file, filepath){
+ var path = filepath + "/" + file.file;
+ var source = fs.readFileSync(path, 'utf8');
+ var lines = source.split("\n");
+ var coverage = detailsToCoverage(lines.length, file.lines.details);
+ return { name : file.file,
+ source : source,
+ coverage : coverage };
+};
+
+var convertLcovToCoveralls = function(input, filepath){
+ filepath = filepath || 'lib';
+ if (filepath[0] !== '/'){
+ filepath = process.cwd() + '/' + filepath;
+ }
+ var parsed = lcovParse(input);
+ var postJson = {
+ service_job_id : TRAVIS_JOB_ID,
+ service_name : "travis-ci",
+ source_files : []
+ };
+ parsed.forEach(function(file){
+ postJson.source_files.push(convertLcovFileObject(file, filepath));
+ });
+ return postJson;
+};
+
+module.exports = convertLcovToCoveralls;
+
+/* example coveralls json file
+
+
+{
+ "service_job_id": "1234567890",
+ "service_name": "travis-ci",
+ "source_files": [
+ {
+ "name": "example.rb",
+ "source": "def four\n 4\nend",
+ "coverage": [null, 1, null]
+ },
+ {
+ "name": "two.rb",
+ "source": "def seven\n eight\n nine\nend",
+ "coverage": [null, 1, 0, null]
+ }
+ ]
+}
+
+
+example output from lcov parser:
+
+ [
+ {
+ "file": "index.js",
+ "lines": {
+ "found": 0,
+ "hit": 0,
+ "details": [
+ {
+ "line": 1,
+ "hit": 1
+ },
+ {
+ "line": 2,
+ "hit": 1
+ },
+ {
+ "line": 3,
+ "hit": 1
+ },
+ {
+ "line": 5,
+ "hit": 1
+ },
+
+*/
+
diff --git a/lib/parser.js b/lib/parser.js
new file mode 100644
index 0000000..83b2c3d
--- /dev/null
+++ b/lib/parser.js
@@ -0,0 +1,131 @@
+/*
+Software License Agreement (BSD License)
+
+Copyright (c) 2012, Dav Glass <[email protected]>.
+All rights reserved.
+
+Redistribution and use of this software in source and binary forms, with or without modification, are
+permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above
+ copyright notice, this list of conditions and the
+ following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the
+ following disclaimer in the documentation and/or other
+ materials provided with the distribution.
+
+* The name of Dav Glass may not be used to endorse or promote products
+ derived from this software without specific prior
+ written permission of Dav Glass.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+var lcovParse = function(str) {
+ var data = [], item = {};
+
+ str = str.split('\n');
+ str.forEach(function(line) {
+ line = line.trim();
+
+ var parts = line.split(':'), lines, fn;
+
+ switch (parts[0].toUpperCase()) {
+ case 'TN':
+ item.title = parts[1].trim();
+ break;
+ case 'SF':
+ item.file = parts[1].trim();
+ break;
+ case 'FNF':
+ item.functions.found = Number(parts[1].trim());
+ break;
+ case 'FNH':
+ item.functions.hit = Number(parts[1].trim());
+ break;
+ case 'LF':
+ item.lines.found = Number(parts[1].trim());
+ break;
+ case 'LH':
+ item.lines.hit = Number(parts[1].trim());
+ break;
+ case 'DA':
+ if (!item.lines) {
+ item.lines = {
+ found: 0,
+ hit: 0,
+ details: []
+ };
+ }
+ lines = parts[1].split(',');
+ item.lines.details.push({
+ line: Number(lines[0]),
+ hit: Number(lines[1])
+ });
+ break;
+ case 'FN':
+ if (!item.functions) {
+ item.functions = {
+ hit: 0,
+ found: 0,
+ details: []
+ };
+ }
+ fn = parts[1].split(',');
+ item.functions.details.push({
+ name: fn[1],
+ line: Number(fn[0])
+ });
+ break;
+ case 'FNDA':
+ fn = parts[1].split(',');
+ item.functions.details.some(function(i, k) {
+ if (i.name === fn[1] && i.hit === undefined) {
+ item.functions.details[k].hit = Number(fn[0]);
+ return true;
+ }
+ });
+ break;
+ case 'BRDA':
+ if (!item.branches) {
+ item.branches = {
+ hit: 0,
+ found: 0,
+ details: []
+ };
+ }
+ fn = parts[1].split(',');
+ item.branches.details.push({
+ line: Number(fn[0]),
+ block: Number(fn[1]),
+ branch: Number(fn[2]),
+ taken: ((fn[3] === '-') ? 0 : Number(fn[3]))
+ });
+ break;
+ case 'BRF':
+ item.branches.found = Number(parts[1]);
+ break;
+ case 'BRH':
+ item.branches.hit = Number(parts[1]);
+ break;
+ }
+
+ if (line.indexOf('end_of_record') > -1) {
+ data.push(item);
+ item = {};
+ }
+ });
+ return data;
+};
+
+module.exports = lcovParse;
diff --git a/lib/sendToCoveralls.js b/lib/sendToCoveralls.js
new file mode 100644
index 0000000..b3514c6
--- /dev/null
+++ b/lib/sendToCoveralls.js
@@ -0,0 +1,11 @@
+var request = require('request');
+
+var sendToCoveralls = function(obj, cb){
+ var str = JSON.stringify(obj);
+ var url = 'https://coveralls.io/api/v1/jobs';
+ request({url : url, method : 'POST', form : { json : str}}, function(err, response, body){
+ cb(err, response, body);
+ });
+};
+
+module.exports = sendToCoveralls;