]> code.delx.au - gnu-emacs-elpa/blob - test/parse-coverage.js
Add local coverage configuration.
[gnu-emacs-elpa] / test / parse-coverage.js
1 #!/usr/bin/env node
2
3 // Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 'use strict';
19
20 var padRight = function (value, padding) {
21 return value + new Array(Math.max(0, padding - String(value).length) + 1).join(' ');
22 };
23
24 var formatSourceFile = function (sourceFile) {
25 var sourceLines = sourceFile.source.split('\n');
26 var results = [
27 padRight('Hits', 5) + ' | Source',
28 new Array(80 + 1).join('-')
29 ];
30 var linesHit = 0;
31 var linesHittable = 0;
32 results = results.concat(sourceFile.coverage.map(function (hits, index) {
33 var hitsValue = hits === null ? 'N/A' : hits;
34 var column = hits === 0 ? '~' : '|';
35 if (hits > 0) {
36 linesHit += 1;
37 }
38 if (hits !== null) {
39 linesHittable += 1;
40 }
41 return padRight(hitsValue, 5) + ' ' + column + ' ' + sourceLines[index];
42 }));
43 results = results.concat([
44 '',
45 'Lines: ' + linesHit + ' / ' + linesHittable,
46 'Coverage: ' + (Math.round(linesHit / linesHittable * 10000) / 100) + '%'
47 ]);
48 return results.join('\n');
49 };
50
51 var format = function (json) {
52 return json.source_files.map(formatSourceFile).join('\n');
53 };
54
55 var read = function () {
56 var whole = '';
57
58 process.stdin.setEncoding('utf8');
59
60 process.stdin.on('readable', function () {
61 var chunk = process.stdin.read();
62 if (chunk !== null) {
63 whole += chunk;
64 }
65 });
66
67 process.stdin.on('end', function () {
68 console.log(format(JSON.parse(whole)));
69 });
70 };
71
72 read();