Merge pull request #2757 from serverless/local-env-vars

add support for lambda env vars in invoke local
This commit is contained in:
Nik Graf 2016-11-21 12:35:00 +01:00 committed by GitHub
commit 63d13a4453
2 changed files with 54 additions and 1 deletions

View File

@ -1,6 +1,7 @@
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');
const validate = require('../lib/validate');
@ -15,6 +16,7 @@ class AwsInvokeLocal {
this.hooks = {
'invoke:local:invoke': () => BbPromise.bind(this)
.then(this.extendedValidate)
.then(this.loadEnvVars)
.then(this.invokeLocal),
};
}
@ -38,6 +40,15 @@ class AwsInvokeLocal {
return BbPromise.resolve();
}
loadEnvVars() {
const providerEnvVars = this.serverless.service.provider.environment || {};
const functionEnvVars = this.options.functionObj.environment || {};
_.merge(process.env, providerEnvVars, functionEnvVars);
return BbPromise.resolve();
}
invokeLocal() {
const runtime = this.options.functionObj.runtime
|| this.serverless.service.provider.runtime

View File

@ -28,15 +28,19 @@ describe('AwsInvokeLocal', () => {
it('should run promise chain in order', () => {
const validateStub = sinon
.stub(awsInvokeLocal, 'extendedValidate').returns(BbPromise.resolve());
const loadEnvVarsStub = sinon
.stub(awsInvokeLocal, 'loadEnvVars').returns(BbPromise.resolve());
const invokeLocalStub = sinon
.stub(awsInvokeLocal, 'invokeLocal').returns(BbPromise.resolve());
return awsInvokeLocal.hooks['invoke:local:invoke']().then(() => {
expect(validateStub.calledOnce).to.be.equal(true);
expect(invokeLocalStub.calledAfter(validateStub)).to.be.equal(true);
expect(loadEnvVarsStub.calledAfter(validateStub)).to.be.equal(true);
expect(invokeLocalStub.calledAfter(loadEnvVarsStub)).to.be.equal(true);
awsInvokeLocal.extendedValidate.restore();
awsInvokeLocal.loadEnvVars.restore();
awsInvokeLocal.invokeLocal.restore();
});
});
@ -150,6 +154,44 @@ describe('AwsInvokeLocal', () => {
});
});
describe('#loadEnvVars()', () => {
beforeEach(() => {
serverless.config.servicePath = true;
serverless.service.provider = {
environment: {
providerVar: 'providerValue',
},
};
awsInvokeLocal.options = {
functionObj: {
environment: {
functionVar: 'functionValue',
},
},
};
});
it('it should load provider env vars', () => awsInvokeLocal
.loadEnvVars().then(() => {
expect(process.env.providerVar).to.be.equal('providerValue');
})
);
it('it should load function env vars', () => awsInvokeLocal
.loadEnvVars().then(() => {
expect(process.env.functionVar).to.be.equal('functionValue');
})
);
it('it should overwrite provider env vars', () => {
awsInvokeLocal.options.functionObj.environment.providerVar = 'providerValueOverwritten';
return awsInvokeLocal.loadEnvVars().then(() => {
expect(process.env.providerVar).to.be.equal('providerValueOverwritten');
});
});
});
describe('#invokeLocal()', () => {
const invokeLocalNodeJsStub = sinon
.stub(awsInvokeLocal, 'invokeLocalNodeJs').returns(BbPromise.resolve());