Skip to content

Commit

Permalink
feat: do not preprocess files if coverage reporter is not used
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
vojtajina committed Jul 22, 2013
1 parent bc20465 commit 277a1ad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
11 changes: 9 additions & 2 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
var istanbul = require('istanbul');

var createCoveragePreprocessor = function(logger, basePath) {
var createCoveragePreprocessor = function(logger, basePath, reporters) {
var log = logger.create('preprocessor.coverage');
var instrumenter = new istanbul.Instrumenter();

// if coverage reporter is not used, do not preprocess the files
if (reporters.indexOf('coverage') === -1) {
return function(content, _, done) {
done(content);
};
}

return function(content, file, done) {
log.debug('Processing "%s".', file.originalPath);

Expand All @@ -17,6 +24,6 @@ var createCoveragePreprocessor = function(logger, basePath) {
};
};

createCoveragePreprocessor.$inject = ['logger', 'config.basePath'];
createCoveragePreprocessor.$inject = ['logger', 'config.basePath', 'config.reporters'];

module.exports = createCoveragePreprocessor;
52 changes: 52 additions & 0 deletions test/preprocessor.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
vm = require 'vm'
util = require 'util'

describe 'preprocessor', ->
createPreprocessor = require '../lib/preprocessor'

ORIGINAL_CODE = '''
if (a) {
something();
} else {
other();
}
'''


mockLogger = create: ->
error: -> throw new Error(util.format.apply util, arguments)
warn: -> null
info: -> null
debug: -> null

# TODO(vojta): refactor this somehow ;-) it's copy pasted from lib/file-list.js
File = (path, mtime) ->
@path = path
@originalPath = path
@contentPath = path
@mtime = mtime
@isUrl = false


it 'should not do anything if coverage reporter is not used', (done) ->
process = createPreprocessor mockLogger, null, ['dots', 'progress']
file = new File '/base/path/file.js'

process ORIGINAL_CODE, file, (preprocessedCode) ->
expect(preprocessedCode).to.equal ORIGINAL_CODE
expect(file.path).to.equal '/base/path/file.js'
done()


it 'should preprocess the code', (done) ->
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress']
file = new File '/base/path/file.js'

process ORIGINAL_CODE, file, (preprocessedCode) ->
sandbox =
a: true
something: ->

vm.runInNewContext preprocessedCode, sandbox
expect(sandbox.__coverage__).to.have.ownProperty './file.js'
done()
19 changes: 0 additions & 19 deletions test/reporter.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
# TODO(vojta): extract this into a separate file
sinon = require 'sinon'
chai = require 'chai'

# publish globals that all specs can use
global.expect = chai.expect
global.should = chai.should()
global.sinon = sinon

# chai plugins
chai.use(require 'sinon-chai')

beforeEach ->
global.sinon = sinon.sandbox.create()

afterEach ->
global.sinon.restore()


#==============================================================================
# lib/reporters/Coverage.js module
#==============================================================================
Expand Down

0 comments on commit 277a1ad

Please sign in to comment.