UNPKG

3.58 kBJavaScriptView Raw
1var TRAVIS_JOB_ID = process.env.TRAVIS_JOB_ID || 'unknown';
2var fs = require('fs');
3var lcovParse = require('lcov-parse');
4var path = require('path');
5var logger = require('./logger')();
6
7var detailsToCoverage = function(length, details){
8 var coverage = new Array(length);
9 details.forEach(function(obj){
10 coverage[obj.line - 1] = obj.hit;
11 });
12 return coverage;
13};
14
15var detailsToBranches = function(details){
16 var branches = [];
17 details.forEach(function(obj){
18 ['line','block','branch','taken'].forEach(function(key){
19 branches.push(obj[key] || 0);
20 });
21 });
22 return branches;
23};
24
25var convertLcovFileObject = function(file, filepath){
26 var rootpath = filepath;
27 filepath = path.resolve(rootpath, file.file);
28 var source = fs.readFileSync(filepath, 'utf8');
29 var lines = source.split("\n");
30 var coverage = detailsToCoverage(lines.length, file.lines.details);
31 var branches = detailsToBranches(file.branches.details);
32 return { name : path.relative(rootpath, path.resolve(rootpath, file.file)).split( path.sep ).join( "/" ),
33 source : source,
34 coverage : coverage,
35 branches : branches };
36};
37
38var cleanFilePath = function(file) {
39 if (file.indexOf('!') > -1) {
40 var regex = /^(.*!)(.*)$/g;
41 var matches = regex.exec(file);
42 return matches[matches.length-1];
43 }
44
45 return file;
46};
47
48var convertLcovToCoveralls = function(input, options, cb){
49 var filepath = options.filepath || '';
50 logger.debug("in: ", filepath);
51 filepath = path.resolve(process.cwd(), filepath);
52 lcovParse(input, function(err, parsed){
53 if (err){
54 logger.error("error from lcovParse: ", err);
55 logger.error("input: ", input);
56 return cb(err);
57 }
58 var postJson = {
59 source_files : []
60 };
61 if (options.git){
62 postJson.git = options.git;
63 }
64 if (options.run_at){
65 postJson.run_at = options.run_at;
66 }
67 if (options.service_name){
68 postJson.service_name = options.service_name;
69 }
70 if (options.service_job_id){
71 postJson.service_job_id = options.service_job_id;
72 }
73 if (options.service_pull_request) {
74 postJson.service_pull_request = options.service_pull_request;
75 }
76 if (options.repo_token) {
77 postJson.repo_token = options.repo_token;
78 }
79 if (options.parallel) {
80 postJson.parallel = options.parallel;
81 }
82 if (options.service_pull_request) {
83 postJson.service_pull_request = options.service_pull_request;
84 }
85 parsed.forEach(function(file){
86 file.file = cleanFilePath(file.file);
87 var currentFilePath = path.resolve(filepath, file.file);
88 if (fs.existsSync(currentFilePath)) {
89 postJson.source_files.push(convertLcovFileObject(file, filepath));
90 }
91 });
92 return cb(null, postJson);
93 });
94};
95
96module.exports = convertLcovToCoveralls;
97
98/* example coveralls json file
99
100
101{
102 "service_job_id": "1234567890",
103 "service_name": "travis-ci",
104 "source_files": [
105 {
106 "name": "example.rb",
107 "source": "def four\n 4\nend",
108 "coverage": [null, 1, null]
109 },
110 {
111 "name": "two.rb",
112 "source": "def seven\n eight\n nine\nend",
113 "coverage": [null, 1, 0, null]
114 }
115 ]
116}
117
118
119example output from lcov parser:
120
121 [
122 {
123 "file": "index.js",
124 "lines": {
125 "found": 0,
126 "hit": 0,
127 "details": [
128 {
129 "line": 1,
130 "hit": 1
131 },
132 {
133 "line": 2,
134 "hit": 1
135 },
136 {
137 "line": 3,
138 "hit": 1
139 },
140 {
141 "line": 5,
142 "hit": 1
143 },
144
145*/