mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Merge pull request #2511 from horike37/fixed-issue-2500
fixed issue 2500
This commit is contained in:
commit
795dad3ecc
@ -75,18 +75,22 @@ class Variables {
|
||||
|
||||
populateVariable(propertyParam, matchedString, valueToPopulate) {
|
||||
let property = propertyParam;
|
||||
|
||||
if (typeof valueToPopulate === 'string') {
|
||||
property = replaceall(matchedString, valueToPopulate, property);
|
||||
} else {
|
||||
if (property !== matchedString) {
|
||||
const errorMessage = [
|
||||
'Trying to populate non string value into',
|
||||
` a string for variable ${matchedString}.`,
|
||||
' Please make sure the value of the property is a string.',
|
||||
].join('');
|
||||
throw new this.serverless.classes
|
||||
.Error(errorMessage);
|
||||
if (typeof valueToPopulate === 'number') {
|
||||
property = replaceall(matchedString, String(valueToPopulate), property);
|
||||
} else {
|
||||
const errorMessage = [
|
||||
'Trying to populate non string value into',
|
||||
` a string for variable ${matchedString}.`,
|
||||
' Please make sure the value of the property is a string.',
|
||||
].join('');
|
||||
throw new this.serverless.classes
|
||||
.Error(errorMessage);
|
||||
}
|
||||
return property;
|
||||
}
|
||||
property = valueToPopulate;
|
||||
}
|
||||
|
||||
@ -155,6 +155,17 @@ describe('Variables', () => {
|
||||
expect(newProperty).to.equal('my stage is dev');
|
||||
});
|
||||
|
||||
it('should populate number variables as sub string', () => {
|
||||
const serverless = new Serverless();
|
||||
const valueToPopulate = 5;
|
||||
const matchedString = '${opt:number}';
|
||||
const property = 'your account number is ${opt:number}';
|
||||
|
||||
const newProperty = serverless.variables
|
||||
.populateVariable(property, matchedString, valueToPopulate);
|
||||
expect(newProperty).to.equal('your account number is 5');
|
||||
});
|
||||
|
||||
it('should populate non string variables', () => {
|
||||
const serverless = new Serverless();
|
||||
const valueToPopulate = 5;
|
||||
@ -166,11 +177,11 @@ describe('Variables', () => {
|
||||
expect(newProperty).to.equal(5);
|
||||
});
|
||||
|
||||
it('should throw error if populating non string variable as sub string', () => {
|
||||
it('should throw error if populating non string or non number variable as sub string', () => {
|
||||
const serverless = new Serverless();
|
||||
const valueToPopulate = 5;
|
||||
const matchedString = '${opt:number}';
|
||||
const property = 'hello ${opt:number}';
|
||||
const valueToPopulate = {};
|
||||
const matchedString = '${opt:object}';
|
||||
const property = 'your account number is ${opt:object}';
|
||||
expect(() => serverless.variables
|
||||
.populateVariable(property, matchedString, valueToPopulate))
|
||||
.to.throw(Error);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user