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 #2997 #3186

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

// populate function names after variables are loaded in case functions were externalized
// (https://github.com/serverless/serverless/issues/2997)
this.service.setFunctionNames(this.processedInput.options);

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

Expand Down
33 changes: 20 additions & 13 deletions lib/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,30 @@ class Service {
that.package.include = serverlessFile.package.include;
}

// setup function.name property
const stageNameForFunction = options.stage || this.provider.stage;
_.forEach(that.functions, (functionObj, functionName) => {
if (!functionObj.events) {
that.functions[functionName].events = [];
}

if (!functionObj.name) {
that.functions[functionName].name =
`${that.service}-${stageNameForFunction}-${functionName}`;
}
});

return this;
});
}

setFunctionNames(rawOptions) {
const that = this;
const options = rawOptions || {};
options.stage = options.stage || options.s;
options.region = options.region || options.r;

// setup function.name property
const stageNameForFunction = options.stage || this.provider.stage;
_.forEach(that.functions, (functionObj, functionName) => {
if (!functionObj.events) {
that.functions[functionName].events = [];
}

if (!functionObj.name) {
that.functions[functionName].name =
`${that.service}-${stageNameForFunction}-${functionName}`;
}
});
}

validate() {
_.forEach(this.functions, (functionObj, functionName) => {
if (!_.isArray(functionObj.events)) {
Expand Down
95 changes: 54 additions & 41 deletions lib/classes/Service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,47 +199,6 @@ describe('Service', () => {
});
});

it('should make sure function name contains the default stage', () => {
const SUtils = new Utils();
const serverlessYml = {
service: 'new-service',
provider: {
name: 'aws',
stage: 'dev',
region: 'us-east-1',
variableSyntax: '\\${{([\\s\\S]+?)}}',
},
plugins: ['testPlugin'],
functions: {
functionA: {},
},
resources: {
aws: {
resourcesProp: 'value',
},
azure: {},
google: {},
},
package: {
exclude: ['exclude-me'],
include: ['include-me'],
artifact: 'some/path/foo.zip',
},
};

SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
YAML.dump(serverlessYml));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

return serviceInstance.load().then(() => {
expect(serviceInstance.functions.functionA.name).to.be.equal('new-service-dev-functionA');
});
});

it('should support Serverless file with a non-aws provider', () => {
const SUtils = new Utils();
const serverlessYaml = {
Expand All @@ -259,6 +218,7 @@ describe('Service', () => {
serviceInstance = new Service(serverless);

return serviceInstance.load().then(() => {
serviceInstance.setFunctionNames();
const expectedFunc = {
functionA: {
name: 'customFunctionName',
Expand Down Expand Up @@ -290,6 +250,7 @@ describe('Service', () => {
serviceInstance = new Service(serverless);

return serviceInstance.load().then(() => {
serviceInstance.setFunctionNames();
const expectedFunc = {
functionA: {
name: 'customFunctionName',
Expand Down Expand Up @@ -319,6 +280,7 @@ describe('Service', () => {
serviceInstance = new Service(serverless);

return serviceInstance.load({ stage: 'dev' }).then(() => {
serviceInstance.setFunctionNames();
const expectedFunc = {
functionA: {
name: 'my-service-dev-functionA',
Expand Down Expand Up @@ -503,6 +465,57 @@ describe('Service', () => {
});
});

describe('#setFunctionNames()', () => {
let serviceInstance;
let tmpDirPath;

beforeEach(() => {
tmpDirPath = testUtils.getTmpDirPath();
});

it('should make sure function name contains the default stage', () => {
const SUtils = new Utils();
const serverlessYml = {
service: 'new-service',
provider: {
name: 'aws',
stage: 'dev',
region: 'us-east-1',
variableSyntax: '\\${{([\\s\\S]+?)}}',
},
plugins: ['testPlugin'],
functions: {
functionA: {},
},
resources: {
aws: {
resourcesProp: 'value',
},
azure: {},
google: {},
},
package: {
exclude: ['exclude-me'],
include: ['include-me'],
artifact: 'some/path/foo.zip',
},
};

SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
YAML.dump(serverlessYml));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

return serviceInstance.load().then(() => {
serviceInstance.setFunctionNames();
expect(serviceInstance.functions.functionA.name).to.be.equal('new-service-dev-functionA');
});
});
});

describe('#update()', () => {
it('should update service instance data', () => {
const serverless = new Serverless();
Expand Down