Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
language: node_js
node_js:
- "0.10"
- "0.12"
- "4.2"
- "5"
- "6"

dist: trusty
sudo: required
Expand All @@ -26,9 +25,9 @@ env:
matrix:
fast_finish: true
include:
- node_js: "5"
- node_js: "6"
env: NPM_SCRIPT=jscs
- node_js: "5"
- node_js: "6"
env: NPM_SCRIPT=lint

before_install:
Expand Down
29 changes: 3 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,30 @@ module.exports = {
return this.preprocessJs(instrumentedTree, '/', this.name, {
registry: this.registry
});
}
};
}
},

/**
* Get content for type
* @param {String} type - type to get content for
* @returns {String} content for type
*/
contentFor: function(type) {
// If coverage is enabled add content to test-body-footer to show percentages
if (type === 'test-body-footer' && this._isCoverageEnabled()) {
return fs.readFileSync(path.join(__dirname, 'lib', 'templates', 'test-body-footer.html'));
var template = fs.readFileSync(path.join(__dirname, 'lib', 'templates', 'test-body-footer.html')).toString();
return template.replace('{%PROJECT_NAME%}', this._parentName());
}

return undefined;
},

/**
* Add instrumentation to JavaScript tree if coverage is enabled
* @param {String} type - what kind of tree
* @param {Tree} tree - tree to process
* @returns {[type]} processed tree
*/
preprocessTree: function(type, tree) {
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' && type !=='addon-js')) {
return tree;
}

// Make sure we exclude files defined in the configuration as well as files from addons
var appFiles = new Funnel(tree, {
exclude: this._getExcludes()
});

// Instrument JavaScript for code coverage
var instrumentedNode = new CoverageInstrumenter(appFiles, {
annotation: 'Instrumenting for code coverage',
appName: this._parentName(),
Expand All @@ -72,22 +58,13 @@ module.exports = {
templateExtensions: this.registry.extensionsForType('template')
});

// Return JavaScript tree with instrumented source
return new BroccoliMergeTrees([tree, instrumentedNode], { overwrite: true });
},

/**
* If coverage is enabled attach coverage middleware to the express server run by ember-cli
* @param {Object} startOptions - Express server start options
*/
serverMiddleware: function(startOptions) {
this.testemMiddleware(startOptions.app);
},

/**
* If coverage is enabled attach coverage middleware to the express server run by testem
* @param {Object} app - the Express app instance
*/
testemMiddleware: function(app) {
if (!this._isCoverageEnabled()) { return; }
attachMiddleware(app, { root: this.project.root, ui: this.ui });
Expand Down
12 changes: 12 additions & 0 deletions lib/templates/test-body-footer.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<script>
function sendCoverage(callback) {
Object.keys(require.entries).forEach(function(file) {
// Only load non-test files from the project
var parts = file.split('/');
if (parts[0] === '{%PROJECT_NAME%}' && parts[1] !== 'tests') {
try {
require(file);
} catch(e) {
throw 'error requiring file for coverage: '+file;
}
}
});

var coverageData = window.__coverage__;
var data = JSON.stringify(coverageData || {});

Expand Down
16 changes: 14 additions & 2 deletions test/unit/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var expect = require('chai').expect;
var sinon = require('sinon');
var Index = require('../../index.js');
describe('index.js', function() {
var sandbox
var sandbox;

beforeEach(function() {
sandbox = sinon.sandbox.create();
Expand All @@ -14,7 +14,7 @@ describe('index.js', function() {
return ['hbs'];
}
};
})
});

afterEach(function() {
sandbox.restore();
Expand All @@ -34,6 +34,14 @@ describe('index.js', function() {
describe('with coverage enabled', function() {
beforeEach(function() {
sandbox.stub(Index, '_isCoverageEnabled').returns(true);
Index.parent = {
isEmberCLIAddon: function() {
return false;
},
name: function() {
return 'fake-app';
}
};
});

it('does nothing if type is not test-body-footer', function() {
Expand All @@ -43,6 +51,10 @@ describe('index.js', function() {
it('returns template for test-body-footer', function() {
expect(Index.contentFor('test-body-footer')).to.match(/sendCoverage/);
});

it('includes the project name in the template for test-body-footer', function() {
expect(Index.contentFor('test-body-footer')).to.match(/fake-app/);
});
});
});

Expand Down