Skip to content

Commit

Permalink
feat: Merge EnvVars and EnVarsFile (#134) (#139)
Browse files Browse the repository at this point in the history
* Fix issue#57 by merging envVars and envVarsFile together

* Added to explain the logic for `envVars` and `envVarsFile`

Co-authored-by: Averi Kitsch <akitsch@google.com>

Co-authored-by: Piolin Victor <victor.piolin@gmail.com>
Co-authored-by: Averi Kitsch <akitsch@google.com>
  • Loading branch information
3 people committed Jul 30, 2021
1 parent 0cd64e2 commit afe2cad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
31 changes: 17 additions & 14 deletions src/cloudFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { cloudfunctions_v1 } from 'googleapis';
import fs from 'fs';
import YAML from 'yaml';
import { env } from 'process';

export type KVPair = {
[key: string]: string;
Expand Down Expand Up @@ -117,21 +118,23 @@ export class CloudFunction {
? opts.availableMemoryMb
: null;

// Only one of envVars and envVarsFile should be set
if (opts?.envVars && opts?.envVarsFile) {
throw new Error(
'Only one of env_vars or env_vars_file can be specified.',
);
}
// Check if `envVars` or `envVarsFile` are set.
// If two var keys are the same between `envVars` and `envVarsFile`
// `envVars` will override the one on `envVarsFile`
if (opts?.envVars || opts?.envVarsFile) {
let envVars;

if (opts?.envVarsFile) {
envVars = this.parseEnvVarsFile(opts.envVarsFile);
}

if (opts?.envVars) {
envVars = {
...envVars,
...this.parseKVPairs(opts.envVars),
};
}

// Parse env vars
let envVars;
if (opts?.envVars) {
envVars = this.parseKVPairs(opts.envVars);
request.environmentVariables = envVars;
}
if (opts?.envVarsFile) {
envVars = this.parseEnvVarsFile(opts.envVarsFile);
request.environmentVariables = envVars;
}

Expand Down
36 changes: 31 additions & 5 deletions tests/cloudFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,38 @@ describe('CloudFunction', function () {
);
});

it('throws an error with both envVarsFile and envVars specified', function () {
it('Merge envVars and envVarsFile if both specified', function () {
const envVarsFile = 'tests/env-var-files/test.good.yaml';
const envVars = 'KEY1=VALUE1,KEY2=VALUE2';
expect(function () {
new CloudFunction({ name, runtime, parent, envVarsFile, envVars });
}).to.throw('Only one of env_vars or env_vars_file can be specified.');
const envVars = 'KEY3=VALUE3,KEY4=VALUE4';
const cf = new CloudFunction({
name,
runtime,
parent,
envVarsFile,
envVars,
});
expect(cf.request.name).equal(`${parent}/functions/${name}`);
expect(cf.request.environmentVariables?.KEY1).equal('VALUE1');
expect(cf.request.environmentVariables?.KEY2).equal('VALUE2');
expect(cf.request.environmentVariables?.JSONKEY).equal('{"bar":"baz"}');
expect(cf.request.environmentVariables?.KEY3).equal('VALUE3');
expect(cf.request.environmentVariables?.KEY4).equal('VALUE4');
});

it('Merge envVars and envVarsFile if both specified, with same key name. envVars will erase the value in envVarsFile', function () {
const envVarsFile = 'tests/env-var-files/test.good.yaml';
const envVars = 'KEY1=NEWVALUE1,KEY2=NEWVALUE2';
const cf = new CloudFunction({
name,
runtime,
parent,
envVarsFile,
envVars,
});
expect(cf.request.name).equal(`${parent}/functions/${name}`);
expect(cf.request.environmentVariables?.KEY1).equal('NEWVALUE1');
expect(cf.request.environmentVariables?.KEY2).equal('NEWVALUE2');
expect(cf.request.environmentVariables?.JSONKEY).equal('{"bar":"baz"}');
});

it('creates an event function', function () {
Expand Down

0 comments on commit afe2cad

Please sign in to comment.