Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / istanbul / lib / report / clover.js
1 var path = require('path'),
2     util = require('util'),
3     Report = require('./index'),
4     FileWriter = require('../util/file-writer'),
5     TreeSummarizer = require('../util/tree-summarizer'),
6     utils = require('../object-utils');
7
8 /**
9  * a `Report` implementation that produces a clover-style XML file.
10  *
11  * Usage
12  * -----
13  *
14  *      var report = require('istanbul').Report.create('clover');
15  *
16  * @class CloverReport
17  * @module report
18  * @extends Report
19  * @constructor
20  * @param {Object} opts optional
21  * @param {String} [opts.dir] the directory in which to the clover.xml will be written
22  * @param {String} [opts.file] the file name, defaulted to config attribute or 'clover.xml'
23  */
24 function CloverReport(opts) {
25     Report.call(this);
26     opts = opts || {};
27     this.projectRoot = process.cwd();
28     this.dir = opts.dir || this.projectRoot;
29     this.file = opts.file || this.getDefaultConfig().file;
30     this.opts = opts;
31 }
32
33 CloverReport.TYPE = 'clover';
34 util.inherits(CloverReport, Report);
35
36 function asJavaPackage(node) {
37     return node.displayShortName().
38         replace(/\//g, '.').
39         replace(/\\/g, '.').
40         replace(/\.$/, '');
41 }
42
43 function asClassName(node) {
44     /*jslint regexp: true */
45     return node.fullPath().replace(/.*[\\\/]/, '');
46 }
47
48 function quote(thing) {
49     return '"' + thing + '"';
50 }
51
52 function attr(n, v) {
53     return ' ' + n + '=' + quote(v) + ' ';
54 }
55
56 function branchCoverageByLine(fileCoverage) {
57     var branchMap = fileCoverage.branchMap,
58         branches = fileCoverage.b,
59         ret = {};
60     Object.keys(branchMap).forEach(function (k) {
61         var line = branchMap[k].line,
62             branchData = branches[k];
63         ret[line] = ret[line] || [];
64         ret[line].push.apply(ret[line], branchData);
65     });
66     Object.keys(ret).forEach(function (k) {
67         var dataArray = ret[k],
68             covered = dataArray.filter(function (item) { return item > 0; }),
69             coverage = covered.length / dataArray.length * 100;
70         ret[k] = { covered: covered.length, total: dataArray.length, coverage: coverage };
71     });
72     return ret;
73 }
74
75 function addClassStats(node, fileCoverage, writer) {
76     fileCoverage = utils.incrementIgnoredTotals(fileCoverage);
77
78     var metrics = node.metrics,
79         branchByLine = branchCoverageByLine(fileCoverage),
80         fnMap,
81         lines;
82
83     writer.println('\t\t\t<file' +
84         attr('name', asClassName(node)) +
85         attr('path', node.fullPath()) +
86         '>');
87
88     writer.println('\t\t\t\t<metrics' +
89         attr('statements', metrics.lines.total) +
90         attr('coveredstatements', metrics.lines.covered) +
91         attr('conditionals', metrics.branches.total) +
92         attr('coveredconditionals', metrics.branches.covered) +
93         attr('methods', metrics.functions.total) +
94         attr('coveredmethods', metrics.functions.covered) +
95         '/>');
96
97     fnMap = fileCoverage.fnMap;
98     lines = fileCoverage.l;
99     Object.keys(lines).forEach(function (k) {
100         var str = '\t\t\t\t<line' +
101             attr('num', k) +
102             attr('count', lines[k]),
103             branchDetail = branchByLine[k];
104
105         if (!branchDetail) {
106             str += ' type="stmt" ';
107         } else {
108                 str += ' type="cond" ' +
109                 attr('truecount', branchDetail.covered) +
110                 attr('falsecount', (branchDetail.total - branchDetail.covered));
111         }
112         writer.println(str + '/>');
113     });
114
115     writer.println('\t\t\t</file>');
116 }
117
118 function walk(node, collector, writer, level, projectRoot) {
119     var metrics,
120         totalFiles = 0,
121         totalPackages = 0,
122         totalLines = 0,
123         tempLines = 0;
124     if (level === 0) {
125         metrics = node.metrics;
126         writer.println('<?xml version="1.0" encoding="UTF-8"?>');
127         writer.println('<coverage' +
128             attr('generated', Date.now()) +
129             'clover="3.2.0">');
130
131         writer.println('\t<project' +
132             attr('timestamp', Date.now()) +
133             attr('name', 'All Files') +
134             '>');
135
136         node.children.filter(function (child) { return child.kind === 'dir'; }).
137             forEach(function (child) {
138                 totalPackages += 1;
139                 child.children.filter(function (child) { return child.kind !== 'dir'; }).
140                     forEach(function (child) {
141                         Object.keys(collector.fileCoverageFor(child.fullPath()).l).forEach(function (k){
142                             tempLines = k;
143                         });
144                         totalLines += Number(tempLines);
145                         totalFiles += 1;
146                 });
147         });
148
149         writer.println('\t\t<metrics' +
150             attr('statements', metrics.lines.total) +
151             attr('coveredstatements', metrics.lines.covered) +
152             attr('conditionals', metrics.branches.total) +
153             attr('coveredconditionals', metrics.branches.covered) +
154             attr('methods', metrics.functions.total) +
155             attr('coveredmethods', metrics.functions.covered) +
156             attr('elements', metrics.lines.total + metrics.branches.total + metrics.functions.total) +
157             attr('coveredelements', metrics.lines.covered + metrics.branches.covered + metrics.functions.covered) +
158             attr('complexity', 0) +
159             attr('packages', totalPackages) +
160             attr('files', totalFiles) +
161             attr('classes', totalFiles) +
162             attr('loc', totalLines) +
163             attr('ncloc', totalLines) +
164             '/>');
165     }
166     if (node.packageMetrics) {
167         metrics = node.packageMetrics;
168         writer.println('\t\t<package' +
169             attr('name', asJavaPackage(node)) +
170             '>');
171
172         writer.println('\t\t\t<metrics' +
173             attr('statements', metrics.lines.total) +
174             attr('coveredstatements', metrics.lines.covered) +
175             attr('conditionals', metrics.branches.total) +
176             attr('coveredconditionals', metrics.branches.covered) +
177             attr('methods', metrics.functions.total) +
178             attr('coveredmethods', metrics.functions.covered) +
179             '/>');
180
181         node.children.filter(function (child) { return child.kind !== 'dir'; }).
182             forEach(function (child) {
183                 addClassStats(child, collector.fileCoverageFor(child.fullPath()), writer);
184             });
185         writer.println('\t\t</package>');
186     }
187     node.children.filter(function (child) { return child.kind === 'dir'; }).
188         forEach(function (child) {
189             walk(child, collector, writer, level + 1, projectRoot);
190         });
191
192     if (level === 0) {
193         writer.println('\t</project>');
194         writer.println('</coverage>');
195     }
196 }
197
198 Report.mix(CloverReport, {
199     synopsis: function () {
200         return 'XML coverage report that can be consumed by the clover tool';
201     },
202     getDefaultConfig: function () {
203         return { file: 'clover.xml' };
204     },
205     writeReport: function (collector, sync) {
206         var summarizer = new TreeSummarizer(),
207             outputFile = path.join(this.dir, this.file),
208             writer = this.opts.writer || new FileWriter(sync),
209             projectRoot = this.projectRoot,
210             that = this,
211             tree,
212             root;
213
214         collector.files().forEach(function (key) {
215             summarizer.addFileCoverageSummary(key, utils.summarizeFileCoverage(collector.fileCoverageFor(key)));
216         });
217         tree = summarizer.getTreeSummary();
218         root = tree.root;
219         writer.on('done', function () { that.emit('done'); });
220         writer.writeFile(outputFile, function (contentWriter) {
221             walk(root, collector, contentWriter, 0, projectRoot);
222             writer.done();
223         });
224     }
225 });
226
227 module.exports = CloverReport;