diff --git a/lib/classes/Variables.js b/lib/classes/Variables.js index c6276cd71..590b8edef 100644 --- a/lib/classes/Variables.js +++ b/lib/classes/Variables.js @@ -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; }); diff --git a/lib/classes/Variables.test.js b/lib/classes/Variables.test.js index 5478eb4ba..5bbc18857 100644 --- a/lib/classes/Variables.test.js +++ b/lib/classes/Variables.test.js @@ -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'))