From 9d5d690489c47332cc3197a6c3934797c6693027 Mon Sep 17 00:00:00 2001 From: Lucas Rudd Date: Fri, 20 Apr 2018 08:50:41 -0500 Subject: [PATCH 1/2] Populating object before parsing step function --- lib/yamlParser.js | 41 ++++++++++++++++++++++++----------------- lib/yamlParser.test.js | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/lib/yamlParser.js b/lib/yamlParser.js index 971f136c..8b78ffe3 100644 --- a/lib/yamlParser.js +++ b/lib/yamlParser.js @@ -14,30 +14,37 @@ module.exports = { return this.serverless.yamlParser .parse(serverlessYmlPath) .then((serverlessFileParam) => { - this.serverless.service.stepFunctions = {}; - this.serverless.service.stepFunctions.stateMachines - = serverlessFileParam.stepFunctions - && serverlessFileParam.stepFunctions.stateMachines - ? serverlessFileParam.stepFunctions.stateMachines : {}; - this.serverless.service.stepFunctions.activities - = serverlessFileParam.stepFunctions - && serverlessFileParam.stepFunctions.activities - ? serverlessFileParam.stepFunctions.activities : []; + this.serverless.variables.populateObject(serverlessFileParam).then((parsedObject) => { + this.serverless.service.stepFunctions = {}; + try { + this.serverless.service.stepFunctions.stateMachines + = parsedObject.stepFunctions + && parsedObject.stepFunctions.stateMachines + ? parsedObject.stepFunctions.stateMachines : {}; + this.serverless.service.stepFunctions.activities + = parsedObject.stepFunctions + && parsedObject.stepFunctions.activities + ? parsedObject.stepFunctions.activities : []; + } catch (e) { + throw new this.serverless.classes + .Error('stateMachine or activity not found in this Service'); + } - if (!this.serverless.pluginManager.cliOptions.stage) { - this.serverless.pluginManager.cliOptions.stage = this.options.stage + if (!this.serverless.pluginManager.cliOptions.stage) { + this.serverless.pluginManager.cliOptions.stage = this.options.stage || (this.serverless.service.provider && this.serverless.service.provider.stage) || 'dev'; - } + } - if (!this.serverless.pluginManager.cliOptions.region) { - this.serverless.pluginManager.cliOptions.region = this.options.region + if (!this.serverless.pluginManager.cliOptions.region) { + this.serverless.pluginManager.cliOptions.region = this.options.region || (this.serverless.service.provider && this.serverless.service.provider.region) || 'us-east-1'; - } + } - this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions); - return BbPromise.resolve(); + this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions); + return BbPromise.resolve(); + }); }); }, diff --git a/lib/yamlParser.test.js b/lib/yamlParser.test.js index a32fa565..921f4963 100644 --- a/lib/yamlParser.test.js +++ b/lib/yamlParser.test.js @@ -22,9 +22,17 @@ describe('#yamlParse', () => { describe('#yamlParse()', () => { let yamlParserStub; let populateServiceStub; + let yamlObjectParserStub; beforeEach(() => { populateServiceStub = sinon.stub(serverlessStepFunctions.serverless.variables, 'populateService').returns(BbPromise.resolve()); + yamlObjectParserStub = sinon.stub(serverlessStepFunctions.serverless.variables, + 'populateObject').returns(BbPromise.resolve({ + stepFunctions: { + stateMachines: 'stepFunctions', + activities: 'my-activity', + }, + })); yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse') .returns(BbPromise.resolve({ stepFunctions: { @@ -35,6 +43,12 @@ describe('#yamlParse', () => { serverlessStepFunctions.serverless.config.servicePath = 'servicePath'; }); + afterEach(() => { + serverlessStepFunctions.serverless.yamlParser.parse.restore(); + serverlessStepFunctions.serverless.variables.populateService.restore(); + serverlessStepFunctions.serverless.variables.populateObject.restore(); + }); + it('should default to dev when stage and provider are not defined', () => { serverlessStepFunctions.serverless.pluginManager.cliOptions.stage = null; serverlessStepFunctions.serverless.service.provider = null; @@ -74,39 +88,49 @@ describe('#yamlParse', () => { serverlessStepFunctions.yamlParse() .then(() => { expect(yamlParserStub.calledOnce).to.be.equal(false); + expect(yamlObjectParserStub.calledOnce).to.be.equal(false); expect(populateServiceStub.calledOnce).to.be.equal(false); expect(serverless.service.stepFunctions).to.be.undefined; // eslint-disable-line - serverlessStepFunctions.serverless.yamlParser.parse.restore(); - serverlessStepFunctions.serverless.variables.populateService.restore(); }); }); it('should create corresponding when stepfunctions param are given', () => { + serverlessStepFunctions.serverless.variables.populateObject.restore(); + yamlObjectParserStub = sinon.stub(serverlessStepFunctions.serverless.variables, + 'populateObject').returns(BbPromise.resolve({ + stepFunctions: { + stateMachines: 'stepFunctions', + activities: 'my-activity', + }, + })); serverlessStepFunctions.yamlParse() .then(() => { expect(yamlParserStub.calledOnce).to.be.equal(true); + expect(yamlObjectParserStub.calledOnce).to.be.equal(true); expect(populateServiceStub.calledOnce).to.be.equal(true); expect(serverless.service.stepFunctions.stateMachines).to.be.equal('stepFunctions'); expect(serverless.service.stepFunctions.activities).to.be.equal('my-activity'); - serverlessStepFunctions.serverless.yamlParser.parse.restore(); - serverlessStepFunctions.serverless.variables.populateService.restore(); }); }); it('should create empty object when stepfunctions param are not given', () => { serverlessStepFunctions.serverless.yamlParser.parse.restore(); + serverlessStepFunctions.serverless.variables.populateObject.restore(); yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse') .returns(BbPromise.resolve({ stepFunctions: {}, })); + yamlObjectParserStub = sinon.stub(serverless.variables, + 'populateObject').returns(BbPromise.resolve({ + stepFunctions: {}, + })); serverlessStepFunctions.yamlParse() .then(() => { expect(yamlParserStub.calledOnce).to.be.equal(true); + expect(yamlObjectParserStub.calledOnce).to.be.equal(true); expect(populateServiceStub.calledOnce).to.be.equal(true); expect(serverless.service.stepFunctions.stateMachines).to.be.deep.equal({}); expect(serverless.service.stepFunctions.activities).to.be.deep.equal([]); - serverlessStepFunctions.serverless.yamlParser.parse.restore(); - serverlessStepFunctions.serverless.variables.populateService.restore(); }); }); }); From 8343f8cecff326c08a09a855fba672a93755ec5a Mon Sep 17 00:00:00 2001 From: Lucas Rudd Date: Fri, 20 Apr 2018 09:45:28 -0500 Subject: [PATCH 2/2] removed try catch --- lib/yamlParser.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/yamlParser.js b/lib/yamlParser.js index 8b78ffe3..d1bb1212 100644 --- a/lib/yamlParser.js +++ b/lib/yamlParser.js @@ -16,19 +16,14 @@ module.exports = { .then((serverlessFileParam) => { this.serverless.variables.populateObject(serverlessFileParam).then((parsedObject) => { this.serverless.service.stepFunctions = {}; - try { - this.serverless.service.stepFunctions.stateMachines + this.serverless.service.stepFunctions.stateMachines = parsedObject.stepFunctions && parsedObject.stepFunctions.stateMachines ? parsedObject.stepFunctions.stateMachines : {}; - this.serverless.service.stepFunctions.activities + this.serverless.service.stepFunctions.activities = parsedObject.stepFunctions && parsedObject.stepFunctions.activities ? parsedObject.stepFunctions.activities : []; - } catch (e) { - throw new this.serverless.classes - .Error('stateMachine or activity not found in this Service'); - } if (!this.serverless.pluginManager.cliOptions.stage) { this.serverless.pluginManager.cliOptions.stage = this.options.stage