From 63c3b46be1419ceb0966c491543e10f7376163a2 Mon Sep 17 00:00:00 2001 From: Dan Freeman Date: Wed, 21 Sep 2016 14:29:38 -0400 Subject: [PATCH] Handle addons with different package names. --- index.js | 32 ++++++++++++++++++--- test/unit/index-test.js | 63 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2a40700d..82f71b60 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ module.exports = { included: function() { if (this._isCoverageEnabled() && this.parent.isEmberCLIAddon()) { - var coveredAddon = this.project.findAddonByName(this.project.pkg.name); + var coveredAddon = this._findCoveredAddon(); var coverageAddonContext = this; coveredAddon.processedAddonJsFiles = function (tree){ @@ -65,7 +65,7 @@ module.exports = { // Instrument JavaScript for code coverage var instrumentedNode = new CoverageInstrumenter(appFiles, { annotation: 'Instrumenting for code coverage', - appName: this.parent.pkg.name, + appName: this._parentName(), appRoot: this.parent.root, babelOptions: this.app.options.babel, useBabelInstrumenter: useBabelInstrumenter, @@ -130,7 +130,7 @@ module.exports = { * @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),'')); + relativePath = path.join('addon', relativePath.replace(path.join('modules', this._parentName()),'')); if (this._existsSync(relativePath)) { return true; @@ -223,7 +223,7 @@ module.exports = { */ _getExcludes: function() { var excludes = this._getConfig().excludes || []; - var name = this.parent.pkg.name; + var name = this._parentName(); excludes.push(this._filterOutAddonFiles.bind(this, name)); return excludes; @@ -241,5 +241,29 @@ module.exports = { } return ['true', true].indexOf(value) !== -1; + }, + + /** + * Determine the name of the parent app or addon. + * @returns {String} the name of the parent + */ + _parentName: function() { + if (this.parent.isEmberCLIAddon()) { + return this._findCoveredAddon().name; + } else { + return this.parent.name; + } + }, + + /** + * Find the addon (if any) that's being covered. + * @returns {Addon} the addon under test + */ + _findCoveredAddon: function() { + if (!this._coveredAddon) { + this._coveredAddon = this.project.findAddonByName(this.project.pkg.name); + } + + return this._coveredAddon; } }; diff --git a/test/unit/index-test.js b/test/unit/index-test.js index e81319fa..85f946d6 100644 --- a/test/unit/index-test.js +++ b/test/unit/index-test.js @@ -249,13 +249,13 @@ describe('index.js', function() { }); }); - describe('_getEcludes', function() { + describe('_getExcludes', function() { beforeEach(function() { sandbox.stub(Index, '_filterOutAddonFiles').returns('test'); Index.parent = { - pkg: { - name: 'foo-bar' + isEmberCLIAddon: function() { + return false; } }; }); @@ -397,4 +397,61 @@ describe('index.js', function() { }); }); }); + + describe('_parentName', function() { + var isAddon; + + beforeEach(function() { + Index.parent = { + name: 'parent-app', + isEmberCLIAddon: function() { + return isAddon; + } + }; + }); + + describe('when parent is an app', function() { + beforeEach(function() { + isAddon = false; + }); + + it('returns the app name', function() { + expect(Index._parentName()).to.equal('parent-app'); + }); + }); + + describe('when parent is an addon', function() { + beforeEach(function() { + isAddon = true; + sandbox.stub(Index, '_findCoveredAddon').returns({ name: 'some-addon' }); + }); + + it('returns the addon name', function() { + expect(Index._parentName()).to.equal('some-addon'); + }); + }); + }); + + describe('_findCoveredAddon', function() { + var result; + + beforeEach(function() { + Index.project = { + findAddonByName: sinon.stub().returns({ name: 'my-addon' }), + pkg: { + name: '@scope/ember-cli-my-addon' + } + }; + + result = Index._findCoveredAddon(); + }); + + it('looks up the addon by the package name', function() { + expect(Index.project.findAddonByName.calledWith('@scope/ember-cli-my-addon')).to.be.true; + }); + + it('returns the located addon', function() { + expect(result.name).to.equal('my-addon'); + }); + }); });