diff --git a/index.js b/index.js index 2f692b94..d14ffa6d 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,20 @@ module.exports = { // Ember Methods + included: function() { + if (this._isCoverageEnabled() && this.parent.isEmberCLIAddon()) { + var coveredAddon = this.project.findAddonByName(this.project.pkg.name); + var coverageAddonContext = this; + + coveredAddon.processedAddonJsFiles = function (tree){ + var instrumentedTree = coverageAddonContext.preprocessTree('addon-js', this.addonJsFiles(tree)); + return this.preprocessJs(instrumentedTree, '/', this.name, { + registry: this.registry + }); + } + } + }, + /** * Get content for type * @param {String} type - type to get content for @@ -39,7 +53,7 @@ module.exports = { var useBabelInstrumenter = this._getConfig().useBabelInstrumenter === true; // If coverage isn't enabled or tree is not JavaScript tree then we don't need to alter the tree - if (!this._isCoverageEnabled() || type !== 'js') { + if (!this._isCoverageEnabled() || (type !== 'js' && type !=='addon-js')) { return tree; } @@ -82,11 +96,11 @@ module.exports = { // Custom Methods /** - * Check if a file exists within the current app/addon - * @param {String} relativePath - path to file within current app/addon - * @returns {Boolean} whether or not the file exists within the current app/addon + * Check if a file exists within the current app directory + * @param {String} relativePath - path to file within current app + * @returns {Boolean} whether or not the file exists within the current app */ - _doesFileExistInCurrentProject: function(relativePath) { + _doesFileExistInCurrentProjectApp: function(relativePath) { relativePath = path.join('app', relativePath); if (this._existsSync(relativePath)) { @@ -96,6 +110,34 @@ module.exports = { return this._doesTemplateFileExist(relativePath); }, + /** + * Check if a file exists within the current addon directory + * @param {String} relativePath - path to file within current app + * @returns {Boolean} whether or not the file exists within the current app + */ + _doesFileExistInCurrentProjectAddon: function(relativePath) { + relativePath = path.join('addon', relativePath); + + if (this._existsSync(relativePath)) { + return true; + } + + return this._doesTemplateFileExist(relativePath); + }, + /** + * Check if a file exists within the current addon directory. Removing `module/` from the path. + * @param {String} relativePath - path to file within current app + * @returns {Boolean} whether or not the file exists within the current app + */ + _doesFileExistIsCurrentProjectAddonModule: function(relativePath) { + relativePath = path.join('addon', relativePath.replace(path.join('modules', this.project.pkg.name),'')); + + if (this._existsSync(relativePath)) { + return true; + } + + return this._doesTemplateFileExist(relativePath); + }, /** * Check if a file exists within the dummy app * @param {String} relativePath - path to file within dummy app @@ -138,7 +180,7 @@ module.exports = { * @param {String} path - path to check existence of * @returns {Boolean} whether or not path exists */ - _existsSync: function (path) { + _existsSync: function(path) { return existsSync(path); }, @@ -149,8 +191,8 @@ module.exports = { * @returns {Boolean} whether or not to filter out file */ _filterOutAddonFiles: function(name, relativePath) { - if(relativePath.indexOf(name + '/') === 0) { - relativePath = relativePath.replace(name + '/', '') + if (relativePath.indexOf(name + '/') === 0) { + relativePath = relativePath.replace(name + '/', ''); } if (relativePath.indexOf('dummy/') === 0) { @@ -159,9 +201,13 @@ module.exports = { var fileExists = ( this._doesFileExistInDummyApp(relativePath) || - this._doesFileExistInCurrentProject(relativePath) + this._doesFileExistInCurrentProjectApp(relativePath) || + this._doesFileExistInCurrentProjectAddon(relativePath) || + this._doesFileExistIsCurrentProjectAddonModule(relativePath) ); + console.log(!fileExists, name, relativePath); + return !fileExists; }, @@ -181,6 +227,7 @@ module.exports = { var excludes = this._getConfig().excludes || []; var name = this.parent.pkg.name; excludes.push(this._filterOutAddonFiles.bind(this, name)); + return excludes; }, diff --git a/lib/coverage-instrumenter.js b/lib/coverage-instrumenter.js index 873c26a3..1a6b4f17 100644 --- a/lib/coverage-instrumenter.js +++ b/lib/coverage-instrumenter.js @@ -97,7 +97,11 @@ CoverageInstrumenter.prototype.processString = function(content, relativePath) { relativePath = fixPath(relativePath, this._appName, this._appRoot, this._templateExtensions); - return instrumenter.instrumentSync(content, relativePath); + try { + return instrumenter.instrumentSync(content, relativePath); + } catch (e) { + console.error('Unable to cover:', relativePath, '. Try setting useBabelInstrumenter to true. \n', e.stack); + } }; module.exports = CoverageInstrumenter; diff --git a/test/unit/index-test.js b/test/unit/index-test.js index dcdbb0f2..e81319fa 100644 --- a/test/unit/index-test.js +++ b/test/unit/index-test.js @@ -3,7 +3,6 @@ var expect = require('chai').expect; var sinon = require('sinon'); var Index = require('../../index.js'); - describe('index.js', function() { var sandbox @@ -96,13 +95,13 @@ describe('index.js', function() { }); }); - describe('_doesFileExistInCurrentProject', function() { + describe('_doesFileExistInCurrentProjectApp', function() { describe('when file exists', function() { var result; beforeEach(function() { sandbox.stub(Index, '_existsSync').returns(true); - result = Index._doesFileExistInCurrentProject('adapters/application.js'); + result = Index._doesFileExistInCurrentProjectApp('adapters/application.js'); }); it('uses path to file in app', function() { @@ -124,7 +123,7 @@ describe('index.js', function() { beforeEach(function() { sandbox.stub(Index, '_doesTemplateFileExist').returns(true); - result = Index._doesFileExistInCurrentProject('templates/application.js'); + result = Index._doesFileExistInCurrentProjectApp('templates/application.js'); }); it('uses path to file in app', function() { @@ -141,7 +140,7 @@ describe('index.js', function() { beforeEach(function() { sandbox.stub(Index, '_doesTemplateFileExist').returns(false); - result = Index._doesFileExistInCurrentProject('templates/application.js'); + result = Index._doesFileExistInCurrentProjectApp('templates/application.js'); }); it('uses path to file in app', function() {