Skip to content

Commit

Permalink
Rewrites files.tryRequire to fail on failed requires (inside required…
Browse files Browse the repository at this point in the history
… module) (#375)

* Rewrites files.tryRequire to fail on failed requires inside the required file

* Removes console log

* Removes log import

* Updates tests

* Rewords some test descriptions
  • Loading branch information
cperryk committed Feb 8, 2017
1 parent 5d22f8c commit 7f7a332
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
13 changes: 4 additions & 9 deletions lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let _ = require('lodash'),
yaml = require('js-yaml'),
pkg = require(path.resolve('package.json')),
temp2env = require('template2env'),
log = require('./services/log').withStandardPrefix(__filename),
req = require,
allowedEnvFiles = ['local', 'config'];

Expand Down Expand Up @@ -170,18 +169,14 @@ function getComponentPath(name) {
* @throw if fails for reason other than missing module
*/
function tryRequire(filePath) {
let result;
let resolvedPath;

try {
result = req(filePath);
resolvedPath = req.resolve(filePath);
} catch (ex) {
if (ex.message && !ex.message.match(/Cannot find module/i)) {
log('error', `Failed to load ${filePath}: ${ex.message}`);
throw ex;
}
return undefined;
}

return result;
return req(resolvedPath);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions lib/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe(_.startCase(filename), function () {

// require shouldn't be called dynamically, but here we go
req = sandbox.stub();
req.resolve = sandbox.stub();
lib.setRequire(req);
});

Expand Down Expand Up @@ -258,21 +259,22 @@ describe(_.startCase(filename), function () {
expect(fn(name)).to.equal(null);
});

it('throw normal errors from require', function () {
it('throws an error if the module exists but cannot be required successfully', function () {
const name = 'some name',
path = 'some path';

req.resolve.returns('/some-path');
req.throws(new Error('some error'));
lib.getComponentPath.returns(path);

expect(function () { fn(name); }).to.throw();
});

it('eats "Cannot find module" errors', function () {
it('returns undefined if the module does not exist', function () {
const name = 'some name',
path = 'some path';

req.throws(new Error('Cannot find module'));
req.resolve.throws(new Error('Cannot find module'));
lib.getComponentPath.returns(path);

expect(fn(name)).to.equal(undefined);
Expand All @@ -284,6 +286,7 @@ describe(_.startCase(filename), function () {
result = 'some result';

lib.getComponentPath.returns(path);
req.resolve.withArgs(path + '/model').returns(path + '/model');
req.withArgs(path + '/model').returns(result);

expect(fn(name)).to.equal(result);
Expand All @@ -295,6 +298,7 @@ describe(_.startCase(filename), function () {
result = 'some result';

lib.getComponentPath.returns(path);
req.resolve.withArgs(path).returns(path);
req.withArgs(path).returns(result);

expect(fn(name)).to.equal(result);
Expand All @@ -306,6 +310,7 @@ describe(_.startCase(filename), function () {
result = 'some result';

lib.getComponentPath.returns(path);
req.resolve.withArgs(path + '/server').returns(path + '/server');
req.withArgs(path + '/server').returns(result);

expect(fn(name)).to.equal(result);
Expand Down Expand Up @@ -343,6 +348,7 @@ describe(_.startCase(filename), function () {
result = [];

lib.getComponentPath.returns(path);
req.resolve.withArgs(path + '/package.json').returns(path + '/package.json');
req.withArgs(path + '/package.json').returns(result);

expect(fn(name)).to.equal(result);
Expand Down

0 comments on commit 7f7a332

Please sign in to comment.