Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / istanbul / lib / report / json-summary.js
1 /*
2  Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
3  Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4  */
5
6 var path = require('path'),
7     objectUtils = require('../object-utils'),
8     Writer = require('../util/file-writer'),
9     util = require('util'),
10     Report = require('./index');
11 /**
12  * a `Report` implementation that produces a coverage JSON object with summary info only.
13  *
14  * Usage
15  * -----
16  *
17  *      var report = require('istanbul').Report.create('json-summary');
18  *
19  *
20  * @class JsonSummaryReport
21  * @extends Report
22  * @module report
23  * @constructor
24  * @param {Object} opts optional
25  * @param {String} [opts.dir] the directory in which to write the `coverage-summary.json` file. Defaults to `process.cwd()`
26  */
27 function JsonSummaryReport(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;
32 }
33 JsonSummaryReport.TYPE = 'json-summary';
34 util.inherits(JsonSummaryReport, Report);
35
36 Report.mix(JsonSummaryReport, {
37     synopsis: function () {
38         return 'prints a summary coverage object as JSON to a file';
39     },
40     getDefaultConfig: function () {
41         return {
42             file: 'coverage-summary.json'
43         };
44     },
45     writeReport: function (collector, sync) {
46         var outputFile = path.resolve(this.opts.dir, this.opts.file),
47             writer = this.opts.writer || new Writer(sync),
48             that = this;
49
50         var summaries = [],
51             finalSummary;
52         collector.files().forEach(function (file) {
53             summaries.push(objectUtils.summarizeFileCoverage(collector.fileCoverageFor(file)));
54         });
55         finalSummary = objectUtils.mergeSummaryObjects.apply(null, summaries);
56
57         writer.on('done', function () { that.emit('done'); });
58         writer.writeFile(outputFile, function (contentWriter) {
59             contentWriter.println("{");
60             contentWriter.write('"total":');
61             contentWriter.write(JSON.stringify(finalSummary));
62
63             collector.files().forEach(function (key) {
64                 contentWriter.println(",");
65                 contentWriter.write(JSON.stringify(key));
66                 contentWriter.write(":");
67                 contentWriter.write(JSON.stringify(objectUtils.summarizeFileCoverage(collector.fileCoverageFor(key))));
68             });
69             contentWriter.println("}");
70         });
71         writer.done();
72     }
73 });
74
75 module.exports = JsonSummaryReport;