Skip to content

Commit 626e7b0

Browse files
committed
feat(preprocessor): free instrumenter
Allow any Istanbul instrumenter to be used in the karma-coverage. Will help karma-runner#120 Closes karma-runner#101 karma-runner#49 BREAKING CHANGE: Karma-coverage does not ship with additional instrumenter. You need to explicitly install the instrumenter you need. Removed **Ibrik** instrumenter that need to be installed explicitly. Quick list of known community instrumenters : - [Ibrik](https://github.com/Constellation/ibrik) (CoffeeScript files instrumenter). - [Ismailia](https://github.com/Spote/ismailia) (ES6 files instrumenter using Traceur). - [Isparta](https://github.com/douglasduteil/isparta) (ES6 files instrumenter using 6to5).
1 parent 0c99f75 commit 626e7b0

File tree

4 files changed

+76
-24
lines changed

4 files changed

+76
-24
lines changed

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,26 +206,47 @@ coverageReporter: {
206206
```
207207

208208
#### instrumenter
209-
Karma-coverage infers the instrumenter regarding of the file extension.
210-
The `.coffee` files are by default covered using
211-
[Ibrik](https://github.com/Constellation/ibrik) (an
212-
[Istanbul](https://github.com/gotwarlost/istanbul) analog for
213-
CoffeeScript files). It is possible to override this behavior and point out an
209+
Karma-coverage can infers the instrumenter regarding of the file extension.
210+
It is possible to override this behavior and point out an
214211
instrumenter for the files matching a specific pattern.
215212
To do so, you need to declare an object under with the keys represents the
216213
pattern to match, and the instrumenter to apply. The matching will be done
217214
using [minimatch](https://github.com/isaacs/minimatch).
218215
If two patterns match, the last one will take the precedence.
219216

217+
For example you can use [Ibrik](https://github.com/Constellation/ibrik) (an
218+
[Istanbul](https://github.com/gotwarlost/istanbul) analog for
219+
CoffeeScript files) with:
220+
220221
```javascript
221222
coverageReporter: {
223+
instrumenters: { ibrik : require('ibrik') }
222224
instrumenter: {
223-
'**/*.coffee': 'istanbul' // Force the use of the Istanbul instrumenter to cover CoffeeScript files
225+
'**/*.coffee': 'ibrik'
224226
},
225227
// ...
226228
}
227229
```
228230

231+
You can pass options additional options to specific instrumenter with:
232+
233+
```javascript
234+
var to5Options = { experimental: true };
235+
236+
// [...]
237+
238+
coverageReporter: {
239+
instrumenters: { isparta : require('isparta') },
240+
instrumenter: {
241+
'**/*.js': 'isparta'
242+
},
243+
instrumenterOptions: {
244+
isparta: { to5 : to5Options }
245+
}
246+
}
247+
```
248+
249+
229250
----
230251

231252
For more information on Karma see the [homepage].

lib/preprocessor.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
var istanbul = require('istanbul'),
2-
ibrik = require('ibrik'),
32
minimatch = require('minimatch'),
4-
globalSourceCache = require('./sourceCache');
3+
globalSourceCache = require('./sourceCache'),
4+
extend = require('util')._extend;
55

66
var createCoveragePreprocessor = function(logger, basePath, reporters, coverageReporter) {
77
var log = logger.create('preprocessor.coverage');
88
var instrumenterOverrides = (coverageReporter && coverageReporter.instrumenter) || {};
9-
var instrumenters = {istanbul: istanbul, ibrik: ibrik};
9+
var instrumenters = extend({istanbul: istanbul}, (coverageReporter && coverageReporter.instrumenters));
1010
var sourceCache = globalSourceCache.getByBasePath(basePath);
11+
var instrumentersOptions = Object.keys(instrumenters).reduce(function getInstumenterOptions(memo, instrumenterName){
12+
memo[instrumenterName] = (coverageReporter && coverageReporter.instrumenterOptions && coverageReporter.instrumenterOptions[instrumenterName]) || {};
13+
return memo;
14+
}, {});
1115

1216
// if coverage reporter is not used, do not preprocess the files
1317
if (reporters.indexOf('coverage') === -1) {
@@ -20,8 +24,8 @@ var createCoveragePreprocessor = function(logger, basePath, reporters, coverageR
2024
function checkInstrumenters() {
2125
var literal;
2226
for (var pattern in instrumenterOverrides) {
23-
literal = String(instrumenterOverrides[pattern]).toLowerCase();
24-
if (literal !== 'istanbul' && literal !== 'ibrik') {
27+
literal = String(instrumenterOverrides[pattern]);
28+
if (Object.keys(instrumenters).indexOf(literal) < 0) {
2529
log.error('Unknown instrumenter: %s', literal);
2630
return false;
2731
}
@@ -38,25 +42,23 @@ var createCoveragePreprocessor = function(logger, basePath, reporters, coverageR
3842
log.debug('Processing "%s".', file.originalPath);
3943

4044
var jsPath = file.originalPath.replace(basePath + '/', './');
41-
var instrumenterLiteral = jsPath.match(/\.coffee$/) ? 'ibrik' : 'istanbul';
45+
// default instrumenters
46+
var instrumenterLiteral = 'istanbul';
4247

4348
for (var pattern in instrumenterOverrides) {
4449
if (minimatch(file.originalPath, pattern, {dot: true})) {
45-
instrumenterLiteral = String(instrumenterOverrides[pattern]).toLowerCase();
50+
instrumenterLiteral = String(instrumenterOverrides[pattern]);
4651
}
4752
}
4853

49-
var instrumenter = new instrumenters[instrumenterLiteral].Instrumenter();
54+
var instrumenter = new instrumenters[instrumenterLiteral].Instrumenter(instrumentersOptions[instrumenterLiteral] || {});
5055

5156
instrumenter.instrument(content, jsPath, function(err, instrumentedCode) {
57+
5258
if (err) {
5359
log.error('%s\n at %s', err.message, file.originalPath);
5460
}
5561

56-
if (instrumenterLiteral === 'ibrik') {
57-
file.path = file.path.replace(/\.coffee$/, '.js');
58-
}
59-
6062
// remember the actual immediate instrumented JS for given original path
6163
sourceCache[jsPath] = content;
6264

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"author": "SATO taichi <ryushi@gmail.com>",
2121
"dependencies": {
2222
"istanbul": "~0.3.0",
23-
"ibrik": "~2.0.0",
2423
"dateformat": "~1.0.6",
2524
"minimatch": "~0.3.0"
2625
},

test/preprocessor.spec.coffee

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,50 @@ describe 'preprocessor', ->
5757
expect(sandbox.__coverage__).to.have.ownProperty './file.js'
5858
done()
5959

60-
it 'should preprocess the coffee code', (done) ->
61-
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'], {}
62-
file = new File '/base/path/file.coffee'
60+
it 'should preprocess the fake code', (done) ->
61+
fakeInstanbulLikeInstrumenter = ->
62+
fakeInstanbulLikeInstrumenter::instrument = (_a, _b, callback) ->
63+
callback()
64+
return
65+
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'],
66+
instrumenters:
67+
fakeInstanbulLike :
68+
Instrumenter : fakeInstanbulLikeInstrumenter
69+
instrumenter:
70+
'**/*.fake': 'fakeInstanbulLike'
71+
file = new File '/base/path/file.fake'
6372

6473
process ORIGINAL_COFFEE_CODE, file, (preprocessedCode) ->
6574
sandbox =
6675
a: true
6776
something: ->
6877

6978
vm.runInNewContext preprocessedCode, sandbox
70-
expect(file.path).to.equal '/base/path/file.js'
71-
expect(sandbox.__coverage__).to.have.ownProperty './file.coffee'
79+
expect(file.path).to.equal '/base/path/file.fake'
7280
done()
7381

82+
it 'should preprocess the fake code with the config options', (done) ->
83+
fakeInstanbulLikeInstrumenter = (options) ->
84+
expect(options.experimental).to.be.ok
85+
return
86+
fakeInstanbulLikeInstrumenter::instrument = (_a, _b, callback) ->
87+
callback()
88+
return
89+
90+
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'],
91+
instrumenters:
92+
fakeInstanbulLike:
93+
Instrumenter: fakeInstanbulLikeInstrumenter
94+
instrumenterOptions:
95+
fakeInstanbulLike:
96+
experimental: yes
97+
instrumenter:
98+
'**/*.fake': 'fakeInstanbulLike'
99+
100+
file = new File '/base/path/file.fake'
101+
102+
process ORIGINAL_COFFEE_CODE, file, done
103+
74104
it 'should not preprocess the coffee code', (done) ->
75105
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress'],
76106
instrumenter:

0 commit comments

Comments
 (0)