Code coverage report for istanbul/lib/command/report.js

Statements: 40.63% (13 / 32)      Branches: 0% (0 / 10)      Functions: 50% (3 / 6)      Lines: 41.94% (13 / 31)     

All files » istanbul/lib/command/ » report.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 1 var nopt = require('nopt'),
7 Report = require('../report'),
8 path = require('path'),
9 fs = require('fs'),
10 Collector = require('../collector'),
11 inputError = require('../util/input-error'),
12 formatOption = require('../util/help-formatter').formatOption,
13 filesFor = require('../util/file-matcher').filesFor,
14 util = require('util'),
15 Command = require('./index');
16
17 1 function ReportCommand() {
18 6 Command.call(this);
19 }
20
21 1 ReportCommand.TYPE = 'report';
22 1 util.inherits(ReportCommand, Command);
23
24 1 Command.mix(ReportCommand, {
25 synopsis: function () {
26 5 return "writes reports for coverage JSON objects produced in a previous run";
27 },
28
29 usage: function () {
30 2 console.error('\nUsage: ' + this.toolName() + ' ' + this.type() + ' <options> [ <format> [<include-pattern>] ]\n\nOptions are:\n\n'
31 + [
32 formatOption('--root <input-directory>', 'The input root directory for finding coverage files'),
33 formatOption('--dir <report-directory>', 'The output directory where files will be written. This defaults to ./coverage/'),
34 formatOption('--verbose, -v', 'verbose mode')
35 ].join('\n\n') + '\n');
36
37 2 console.error('\n');
38
39 2 console.error('<format> is one of html, lcovonly or lcov (html + lcovonly). Default is lcov');
40 2 console.error('<include-pattern> is a fileset pattern that can be used to select one or move coverage files for merged reporting. This defaults to "**/coverage*.json"');
41
42 2 console.error('\n');
43 },
44
45 run: function (args) {
46
47 var config = {
48 root: path,
49 dir: path,
50 verbose: Boolean
51 },
52 opts = nopt(config, { v : '--verbose' }, args, 0),
53 fmtAndArgs = opts.argv.remain,
54 fmt = 'lcov',
55 includePattern = '**/coverage*.json',
56 reporter,
57 root,
58 collector = new Collector();
59
60 if (fmtAndArgs.length > 0) {
61 fmt = fmtAndArgs[0];
62 }
63
64 if (fmtAndArgs.length > 1) {
65 includePattern = fmtAndArgs[1];
66 }
67
68 opts.dir = opts.dir || path.resolve(process.cwd(), 'coverage');
69
70 try {
71 reporter = Report.create(fmt, opts);
72 } catch (ex) {
73 throw inputError.create('Invalid report format [' + fmt + ']');
74 }
75
76 root = opts.root || process.cwd();
77 filesFor({
78 root: root,
79 includes: [ includePattern ]
80 }, function (err, files) {
81 if (err) { throw err; }
82 files.forEach(function (file) {
83 var coverageObject = JSON.parse(fs.readFileSync(file, 'utf8'));
84 collector.add(coverageObject);
85 });
86 console.log('Using reporter [' + fmt + ']');
87 reporter.writeReport(collector);
88 console.log('Done');
89 });
90 }
91 });
92
93 1 module.exports = ReportCommand;
94
95
96