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

Statements: 100% (34 / 34)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (34 / 34)      Ignored: none     

All files » istanbul/lib/report/ » teamcity.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          89                                             89 100 100 100 100     89 89   89 21     89   2     95     3         3 12     3   3 3     3     3 3 3     3 3 3   3   3 3 1 1   2   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'),
    util = require('util'),
    mkdirp = require('mkdirp'),
    fs = require('fs'),
    utils = require('../object-utils'),
    Report = require('./index');
 
/**
 * a `Report` implementation that produces system messages interpretable by TeamCity.
 *
 * Usage
 * -----
 *
 *      var report = require('istanbul').Report.create('teamcity');
 *
 * @class TeamcityReport
 * @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 TeamcityReport(opts) {
    Report.call(this);
    opts = opts || {};
    this.dir = opts.dir || process.cwd();
    this.file = opts.file;
}
 
TeamcityReport.TYPE = 'teamcity';
util.inherits(TeamcityReport, Report);
 
function lineForKey(value, teamcityVar) {
    return '##teamcity[buildStatisticValue key=\'' + teamcityVar + '\' value=\'' + value + '\']';
}
 
Report.mix(TeamcityReport, {
    synopsis: function () {
        return 'report with system messages that can be interpreted with TeamCity';
    },
    getDefaultConfig: function () {
        return { file: null };
    },
    writeReport: function (collector /*, sync */) {
        var summaries = [],
            finalSummary,
            lines = [],
            text;
 
        collector.files().forEach(function (file) {
            summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
        });
 
        finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
 
        lines.push('');
        lines.push('##teamcity[blockOpened name=\'Code Coverage Summary\']');
 
        //Statements Covered
        lines.push(lineForKey(finalSummary.statements.pct, 'CodeCoverageB'));
 
        //Methods Covered
        lines.push(lineForKey(finalSummary.functions.covered, 'CodeCoverageAbsMCovered'));
        lines.push(lineForKey(finalSummary.functions.total, 'CodeCoverageAbsMTotal'));
        lines.push(lineForKey(finalSummary.functions.pct, 'CodeCoverageM'));
 
        //Lines Covered
        lines.push(lineForKey(finalSummary.lines.covered, 'CodeCoverageAbsLCovered'));
        lines.push(lineForKey(finalSummary.lines.total, 'CodeCoverageAbsLTotal'));
        lines.push(lineForKey(finalSummary.lines.pct, 'CodeCoverageL'));
 
        lines.push('##teamcity[blockClosed name=\'Code Coverage Summary\']');
 
        text = lines.join('\n');
        if (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 = TeamcityReport;