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
Copy path View file
@@ -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; return this;
} }


Copy path View file
@@ -9,6 +9,7 @@ const Service = require('../../lib/classes/Service');
const Utils = require('../../lib/classes/Utils'); const Utils = require('../../lib/classes/Utils');
const Serverless = require('../../lib/Serverless'); const Serverless = require('../../lib/Serverless');
const testUtils = require('../../tests/utils'); const testUtils = require('../../tests/utils');
const CLI = require('../../lib/classes/CLI');


// Configure chai // Configure chai
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
@@ -722,6 +723,46 @@ describe('Service', () => {
.to.throw('Events for "functionA" must be an array, not an string'); .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', () => { describe('#mergeArrays', () => {
ProTip! Use n and p to navigate between commits in a pull request.