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

[Fix #2418] Allow function event definitions to be variables #2434

Merged
merged 6 commits into from
Jan 24, 2017
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
3 changes: 3 additions & 0 deletions lib/Serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Serverless {
// (https://github.com/serverless/serverless/issues/2041)
this.variables.populateService(this.pluginManager.cliOptions);

// validate the service configuration, now that variables are loaded
this.service.validate();

// trigger the plugin lifecycle when there's something which should be processed
return this.pluginManager.run(this.processedInput.commands);
}
Expand Down
15 changes: 11 additions & 4 deletions lib/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ class Service {
if (!functionObj.events) {
that.functions[functionName].events = [];
}
if (!_.isArray(functionObj.events)) {
throw new SError(`Events for "${functionName}" must be an array,` +
` not an ${typeof functionObj.events}`);
}

if (!functionObj.name) {
that.functions[functionName].name =
Expand All @@ -167,6 +163,17 @@ class Service {
});
}

validate() {
_.forEach(this.functions, (functionObj, functionName) => {
if (!_.isArray(functionObj.events)) {
throw new SError(`Events for "${functionName}" must be an array,` +
` not an ${typeof functionObj.events}`);
}
});

return this;
}

update(data) {
return _.merge(this, data);
}
Expand Down
42 changes: 29 additions & 13 deletions lib/classes/Service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,36 +392,52 @@ describe('Service', () => {
YAML.dump(serverlessYml));

const serverless = new Serverless({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);
serverless.service = new Service(serverless);
serverless.variables.service = serverless.service;

return serviceInstance.load().then(() => {
expect(serverless.service.functions).to.deep.equal({});
return serverless.service.load().then(() => {
// if we reach this, then no error was thrown
// populate variables in service configuration
serverless.variables.populateService();

// validate the service configuration, now that variables are loaded
serviceInstance.validate();

expect(serviceInstance.functions.functionA.events).to.deep.equal({});
}).catch(() => {
// make assertion fail intentionally to let us know something is wrong
expect(1).to.equal(2);
});
});

it("should throw error if a function's event is not an array", () => {
it('should throw error if a function\'s event is not an array or a variable', () => {
const SUtils = new Utils();
const serverlessYml = {
service: 'service-name',
provider: 'aws',
functions: {
functionA: {
events: {},
events: 'not an array or a variable',
},
},
};
SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
YAML.dump(serverlessYml));

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

return serviceInstance.load().then(() => {
// if we reach this, then no error was thrown as expected
// so make assertion fail intentionally to let us know something is wrong
expect(1).to.equal(2);
}).catch(e => {
expect(e.name).to.be.equal('ServerlessError');
serverless.service = new Service(serverless);

return serverless.service.load().then(() => {
// validate the service configuration, now that variables are loaded
try {
serverless.service.validate();

// if we reach this, then no error was thrown as expected
// so make assertion fail intentionally to let us know something is wrong
expect(1).to.equal(2);
} catch (e) {
expect(e.name).to.be.equal('ServerlessError');
}
});
});

Expand Down