Populate objects if default syntax targets an object

This commit is contained in:
Frank Schmid 2017-12-04 18:08:39 +01:00
parent b7b31535f6
commit 37340467ed
No known key found for this signature in database
GPG Key ID: D6D684E158939B0C
2 changed files with 43 additions and 48 deletions

View File

@ -97,13 +97,7 @@ class Variables {
if (variableString.match(this.overwriteSyntax)) {
singleValueToPopulate = this.overwrite(variableString);
} else {
singleValueToPopulate = this.getValueFromSource(variableString)
.then(valueToPopulate => {
if (typeof valueToPopulate === 'object') {
return this.populateObject(valueToPopulate);
}
return valueToPopulate;
});
singleValueToPopulate = this.getValueFromSource(variableString);
}
singleValueToPopulate = singleValueToPopulate.then(valueToPopulate => {
@ -192,7 +186,13 @@ class Variables {
}
if (valueFromSource) {
const resolver = BbPromise.resolve(valueFromSource)
.then((result) => {
.then(result => {
if (_.isObject(result)) {
return this.populateObject(result);
}
return result;
})
.then(result => {
this.cache[variableString] = result;
return result;
});

View File

@ -219,46 +219,6 @@ describe('Variables', () => {
});
});
it('should call populateObject if variable value is an object', () => {
const serverless = new Serverless();
serverless.variables.options = {
stage: 'prod',
};
const property = '${opt:stage}';
const variableValue = {
stage: '${opt:stage}',
};
const variableValuePopulated = {
stage: 'prod',
};
serverless.variables.loadVariableSyntax();
const populateObjectStub = sinon
.stub(serverless.variables, 'populateObject')
.resolves(variableValuePopulated);
const getValueFromSourceStub = sinon
.stub(serverless.variables, 'getValueFromSource')
.resolves(variableValue);
const populateVariableStub = sinon
.stub(serverless.variables, 'populateVariable')
.resolves(variableValuePopulated);
return serverless.variables.populateProperty(property).then(newProperty => {
expect(populateObjectStub.called).to.equal(true);
expect(getValueFromSourceStub.called).to.equal(true);
expect(populateVariableStub.called).to.equal(true);
expect(newProperty).to.deep.equal(variableValuePopulated);
return BbPromise.resolve();
})
.finally(() => {
serverless.variables.populateObject.restore();
serverless.variables.getValueFromSource.restore();
serverless.variables.populateVariable.restore();
});
});
it('should run recursively if nested variables provided', () => {
const serverless = new Serverless();
const property = 'my stage is ${env:${opt.name}}';
@ -541,6 +501,41 @@ describe('Variables', () => {
});
});
it('should call populateObject if variable value is an object', () => {
const serverless = new Serverless();
serverless.variables.options = {
stage: 'prod',
};
const property = 'self:stage';
const variableValue = {
stage: '${opt:stage}',
};
const variableValuePopulated = {
stage: 'prod',
};
serverless.variables.loadVariableSyntax();
const populateObjectStub = sinon
.stub(serverless.variables, 'populateObject')
.resolves(variableValuePopulated);
const getValueFromSelfStub = sinon
.stub(serverless.variables, 'getValueFromSelf')
.resolves(variableValue);
return serverless.variables.getValueFromSource(property).then(newProperty => {
expect(populateObjectStub).to.have.been.calledOnce;
expect(getValueFromSelfStub).to.have.been.calledOnce;
expect(newProperty).to.deep.equal(variableValuePopulated);
return BbPromise.resolve();
})
.finally(() => {
serverless.variables.populateObject.restore();
serverless.variables.getValueFromSelf.restore();
});
});
it('should throw error if referencing an invalid source', () => {
const serverless = new Serverless();
expect(() => serverless.variables.getValueFromSource('weird:source'))