Skip to content

Commit e44d8ef

Browse files
authored
Merge pull request #116 from lucas-rudd/PopulateObjectBeforeParsingStepFunction
Populating object before parsing step function
2 parents 8bb8fca + 8343f8c commit e44d8ef

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

lib/yamlParser.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,32 @@ module.exports = {
1414
return this.serverless.yamlParser
1515
.parse(serverlessYmlPath)
1616
.then((serverlessFileParam) => {
17-
this.serverless.service.stepFunctions = {};
18-
this.serverless.service.stepFunctions.stateMachines
19-
= serverlessFileParam.stepFunctions
20-
&& serverlessFileParam.stepFunctions.stateMachines
21-
? serverlessFileParam.stepFunctions.stateMachines : {};
22-
this.serverless.service.stepFunctions.activities
23-
= serverlessFileParam.stepFunctions
24-
&& serverlessFileParam.stepFunctions.activities
25-
? serverlessFileParam.stepFunctions.activities : [];
17+
this.serverless.variables.populateObject(serverlessFileParam).then((parsedObject) => {
18+
this.serverless.service.stepFunctions = {};
19+
this.serverless.service.stepFunctions.stateMachines
20+
= parsedObject.stepFunctions
21+
&& parsedObject.stepFunctions.stateMachines
22+
? parsedObject.stepFunctions.stateMachines : {};
23+
this.serverless.service.stepFunctions.activities
24+
= parsedObject.stepFunctions
25+
&& parsedObject.stepFunctions.activities
26+
? parsedObject.stepFunctions.activities : [];
2627

27-
if (!this.serverless.pluginManager.cliOptions.stage) {
28-
this.serverless.pluginManager.cliOptions.stage = this.options.stage
28+
if (!this.serverless.pluginManager.cliOptions.stage) {
29+
this.serverless.pluginManager.cliOptions.stage = this.options.stage
2930
|| (this.serverless.service.provider && this.serverless.service.provider.stage)
3031
|| 'dev';
31-
}
32+
}
3233

33-
if (!this.serverless.pluginManager.cliOptions.region) {
34-
this.serverless.pluginManager.cliOptions.region = this.options.region
34+
if (!this.serverless.pluginManager.cliOptions.region) {
35+
this.serverless.pluginManager.cliOptions.region = this.options.region
3536
|| (this.serverless.service.provider && this.serverless.service.provider.region)
3637
|| 'us-east-1';
37-
}
38+
}
3839

39-
this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
40-
return BbPromise.resolve();
40+
this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
41+
return BbPromise.resolve();
42+
});
4143
});
4244
},
4345

lib/yamlParser.test.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ describe('#yamlParse', () => {
2222
describe('#yamlParse()', () => {
2323
let yamlParserStub;
2424
let populateServiceStub;
25+
let yamlObjectParserStub;
2526
beforeEach(() => {
2627
populateServiceStub = sinon.stub(serverlessStepFunctions.serverless.variables,
2728
'populateService').returns(BbPromise.resolve());
29+
yamlObjectParserStub = sinon.stub(serverlessStepFunctions.serverless.variables,
30+
'populateObject').returns(BbPromise.resolve({
31+
stepFunctions: {
32+
stateMachines: 'stepFunctions',
33+
activities: 'my-activity',
34+
},
35+
}));
2836
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
2937
.returns(BbPromise.resolve({
3038
stepFunctions: {
@@ -35,6 +43,12 @@ describe('#yamlParse', () => {
3543
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
3644
});
3745

46+
afterEach(() => {
47+
serverlessStepFunctions.serverless.yamlParser.parse.restore();
48+
serverlessStepFunctions.serverless.variables.populateService.restore();
49+
serverlessStepFunctions.serverless.variables.populateObject.restore();
50+
});
51+
3852
it('should default to dev when stage and provider are not defined', () => {
3953
serverlessStepFunctions.serverless.pluginManager.cliOptions.stage = null;
4054
serverlessStepFunctions.serverless.service.provider = null;
@@ -74,39 +88,49 @@ describe('#yamlParse', () => {
7488
serverlessStepFunctions.yamlParse()
7589
.then(() => {
7690
expect(yamlParserStub.calledOnce).to.be.equal(false);
91+
expect(yamlObjectParserStub.calledOnce).to.be.equal(false);
7792
expect(populateServiceStub.calledOnce).to.be.equal(false);
7893
expect(serverless.service.stepFunctions).to.be.undefined; // eslint-disable-line
79-
serverlessStepFunctions.serverless.yamlParser.parse.restore();
80-
serverlessStepFunctions.serverless.variables.populateService.restore();
8194
});
8295
});
8396

8497
it('should create corresponding when stepfunctions param are given', () => {
98+
serverlessStepFunctions.serverless.variables.populateObject.restore();
99+
yamlObjectParserStub = sinon.stub(serverlessStepFunctions.serverless.variables,
100+
'populateObject').returns(BbPromise.resolve({
101+
stepFunctions: {
102+
stateMachines: 'stepFunctions',
103+
activities: 'my-activity',
104+
},
105+
}));
85106
serverlessStepFunctions.yamlParse()
86107
.then(() => {
87108
expect(yamlParserStub.calledOnce).to.be.equal(true);
109+
expect(yamlObjectParserStub.calledOnce).to.be.equal(true);
88110
expect(populateServiceStub.calledOnce).to.be.equal(true);
89111
expect(serverless.service.stepFunctions.stateMachines).to.be.equal('stepFunctions');
90112
expect(serverless.service.stepFunctions.activities).to.be.equal('my-activity');
91-
serverlessStepFunctions.serverless.yamlParser.parse.restore();
92-
serverlessStepFunctions.serverless.variables.populateService.restore();
93113
});
94114
});
95115

96116
it('should create empty object when stepfunctions param are not given', () => {
97117
serverlessStepFunctions.serverless.yamlParser.parse.restore();
118+
serverlessStepFunctions.serverless.variables.populateObject.restore();
98119
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
99120
.returns(BbPromise.resolve({
100121
stepFunctions: {},
101122
}));
123+
yamlObjectParserStub = sinon.stub(serverless.variables,
124+
'populateObject').returns(BbPromise.resolve({
125+
stepFunctions: {},
126+
}));
102127
serverlessStepFunctions.yamlParse()
103128
.then(() => {
104129
expect(yamlParserStub.calledOnce).to.be.equal(true);
130+
expect(yamlObjectParserStub.calledOnce).to.be.equal(true);
105131
expect(populateServiceStub.calledOnce).to.be.equal(true);
106132
expect(serverless.service.stepFunctions.stateMachines).to.be.deep.equal({});
107133
expect(serverless.service.stepFunctions.activities).to.be.deep.equal([]);
108-
serverlessStepFunctions.serverless.yamlParser.parse.restore();
109-
serverlessStepFunctions.serverless.variables.populateService.restore();
110134
});
111135
});
112136
});

0 commit comments

Comments
 (0)