Code coverage report for istanbul/lib/report/text-summary.js

Statements: 91.89% (34 / 37)      Branches: 75% (9 / 12)      Functions: 100% (6 / 6)      Lines: 91.67% (33 / 36)      Ignored: none     

All files » istanbul/lib/report/ » text-summary.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94          89                                               89 147 147 147 147 147     89 89   89 48       48 48 48 48 48     48     89   2     95     12         12 47   12 12 12 12           12 12 12       12   12       89  
/*
 Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */
 
var path = require('path'),
    util = require('util'),
    mkdirp = require('mkdirp'),
    defaults = require('./common/defaults'),
    fs = require('fs'),
    utils = require('../object-utils'),
    Report = require('./index');
 
/**
 * a `Report` implementation that produces text output for overall coverage in summary format.
 *
 * Usage
 * -----
 *
 *      var report = require('istanbul').Report.create('text-summary');
 *
 * @class TextSummaryReport
 * @extends Report
 * @module report
 * @constructor
 * @param {Object} opts optional
 * @param {String} [opts.dir] the directory in which to the text coverage report will be written, when writing to a file
 * @param {String} [opts.file] the filename for the report. When omitted, the report is written to console
 */
function TextSummaryReport(opts) {
    Report.call(this);
    opts = opts || {};
    this.dir = opts.dir || process.cwd();
    this.file = opts.file;
    this.watermarks = opts.watermarks || defaults.watermarks();
}
 
TextSummaryReport.TYPE = 'text-summary';
util.inherits(TextSummaryReport, Report);
 
function lineForKey(summary, key, watermarks) {
    var metrics = summary[key],
        skipped,
        result,
        clazz = defaults.classFor(key, summary, watermarks);
    key = key.substring(0, 1).toUpperCase() + key.substring(1);
    Eif (key.length < 12) { key += '                   '.substring(0, 12 - key.length); }
    result = [ key , ':', metrics.pct + '%', '(', metrics.covered + '/' + metrics.total, ')'].join(' ');
    skipped = metrics.skipped;
    Iif (skipped > 0) {
        result += ', ' + skipped + ' ignored';
    }
    return defaults.colorize(result, clazz);
}
 
Report.mix(TextSummaryReport, {
    synopsis: function () {
        return 'text report that prints a coverage summary across all files, typically to console';
    },
    getDefaultConfig: function () {
        return { file: null };
    },
    writeReport: function (collector /*, sync */) {
        var summaries = [],
            finalSummary,
            lines = [],
            watermarks = this.watermarks,
            text;
        collector.files().forEach(function (file) {
            summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
        });
        finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
        lines.push('');
        lines.push('=============================== Coverage summary ===============================');
        lines.push.apply(lines, [
            lineForKey(finalSummary, 'statements', watermarks),
            lineForKey(finalSummary, 'branches', watermarks),
            lineForKey(finalSummary, 'functions', watermarks),
            lineForKey(finalSummary, 'lines', watermarks)
        ]);
        lines.push('================================================================================');
        text = lines.join('\n');
        Iif (this.file) {
            mkdirp.sync(this.dir);
            fs.writeFileSync(path.join(this.dir, this.file), text, 'utf8');
        } else {
            console.log(text);
        }
        this.emit('done');
    }
});
 
module.exports = TextSummaryReport;