1 | var TRAVIS_JOB_ID = process.env.TRAVIS_JOB_ID || 'unknown';
|
2 | var fs = require('fs');
|
3 | var lcovParse = require('lcov-parse');
|
4 | var path = require('path');
|
5 | var logger = require('./logger')();
|
6 |
|
7 | var 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 |
|
15 | var 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 |
|
25 | var 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 |
|
38 | var 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 |
|
48 | var 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 |
|
96 | module.exports = convertLcovToCoveralls;
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|