2 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
3 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6 var path = require('path'),
7 util = require('util'),
8 mkdirp = require('mkdirp'),
10 utils = require('../object-utils'),
11 Report = require('./index');
14 * a `Report` implementation that produces system messages interpretable by TeamCity.
19 * var report = require('istanbul').Report.create('teamcity');
21 * @class TeamcityReport
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
29 function TeamcityReport(opts) {
32 this.dir = opts.dir || process.cwd();
33 this.file = opts.file;
34 this.blockName = opts.blockName || this.getDefaultConfig().blockName;
37 TeamcityReport.TYPE = 'teamcity';
38 util.inherits(TeamcityReport, Report);
40 function lineForKey(value, teamcityVar) {
41 return '##teamcity[buildStatisticValue key=\'' + teamcityVar + '\' value=\'' + value + '\']';
44 Report.mix(TeamcityReport, {
45 synopsis: function () {
46 return 'report with system messages that can be interpreted with TeamCity';
48 getDefaultConfig: function () {
49 return { file: null , blockName: 'Code Coverage Summary'};
51 writeReport: function (collector /*, sync */) {
57 collector.files().forEach(function (file) {
58 summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
61 finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
64 lines.push('##teamcity[blockOpened name=\''+ this.blockName +'\']');
67 lines.push(lineForKey(finalSummary.statements.pct, 'CodeCoverageB'));
70 lines.push(lineForKey(finalSummary.functions.covered, 'CodeCoverageAbsMCovered'));
71 lines.push(lineForKey(finalSummary.functions.total, 'CodeCoverageAbsMTotal'));
72 lines.push(lineForKey(finalSummary.functions.pct, 'CodeCoverageM'));
75 lines.push(lineForKey(finalSummary.lines.covered, 'CodeCoverageAbsLCovered'));
76 lines.push(lineForKey(finalSummary.lines.total, 'CodeCoverageAbsLTotal'));
77 lines.push(lineForKey(finalSummary.lines.pct, 'CodeCoverageL'));
79 lines.push('##teamcity[blockClosed name=\''+ this.blockName +'\']');
81 text = lines.join('\n');
83 mkdirp.sync(this.dir);
84 fs.writeFileSync(path.join(this.dir, this.file), text, 'utf8');
92 module.exports = TeamcityReport;