Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for multiple functions having same handler #5638

Merged
merged 6 commits into from Jan 1, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/classes/Service.js
Expand Up @@ -208,6 +208,28 @@ class Service {
}
});

const warnOnDuplicateHandlers = () => {
const functionObjs = this.getAllFunctions()
.map(name => ({
name,
body: this.getFunction(name),
}))
.filter(func => _.isString(func.body.handler));
const groupedByHandler = _.groupBy(functionObjs, func => func.body.handler);
_.entriesIn(groupedByHandler)
.filter(arg => arg[1].length >= 2)
.forEach(arg => {
const handlerName = arg[0];
const functionGroup = arg[1].map(func => func.name);
const warnMessage = [
`Warning: A handler "${handlerName}" is duplicated in functions: `,
`${functionGroup.join(', ')}.`,
].join('');
this.serverless.cli.log(warnMessage);
});
};
warnOnDuplicateHandlers();

return this;
}

Expand Down
41 changes: 41 additions & 0 deletions lib/classes/Service.test.js
Expand Up @@ -9,6 +9,7 @@ const Service = require('../../lib/classes/Service');
const Utils = require('../../lib/classes/Utils');
const Serverless = require('../../lib/Serverless');
const testUtils = require('../../tests/utils');
const CLI = require('../../lib/classes/CLI');

// Configure chai
chai.use(require('chai-as-promised'));
Expand Down Expand Up @@ -722,6 +723,46 @@ describe('Service', () => {
.to.throw('Events for "functionA" must be an array, not an string');
});
});

it('should warn if multiple functions have same handler', () => {
const SUtils = new Utils();
const serverlessYml = {
service: 'testService',
provider: 'testProvider',
functions: {
functionA: {
handler: 'foo.functionA',
events: [],
},
functionB: {
handler: 'foo.functionA',
events: [],
},
function123: {
handler: 'foo.function123',
events: [],
},
function456: {
handler: 'foo.function123',
events: [],
},
},
};
SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
YAML.dump(serverlessYml));

const serverless = new Serverless({ servicePath: tmpDirPath });

class CustomCLI extends CLI {}
const consoleLogStub = sinon.stub(CustomCLI.prototype, 'log');
serverless.cli = new CustomCLI(serverless);

return expect(serverless.service.load()).to.eventually.be.fulfilled
.then(() => {
serverless.service.validate();
expect(consoleLogStub.callCount).to.equal(2);
});
});
});

describe('#mergeArrays', () => {
Expand Down