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

Statements: 100% (27 / 27)      Branches: 100% (12 / 12)      Functions: 100% (7 / 7)      Lines: 100% (26 / 26)      Ignored: none     

All files » istanbul/lib/report/ » json-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          89                                         89 101 101 101 101   89 89   89   2     194         4       4 4 4 4 4 16 4   12   16 16 16   4   4       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'),
    objectUtils = require('../object-utils'),
    Writer = require('../util/file-writer'),
    util = require('util'),
    Report = require('./index');
/**
 * a `Report` implementation that produces a coverage JSON object with summary info only.
 *
 * Usage
 * -----
 *
 *      var report = require('istanbul').Report.create('json-summary');
 *
 *
 * @class JsonSummaryReport
 * @extends Report
 * @module report
 * @constructor
 * @param {Object} opts optional
 * @param {String} [opts.dir] the directory in which to write the `coverage-summary.json` file. Defaults to `process.cwd()`
 */
function JsonSummaryReport(opts) {
    this.opts = opts || {};
    this.opts.dir = this.opts.dir || process.cwd();
    this.opts.file = this.opts.file || this.getDefaultConfig().file;
    this.opts.writer = this.opts.writer || null;
}
JsonSummaryReport.TYPE = 'json-summary';
util.inherits(JsonSummaryReport, Report);
 
Report.mix(JsonSummaryReport, {
    synopsis: function () {
        return 'prints a summary coverage object as JSON to a file';
    },
    getDefaultConfig: function () {
        return {
            file: 'coverage-summary.json'
        };
    },
    writeReport: function (collector, sync) {
        var outputFile = path.resolve(this.opts.dir, this.opts.file),
            writer = this.opts.writer || new Writer(sync),
            that = this;
 
        writer.on('done', function () { that.emit('done'); });
        writer.writeFile(outputFile, function (contentWriter) {
            var first = true;
            contentWriter.println("{");
            collector.files().forEach(function (key) {
                if (first) {
                    first = false;
                } else {
                    contentWriter.println(",");
                }
                contentWriter.write(JSON.stringify(key));
                contentWriter.write(":");
                contentWriter.write(JSON.stringify(objectUtils.summarizeFileCoverage(collector.fileCoverageFor(key))));
            });
            contentWriter.println("}");
        });
        writer.done();
    }
});
 
module.exports = JsonSummaryReport;