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

Statements: 93.2% (96 / 103)      Branches: 83.33% (30 / 36)      Functions: 100% (17 / 17)      Lines: 93.2% (96 / 103)      Ignored: none     

All files » istanbul/lib/report/ » text.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 95 96 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213          89                                                             89 100 100 100 100 100 100 100     89 89   89 276   276 276 1215   276     89 135 135   135             135 135 135 135             135 135     89 27     89 108     89 48       89 3 3 3 3 3 3 3     89 24             24 24 24 24 24   24     89 24 24 24 24 12   24 21   24     89 3       3 3 3 3 3 3     89 24 24 3 3 3 3   21   24 21   24 3 3 3       89   2     95     3                 3 12   3 3 3 3           3 3 3       3   3       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'),
    mkdirp = require('mkdirp'),
    util = require('util'),
    fs = require('fs'),
    defaults = require('./common/defaults'),
    Report = require('./index'),
    TreeSummarizer = require('../util/tree-summarizer'),
    utils = require('../object-utils'),
    PCT_COLS = 10,
    TAB_SIZE = 3,
    DELIM = ' |',
    COL_DELIM = '-|';
 
/**
 * a `Report` implementation that produces text output in a detailed table.
 *
 * Usage
 * -----
 *
 *      var report = require('istanbul').Report.create('text');
 *
 * @class TextReport
 * @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
 * @param {Number} [opts.maxCols] the max column width of the report. By default, the width of the report is adjusted based on the length of the paths
 *              to be reported.
 */
function TextReport(opts) {
    Report.call(this);
    opts = opts || {};
    this.dir = opts.dir || process.cwd();
    this.file = opts.file;
    this.summary = opts.summary;
    this.maxCols = opts.maxCols || 0;
    this.watermarks = opts.watermarks || defaults.watermarks();
}
 
TextReport.TYPE = 'text';
util.inherits(TextReport, Report);
 
function padding(num, ch) {
    var str = '',
        i;
    ch = ch || ' ';
    for (i = 0; i < num; i += 1) {
        str += ch;
    }
    return str;
}
 
function fill(str, width, right, tabs, clazz) {
    tabs = tabs || 0;
    str = String(str);
 
    var leadingSpaces = tabs * TAB_SIZE,
        remaining = width - leadingSpaces,
        leader = padding(leadingSpaces),
        fmtStr = '',
        fillStr,
        strlen = str.length;
 
    Eif (remaining > 0) {
        Eif (remaining >= strlen) {
            fillStr = padding(remaining - strlen);
            fmtStr = right ? fillStr + str : str + fillStr;
        } else {
            fmtStr = str.substring(strlen - remaining);
            fmtStr = '... ' + fmtStr.substring(4);
        }
    }
 
    fmtStr = defaults.colorize(fmtStr, clazz);
    return leader + fmtStr;
}
 
function formatName(name, maxCols, level, clazz) {
    return fill(name, maxCols, false, level, clazz);
}
 
function formatPct(pct, clazz) {
    return fill(pct, PCT_COLS, true, 0, clazz);
}
 
function nodeName(node) {
    return node.displayShortName() || 'All files';
}
 
 
function tableHeader(maxNameCols) {
    var elements = [];
    elements.push(formatName('File', maxNameCols, 0));
    elements.push(formatPct('% Stmts'));
    elements.push(formatPct('% Branches'));
    elements.push(formatPct('% Funcs'));
    elements.push(formatPct('% Lines'));
    return elements.join(' |') + ' |';
}
 
function tableRow(node, maxNameCols, level, watermarks) {
    var name = nodeName(node),
        statements = node.metrics.statements.pct,
        branches = node.metrics.branches.pct,
        functions = node.metrics.functions.pct,
        lines = node.metrics.lines.pct,
        elements = [];
 
    elements.push(formatName(name, maxNameCols, level, defaults.classFor('statements', node.metrics, watermarks)));
    elements.push(formatPct(statements, defaults.classFor('statements', node.metrics, watermarks)));
    elements.push(formatPct(branches, defaults.classFor('branches', node.metrics, watermarks)));
    elements.push(formatPct(functions, defaults.classFor('functions', node.metrics, watermarks)));
    elements.push(formatPct(lines, defaults.classFor('lines', node.metrics, watermarks)));
 
    return elements.join(DELIM) + DELIM;
}
 
function findNameWidth(node, level, last) {
    last = last || 0;
    level = level || 0;
    var idealWidth = TAB_SIZE * level + nodeName(node).length;
    if (idealWidth > last) {
        last = idealWidth;
    }
    node.children.forEach(function (child) {
        last = findNameWidth(child, level + 1, last);
    });
    return last;
}
 
function makeLine(nameWidth) {
    var name = padding(nameWidth, '-'),
        pct = padding(PCT_COLS, '-'),
        elements = [];
 
    elements.push(name);
    elements.push(pct);
    elements.push(pct);
    elements.push(pct);
    elements.push(pct);
    return elements.join(COL_DELIM) + COL_DELIM;
}
 
function walk(node, nameWidth, array, level, watermarks) {
    var line;
    if (level === 0) {
        line = makeLine(nameWidth);
        array.push(line);
        array.push(tableHeader(nameWidth));
        array.push(line);
    } else {
        array.push(tableRow(node, nameWidth, level, watermarks));
    }
    node.children.forEach(function (child) {
        walk(child, nameWidth, array, level + 1, watermarks);
    });
    if (level === 0) {
        array.push(line);
        array.push(tableRow(node, nameWidth, level, watermarks));
        array.push(line);
    }
}
 
Report.mix(TextReport, {
    synopsis: function () {
        return 'text report that prints a coverage line for every file, typically to console';
    },
    getDefaultConfig: function () {
        return { file: null, maxCols: 0 };
    },
    writeReport: function (collector /*, sync */) {
        var summarizer = new TreeSummarizer(),
            tree,
            root,
            nameWidth,
            statsWidth = 4 * ( PCT_COLS + 2),
            maxRemaining,
            strings = [],
            text;
 
        collector.files().forEach(function (key) {
            summarizer.addFileCoverageSummary(key, utils.summarizeFileCoverage(collector.fileCoverageFor(key)));
        });
        tree = summarizer.getTreeSummary();
        root = tree.root;
        nameWidth = findNameWidth(root);
        Iif (this.maxCols > 0) {
            maxRemaining = this.maxCols - statsWidth - 2;
            if (nameWidth > maxRemaining) {
                nameWidth = maxRemaining;
            }
        }
        walk(root, nameWidth, strings, 0, this.watermarks);
        text = strings.join('\n') + '\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 = TextReport;