Skip to content

Commit

Permalink
Merge pull request #8 from lukemelia/test-coverage
Browse files Browse the repository at this point in the history
Add test coverage and willDeploy config validation
  • Loading branch information
Aaron Chambers committed May 29, 2015
2 parents ece39bd + 297ff61 commit 86847e8
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 7 deletions.
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ var Promise = require('ember-cli/lib/ext/promise');
var glob = require('glob');
var chalk = require('chalk');
var blue = chalk.blue;
var validateConfig = require('./lib/utilities/validate-config');

module.exports = {
name: 'ember-cli-deploy-build',

createDeployPlugin: function(options) {
function _beginMessage(ui, outputPath) {
function _beginMessage(ui, buildEnv, outputPath) {
ui.write(blue('| '));
ui.writeLine(blue('- building files to `' + outputPath + '`'));
ui.writeLine(blue('- building app using buildEnv `' + buildEnv + '` to `' + outputPath + '`'));

return Promise.resolve();
}
Expand All @@ -37,6 +38,18 @@ module.exports = {
return {
name: options.name,

willDeploy: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] = deployment.config[this.name] || {};

return validateConfig(ui, config)
.then(function() {
ui.write(blue('| '));
ui.writeLine(blue('- config ok'));
});
},

build: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
Expand All @@ -54,7 +67,7 @@ module.exports = {
project: project
});

return _beginMessage(ui, outputPath)
return _beginMessage(ui, buildEnv, outputPath)
.then(builder.build.bind(builder))
.finally(function() {
return builder.cleanup();
Expand Down
29 changes: 29 additions & 0 deletions lib/utilities/validate-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var Promise = require('ember-cli/lib/ext/promise');

var chalk = require('chalk');
var yellow = chalk.yellow;
var blue = chalk.blue;

function applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui){
if (!config[prop]) {
var value = defaultConfig[prop];
config[prop] = value;
ui.write(blue('| '));
ui.writeLine(yellow('- Missing config: `' + prop + '`, using default: `' + value + '`'));
}
}

module.exports = function(ui, config) {
ui.write(blue('| '));
ui.writeLine(blue('- validating config'));

var defaultConfig = {
buildEnv: 'production',
buildPath: 'tmp/dist-deploy'
};
['buildEnv', 'buildPath'].forEach(function(propName){
applyDefaultConfigIfNecessary(config, propName, defaultConfig, ui);
});

return Promise.resolve();
}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "ember server",
"build": "ember build",
"test": "ember try:testall"
"test": "node tests/runner.js"
},
"repository": "https://github.com/zapnito/ember-cli-deploy-build",
"engines": {
Expand All @@ -19,6 +19,8 @@
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.2",
"chai": "^2.2.0",
"chai-as-promised": "^5.0.0",
"ember-cli": "0.2.3",
"ember-cli-app-version": "0.3.3",
"ember-cli-content-security-policy": "0.4.0",
Expand All @@ -31,7 +33,9 @@
"ember-data": "1.0.0-beta.16.1",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-export-application-global": "^1.0.2",
"ember-try": "0.0.4"
"ember-try": "0.0.4",
"glob": "^5.0.5",
"mocha": "^2.2.4"
},
"keywords": [
"ember-addon",
Expand All @@ -40,7 +44,8 @@
"dependencies": {
"chalk": "^1.0.0",
"ember-cli-babel": "^5.0.0",
"glob": "^5.0.5"
"glob": "^5.0.5",
"rsvp": "^3.0.18"
},
"ember-addon": {
"configPath": "tests/dummy/config"
Expand Down
8 changes: 7 additions & 1 deletion tests/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
"andThen",
"currentURL",
"currentPath",
"currentRouteName"
"currentRouteName",
"require",
"describe",
"before",
"beforeEach",
"it",
"process"
],
"node": false,
"browser": false,
Expand Down
28 changes: 28 additions & 0 deletions tests/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*jshint globalstrict: true*/
'use strict';

var glob = require('glob');
var Mocha = require('mocha');

var mocha = new Mocha({
reporter: 'spec'
});

var arg = process.argv[2];
var root = 'tests/';

function addFiles(mocha, files) {
glob.sync(root + files).forEach(mocha.addFile.bind(mocha));
}

addFiles(mocha, '/**/*-nodetest.js');

if (arg === 'all') {
addFiles(mocha, '/**/*-nodetest-slow.js');
}

mocha.run(function(failures) {
process.on('exit', function() {
process.exit(failures);
});
});
112 changes: 112 additions & 0 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*jshint globalstrict: true*/
'use strict';

var RSVP = require('ember-cli/lib/ext/promise');

var assert = require('ember-cli/tests/helpers/assert');

describe('build plugin', function() {
var subject;

before(function() {
subject = require('../../index');
});

it('has a name', function() {
var result = subject.createDeployPlugin({
name: 'test-plugin'
});

assert.equal(result.name, 'test-plugin');
});

it('implements the correct hooks', function() {
var result = subject.createDeployPlugin({
name: 'test-plugin'
});

assert.equal(typeof result.willDeploy, 'function');
assert.equal(typeof result.build, 'function');
});

describe('willDeploy hook', function() {
it('resolves if config is ok', function() {
var plugin = subject.createDeployPlugin({
name: 'build'
});

var context = {
deployment: {
ui: { write: function() {}, writeLine: function() {} },
config: {
}
}
};
return assert.isFulfilled(plugin.willDeploy.call(plugin, context));
});
});

describe('build hook', function() {
var plugin;
var context;

beforeEach(function() {
plugin = subject.createDeployPlugin({
name: 'build'
});

context = {
redisClient: {
upload: function() {
return RSVP.resolve('redis-key');
}
},
tag: 'some-tag',
deployment: {
ui: { write: function() {}, writeLine: function() {} },
project: { name: function() { return 'test-project'; }, addons: [], root: 'tests/dummy' },
config: {
build: {
buildEnv: 'development',
buildPath: 'tmp/dist-deploy',
}
}
}
};
});

it('builds the app and returns distDir and distFiles', function(done) {
this.timeout(50000);
return assert.isFulfilled(plugin.build.call(plugin, context))
.then(function(result) {
assert.deepEqual(result, {
distDir: 'dist',
distFiles: [
'assets/dummy.css',
'assets/dummy.js',
'assets/dummy.map',
'assets/ember-data.js.map',
'assets/failed.png',
'assets/passed.png',
'assets/test-loader.js',
'assets/test-support.css',
'assets/test-support.js',
'assets/test-support.map',
'assets/vendor.css',
'assets/vendor.js',
'assets/vendor.map',
'crossdomain.xml',
'index.html',
'robots.txt',
'testem.js',
'tests/index.html'
]
});
done();
}).catch(function(reason){
console.log(reason.actual.stack);
done(reason.actual);
});
});
});
});
81 changes: 81 additions & 0 deletions tests/unit/lib/utilities/validate-config-nodetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* node:true */
var assert = require('ember-cli/tests/helpers/assert');

describe('validate-config', function() {
var subject;
var config;
var mockUi;

before(function() {
subject = require('../../../../lib/utilities/validate-config');
});

beforeEach(function() {
mockUi = {
messages: [],
write: function() { },
writeLine: function(message) {
this.messages.push(message);
}
};
});

describe('without providing config', function () {
beforeEach(function() {
config = { };
});
it('warns about missing optional config', function() {
return assert.isFulfilled(subject(mockUi, config))
.then(function() {
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing config:\s.*, using default:\s/.test(current)) {
previous.push(current);
}

return previous;
}, []);

assert.equal(messages.length, 2);
});
});

it('adds default config to the config object', function() {
return assert.isFulfilled(subject(mockUi, config))
.then(function() {
assert.isDefined(config.buildEnv);
assert.isDefined(config.buildPath);
});
});

it('resolves', function() {
return assert.isFulfilled(subject(mockUi, config));
});
});

describe('with a buildEnv and buildPath provided', function () {
beforeEach(function() {
config = {
buildEnv: 'development',
buildPath: 'tmp/dist-deploy'
};
});
it('does not warns about missing optional config', function() {
return assert.isFulfilled(subject(mockUi, config))
.then(function() {
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing config:\s.*, using default:\s/.test(current)) {
previous.push(current);
}

return previous;
}, []);

assert.equal(messages.length, 0);
});
});

it('resolves', function() {
return assert.isFulfilled(subject(mockUi, config));
});
});
});

0 comments on commit 86847e8

Please sign in to comment.