Debian Codegraph @

lib/report/teamcity.js

raw symbols SCIP
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 util = require('util'),
8 mkdirp = require('mkdirp'),
9 fs = require('fs'),
10 utils = require('../object-utils'),
11 Report = require('./index');
12
13 /**
14 * a `Report` implementation that produces system messages interpretable by TeamCity.
15 *
16 * Usage
17 * -----
18 *
19 * var report = require('istanbul').Report.create('teamcity');
20 *
21 * @class TeamcityReport
22 * @extends Report
23 * @module report
24 * @constructor
25 * @param {Object} opts optional
26 * @param {String} [opts.dir] the directory in which to the text coverage report will be written, when writing to a file
27 * @param {String} [opts.file] the filename for the report. When omitted, the report is written to console
28 */
29 function TeamcityReport(opts) {
30 Report.call(this);
31 opts = opts || {};
32 this.dir = opts.dir || process.cwd();
33 this.file = opts.file;
34 this.blockName = opts.blockName || this.getDefaultConfig().blockName;
35 }
36
37 TeamcityReport.TYPE = 'teamcity';
38 util.inherits(TeamcityReport, Report);
39
40 function lineForKey(value, teamcityVar) {
41 return '##teamcity[buildStatisticValue key=\'' + teamcityVar + '\' value=\'' + value + '\']';
42 }
43
44 Report.mix(TeamcityReport, {
45 synopsis: function () {
46 return 'report with system messages that can be interpreted with TeamCity';
47 },
48 getDefaultConfig: function () {
49 return { file: null , blockName: 'Code Coverage Summary'};
50 },
51 writeReport: function (collector /*, sync */) {
52 var summaries = [],
53 finalSummary,
54 lines = [],
55 text;
56
57 collector.files().forEach(function (file) {
58 summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
59 });
60
61 finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
62
63 lines.push('');
64 lines.push('##teamcity[blockOpened name=\''+ this.blockName +'\']');
65
66 //Statements Covered
67 lines.push(lineForKey(finalSummary.statements.pct, 'CodeCoverageB'));
68
69 //Methods Covered
70 lines.push(lineForKey(finalSummary.functions.covered, 'CodeCoverageAbsMCovered'));
71 lines.push(lineForKey(finalSummary.functions.total, 'CodeCoverageAbsMTotal'));
72 lines.push(lineForKey(finalSummary.functions.pct, 'CodeCoverageM'));
73
74 //Lines Covered
75 lines.push(lineForKey(finalSummary.lines.covered, 'CodeCoverageAbsLCovered'));
76 lines.push(lineForKey(finalSummary.lines.total, 'CodeCoverageAbsLTotal'));
77 lines.push(lineForKey(finalSummary.lines.pct, 'CodeCoverageL'));
78
79 lines.push('##teamcity[blockClosed name=\''+ this.blockName +'\']');
80
81 text = lines.join('\n');
82 if (this.file) {
83 mkdirp.sync(this.dir);
84 fs.writeFileSync(path.join(this.dir, this.file), text, 'utf8');
85 } else {
86 console.log(text);
87 }
88 this.emit('done');
89 }
90 });
91
92 module.exports = TeamcityReport;