Skip to content

Commit

Permalink
code coverage and minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexej Yaroshevich committed Nov 30, 2014
1 parent 96d035b commit a101e1e
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ node_js:
- "0.10"
- "0.11"

matrix:
allow_failures:
- node_js: "0.11"
script: npm run travis
18 changes: 13 additions & 5 deletions lib/pym.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function App(opts) {
* or package object, or package function
* @returns {PYM} this
*/
this.usePackage = function use(package, options) {
this.usePackage = function usePackage(package, options) {
options = options || {};

var resolvedPackage = _resolvePackageSync(path, package);
Expand All @@ -62,9 +62,17 @@ function App(opts) {

// load required modules if passed
if (opts.uses) {
opts.uses.forEach(function (plugin) {
this.use(plugin);
}, this);
if (Array.isArray(opts.uses)) {
opts.uses.forEach(function (plugin) {
this.usePackage(plugin);
}, this);
} else if (typeof opts.uses === 'object') {
Object.keys(opts.uses).forEach(function (key) {
this.usePackage(key, opts.uses[key]);
}, this);
} else {
throw new Error('Unsupported value in uses property');
}
}
}

Expand Down Expand Up @@ -142,7 +150,7 @@ function _wrapToArchitect(setup, metadata) {
function _provideService(provide, cached, service) {
if (cached.err) {
provide(null, cached.err);
} else if (!cached.result[service]) {
} else if (!cached.result || !cached.result[service]) {
provide(null, new Error('Invalid architect plugin found on service ' + service));
} else {
provide(cached.result[service]);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies": {
"istanbul": "^0.3.2",
"istanbul-coveralls": "1.0.x",
"jscs": "1.8.x",
"jscs-jsdoc": "0.2.x",
"jshint": "~2.5.10",
Expand All @@ -44,7 +45,7 @@
"lint": "jshint lib test && jscs lib test",
"test": "npm run lint && mocha",
"travis": "npm run test && npm run coveralls",
"coveralls": ""
"coveralls": "istanbul cover ./node_modules/.bin/_mocha -- -R spec && istanbul-coveralls"
},
"licenses": [
{
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/modules/architect-invalid-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Example of invalid architect api plugin
* @param {Object} options
* @param {Object} imports
* @param {function(Error, Object)} register
*/
module.exports = function (options, imports, register) {
if (options.throwError) {
register(new Error('invalid plugin error'));
} else if (options.returnInvalidString) {
register(null, 'invalid');
} else if (options.multipleTimes) {
register(null, {
config: '1'
});
register(null, {
config: '2'
});
}
};
8 changes: 8 additions & 0 deletions test/fixtures/modules/architect-invalid-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "architect-imports-abc",
"version": "0.0.1",
"main": "index.js",
"plugin": {
"provides": ["invalid"]
}
}
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--reporter spec
--timeout 10
--timeout 20
--growl
146 changes: 146 additions & 0 deletions test/simple-app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,154 @@
var ASSERT = require('assert');
var domain = require('domain');
var util = require('util');

var App = require('..');

describe('Basic tests', function () {

it('Mono app should load package relative to main file', function (done) {
var app = App.create();
app.usePackage('../../../test/fixtures/modules/some-config');
app.require(['config'], function (config) {
ASSERT.equal(config.title, 'I\'m a config module. Trust me!');
ASSERT.equal(config.options, '{}');
done();
}, done);
});

it('App with uses should preload package', function (done) {
var app = App.create({
path: './test/fixtures/simple-app',
uses: ['some-config']
});
app.require(['config'], function (config) {
ASSERT.equal(config.title, 'I\'m a config module. Trust me!');
ASSERT.equal(config.options, '{}');
done();
}, done);
});

it('App with uses should preload package with config', function (done) {
var app = App.create({
path: './test/fixtures/simple-app',
uses: {'some-config': {yolo: 'swag'}}
});
app.require(['config'], function (config) {
ASSERT.equal(config.options, '{"yolo":"swag"}');
done();
}, done);
});

it('App with uses should throw if not array or object', function () {
ASSERT.throws(function () {
App.create({
path: './test/fixtures/simple-app',
uses: 'foo-bar'
});
});
});

it('Provide could resolve in an error', function (done) {
var d = domain.create();
d.on('error', function () { done(); });
d.run(function () {
var app = App.create();
app.define('module', function (provide) {
provide(null, new Error('Oops!'));
});
app.require('module', function (module) {
console.log('Should not be executed but resolves in ' + module);
});
});
});

it('should throw if architect plugin is with wrong api', function () {
ASSERT.throws(function () {
var app = App.create();
app.usePackage('./unknown/');
},
/Can\'t find "/);
});

it('should throw if module name or path is wrong', function () {
ASSERT.throws(function () {
var app = App.create();
app.usePackage('./unknown/');
},
/Can\'t find "/);
});

it('should throw if module is with unknown format', function () {
ASSERT.throws(function () {
var app = App.create();
app.usePackage('./');
},
/Unsupported package/);
});

it('should throw if architect register calls with an error', function (done) {
shouldThrow(function () {
var app = App.create();
app.usePackage('../../../test/fixtures/modules/architect-invalid-plugin', {throwError: true});
app.require('invalid', function () {});
},
/Invalid acrhitect plugin format/, done);
});

it('should throw if architect register calls with non-object value', function (done) {
shouldThrow(function () {
var app = App.create();
app.usePackage('../../../test/fixtures/modules/architect-invalid-plugin', {returnInvalidString: true});
app.require('invalid', function () {});
},
/Invalid acrhitect plugin format/, done);
});

it('should throw if architect register called multiple times', function (done) {
shouldThrow(function () {
var app = App.create();
app.usePackage('../../../test/fixtures/modules/architect-invalid-plugin', {multipleTimes: true});
app.require('invalid', function () {});
},
/Service overloading not supported/, done);
});

it('should throw if architect register does not call', function (done) {
shouldThrow(function () {
var app = App.create();
app.usePackage('../../../test/fixtures/modules/architect-invalid-plugin', {empty: true});
app.require('invalid', function () {});
},
/Invalid architect plugin found on service/, done);
});

/**
* Helper for wrapping throws into domain events
* @param {Function} code
* @param {RegExp} [errRe]
* @param {Function} done
*/
function shouldThrow(code, errRe, done) {
if (arguments.length === 2) {
done = errRe;
errRe = null;
}

ASSERT(typeof code === 'function', 'Expect code callback as first argument');
ASSERT(typeof done === 'function', 'Expect done callback as last argument');

var d = domain.create();
d.on('error', function (err) {
if (!errRe || errRe.test(err)) {
done();
} else {
done(err);
}
});
d.run(code);
}
});

describe('Simple inherited app', function () {

var app;
Expand Down

0 comments on commit a101e1e

Please sign in to comment.