Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80679 views
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
}
35
36
TeamcityReport.TYPE = 'teamcity';
37
util.inherits(TeamcityReport, Report);
38
39
function lineForKey(value, teamcityVar) {
40
return '##teamcity[buildStatisticValue key=\'' + teamcityVar + '\' value=\'' + value + '\']';
41
}
42
43
Report.mix(TeamcityReport, {
44
synopsis: function () {
45
return 'report with system messages that can be interpreted with TeamCity';
46
},
47
getDefaultConfig: function () {
48
return { file: null };
49
},
50
writeReport: function (collector /*, sync */) {
51
var summaries = [],
52
finalSummary,
53
lines = [],
54
text;
55
56
collector.files().forEach(function (file) {
57
summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
58
});
59
60
finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
61
62
lines.push('');
63
lines.push('##teamcity[blockOpened name=\'Code Coverage Summary\']');
64
65
//Statements Covered
66
lines.push(lineForKey(finalSummary.statements.pct, 'CodeCoverageB'));
67
68
//Methods Covered
69
lines.push(lineForKey(finalSummary.functions.covered, 'CodeCoverageAbsMCovered'));
70
lines.push(lineForKey(finalSummary.functions.total, 'CodeCoverageAbsMTotal'));
71
lines.push(lineForKey(finalSummary.functions.pct, 'CodeCoverageM'));
72
73
//Lines Covered
74
lines.push(lineForKey(finalSummary.lines.covered, 'CodeCoverageAbsLCovered'));
75
lines.push(lineForKey(finalSummary.lines.total, 'CodeCoverageAbsLTotal'));
76
lines.push(lineForKey(finalSummary.lines.pct, 'CodeCoverageL'));
77
78
lines.push('##teamcity[blockClosed name=\'Code Coverage Summary\']');
79
80
text = lines.join('\n');
81
if (this.file) {
82
mkdirp.sync(this.dir);
83
fs.writeFileSync(path.join(this.dir, this.file), text, 'utf8');
84
} else {
85
console.log(text);
86
}
87
this.emit('done');
88
}
89
});
90
91
module.exports = TeamcityReport;
92
93