mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
fix #2997
This commit is contained in:
parent
53c96088a2
commit
7bbf9ce6d0
@ -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();
|
||||
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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 = {
|
||||
@ -259,6 +218,7 @@ describe('Service', () => {
|
||||
serviceInstance = new Service(serverless);
|
||||
|
||||
return serviceInstance.load().then(() => {
|
||||
serviceInstance.setFunctionNames();
|
||||
const expectedFunc = {
|
||||
functionA: {
|
||||
name: 'customFunctionName',
|
||||
@ -290,6 +250,7 @@ describe('Service', () => {
|
||||
serviceInstance = new Service(serverless);
|
||||
|
||||
return serviceInstance.load().then(() => {
|
||||
serviceInstance.setFunctionNames();
|
||||
const expectedFunc = {
|
||||
functionA: {
|
||||
name: 'customFunctionName',
|
||||
@ -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',
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user