Skip to content

Commit

Permalink
Test conflicting on-disk actions
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Sep 22, 2016
1 parent 5598179 commit 816167e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/app/private/controller/load-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ module.exports = function (cb) {
var actionIdentity = (identity + path.sep + actionName).toLowerCase().replace((new RegExp(path.sep,'g')), '.');
// If the action identity matches one we've already loaded from disk, bail.
if (actionsLoadedFromDisk[actionIdentity]) {
throw new Error('The action `' + actionName + '` in `' + filePath + '` conflicts with a previously-loaded action.');
var conflictError = new Error('The action `' + actionName + '` in `' + filePath + '` conflicts with a previously-loaded action.');
conflictError.code = 'E_CONFLICT';
conflictError.identity = actionIdentity;
throw conflictError;
}
// Attempt to load the action into our set of actions.
// This may throw an error, which will be caught below.
Expand All @@ -75,7 +78,10 @@ module.exports = function (cb) {
// e.g. somefolder.dostuff
var actionIdentity = match[1].toLowerCase().replace((new RegExp(path.sep,'g')), '.');
if (actionsLoadedFromDisk[actionIdentity]) {
throw new Error('The action `' + _.last(actionIdentity.split('.')) + '` in `' + filePath + '` conflicts with a previously-loaded action.');
var conflictError = new Error('The action `' + _.last(actionIdentity.split('.')) + '` in `' + filePath + '` conflicts with a previously-loaded action.');
conflictError.code = 'E_CONFLICT';
conflictError.identity = actionIdentity;
throw conflictError;
}
// Attempt to load the action into our set of actions.
// This may throw an error, which will be caught below.
Expand Down
54 changes: 53 additions & 1 deletion test/integration/controllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ describe('controllers :: ', function() {
});
});

after(function() {
after(function(done) {
sailsApp.lower(function() {
process.chdir(curDir);
return done();
});
});

Expand Down Expand Up @@ -266,4 +267,55 @@ describe('controllers :: ', function() {

});

describe.only('with conflicting actions in api/controllers', function() {

var curDir, tmpDir, sailsApp;
var warn;
var warnings = [];

before(function(done) {
// Cache the current working directory.
curDir = process.cwd();
// Create a temp directory.
tmpDir = tmp.dirSync({gracefulCleanup: true, unsafeCleanup: true});
// Switch to the temp directory.
process.chdir(tmpDir.name);
// Create a top-level legacy controller file.
Filesystem.writeSync({
force: true,
destination: 'api/controllers/TopLevelController.js',
string: 'module.exports = { fnAction: function (req, res) { res.send(\'fn controller action!\'); } };'
}).execSync();
// Create a top-level action file with a req/res function.
Filesystem.writeSync({
force: true,
destination: 'api/controllers/toplevel/fnaction.js',
string: 'module.exports = function (req, res) { res.send(\'standalone fn!\'); };'
}).execSync();

return done();

});

after(function() {
process.chdir(curDir);
});

it('should fail to load sails', function(done) {
// Load the Sails app.
appHelper.load({hooks: {views: false, grunt: false}, log: {level: 'error'}}, function(err, _sails) {
if (!err) {
_sails.lower(function() {
return done(new Error('Should have thrown an error!'));
});
}
assert.equal(err.code, 'E_CONFLICT');
assert.equal(err.identity, 'toplevel.fnaction');
return done();
});

});

});

});

0 comments on commit 816167e

Please sign in to comment.