|
1 | | -var fs = require('fs'), |
| 1 | +var path = require('path'), |
2 | 2 | util = require('util'), |
3 | | - _ = require('underscore'), |
4 | | - bunker; |
| 3 | + _ = require('underscore'); |
5 | 4 |
|
6 | | -try { |
7 | | - bunker = require('bunker'); |
8 | | -} catch (err) {} |
| 5 | +var istanbul, collector; |
9 | 6 |
|
10 | | -exports.instrument = function(path) { |
11 | | - var src = fs.readFileSync(path, 'utf-8'), |
12 | | - newSrc = bunker(src).compile(); |
| 7 | +try { istanbul = require('istanbul'); } |
| 8 | +catch (e) {} |
13 | 9 |
|
14 | | - fs.renameSync(src, '__' + src); |
15 | | - fs.writeFileSync(path, newSrc, 'utf-8'); |
| 10 | +exports.setup = function() { |
| 11 | + collector = new istanbul.Collector(); |
16 | 12 | }; |
17 | 13 |
|
| 14 | +exports.append = function(coverage) { |
| 15 | + collector && coverage && collector.add(coverage); |
| 16 | +}; |
18 | 17 |
|
19 | | -exports.restore = function(path) { |
20 | | - // do it only if the original file exist |
21 | | - if (fs.statSync('__' + path).isFile()) { |
22 | | - fs.unlinkSync(path); |
23 | | - fs.renameSync('__' + path, path); |
| 18 | +exports.report = function() { |
| 19 | + if (collector) { |
| 20 | + var opts = { dir: path.resolve('coverage') }, |
| 21 | + Report = istanbul.Report, |
| 22 | + reports = [ Report.create('lcov', opts), Report.create('json', opts), Report.create('text-summary', opts) ]; |
| 23 | + reports.forEach(function (rep) { |
| 24 | + rep.writeReport(collector, true); |
| 25 | + }); |
24 | 26 | } |
| 27 | +}; |
| 28 | + |
| 29 | +function resolvePaths(files) { |
| 30 | + if (Array.isArray(files)) return files.map(function (file) { |
| 31 | + return file.path; |
| 32 | + }); |
| 33 | + return [files.path]; |
| 34 | +} |
25 | 35 |
|
| 36 | +exports.cover = function(options, run) { |
| 37 | + istanbul.matcherFor({ |
| 38 | + includes: resolvePaths(options.code), |
| 39 | + excludes: resolvePaths(options.tests) |
| 40 | + }, function (err, matcher) { |
| 41 | + if (err) { throw err; } |
| 42 | + var instrumenter = new istanbul.Instrumenter(); |
| 43 | + istanbul.hook.hookRequire(matcher, instrumenter.instrumentSync.bind(instrumenter)); |
| 44 | + run(); |
| 45 | + }); |
26 | 46 | }; |
27 | 47 |
|
28 | | -if (!bunker) { |
| 48 | +if (!istanbul) { |
29 | 49 | _.each(exports, function(fn, name) { |
30 | 50 | exports[name] = function() { |
31 | | - util.error('Module "bunker" is not installed.'.red); |
| 51 | + util.error('Module "istanbul" is not installed.'.red); |
32 | 52 | process.exit(1); |
33 | 53 | }; |
34 | 54 | }); |
|
0 commit comments