2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6 var path = require('path'),
7 Writer = require('../util/file-writer'),
8 util = require('util'),
9 Report = require('./index'),
10 utils = require('../object-utils');
12 * a `Report` implementation that produces an LCOV coverage file from coverage objects.
17 * var report = require('istanbul').Report.create('lcovonly');
20 * @class LcovOnlyReport
24 * @param {Object} opts optional
25 * @param {String} [opts.dir] the directory in which to the `lcov.info` file. Defaults to `process.cwd()`
27 function LcovOnlyReport(opts) {
28 this.opts = opts || {};
29 this.opts.dir = this.opts.dir || process.cwd();
30 this.opts.file = this.opts.file || this.getDefaultConfig().file;
31 this.opts.writer = this.opts.writer || null;
33 LcovOnlyReport.TYPE = 'lcovonly';
34 util.inherits(LcovOnlyReport, Report);
36 Report.mix(LcovOnlyReport, {
37 synopsis: function () {
38 return 'lcov coverage report that can be consumed by the lcov tool';
40 getDefaultConfig: function () {
41 return { file: 'lcov.info' };
43 writeFileCoverage: function (writer, fc) {
45 functionMap = fc.fnMap,
48 branchMap = fc.branchMap,
49 summary = utils.summarizeFileCoverage(fc);
51 writer.println('TN:'); //no test name
52 writer.println('SF:' + fc.path);
54 Object.keys(functions).forEach(function (key) {
55 var meta = functionMap[key];
56 writer.println('FN:' + [ meta.line, meta.name ].join(','));
58 writer.println('FNF:' + summary.functions.total);
59 writer.println('FNH:' + summary.functions.covered);
61 Object.keys(functions).forEach(function (key) {
62 var stats = functions[key],
63 meta = functionMap[key];
64 writer.println('FNDA:' + [ stats, meta.name ].join(','));
67 Object.keys(lines).forEach(function (key) {
68 var stat = lines[key];
69 writer.println('DA:' + [ key, stat ].join(','));
71 writer.println('LF:' + summary.lines.total);
72 writer.println('LH:' + summary.lines.covered);
74 Object.keys(branches).forEach(function (key) {
75 var branchArray = branches[key],
76 meta = branchMap[key],
79 branchArray.forEach(function (b) {
80 writer.println('BRDA:' + [line, key, i, b].join(','));
84 writer.println('BRF:' + summary.branches.total);
85 writer.println('BRH:' + summary.branches.covered);
86 writer.println('end_of_record');
89 writeReport: function (collector, sync) {
90 var outputFile = path.resolve(this.opts.dir, this.opts.file),
91 writer = this.opts.writer || new Writer(sync),
93 writer.on('done', function () { that.emit('done'); });
94 writer.writeFile(outputFile, function (contentWriter) {
95 collector.files().forEach(function (key) {
96 that.writeFileCoverage(contentWriter, collector.fileCoverageFor(key));
103 module.exports = LcovOnlyReport;