Skip to content

Commit

Permalink
fix: improve env var parsing behaviour (#122) (#126)
Browse files Browse the repository at this point in the history
* fix(envVars): Handle multiple delimiters when parsing

Split a key/val pair only on the first equals character, preserving
any remaining equals characters in the key and value.

* test(envVars): Test env var string parsing with multiple delim chars

Co-authored-by: Rob Cowie <rob_cowie@mac.com>
  • Loading branch information
bharathkkb and robcowie committed Jul 21, 2021
1 parent 640c9df commit 199d134
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/cloudFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ export class CloudFunction {
`The expected data format should be "KEY1=VALUE1", got "${pair}" while parsing "${values}"`,
);
}
const keyValue = pair.split('=');
kvPairs[keyValue[0]] = keyValue[1];
// Split on the first delimiter only
const name = pair.substring(0, pair.indexOf('='));
const value = pair.substring(pair.indexOf('=') + 1);
kvPairs[name] = value;
});
return kvPairs;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/cloudFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ describe('CloudFunction', function () {
);
});

it('creates a http function with two envVars containing equals character', function () {
const envVars = 'KEY1=VALUE=1,KEY2=VALUE=2';
const cf = new CloudFunction({ name, runtime, parent, envVars });
expect(cf.request.name).equal(`${parent}/functions/${name}`);
expect(cf.request.runtime).equal(runtime);
expect(cf.request.httpsTrigger).not.to.be.null;
expect(cf.request.environmentVariables?.KEY1).equal('VALUE=1');
expect(cf.request.environmentVariables?.KEY2).equal('VALUE=2');
});

it('creates a http function with envVarsFile', function () {
const envVarsFile = 'tests/env-var-files/test.good.yaml';
const cf = new CloudFunction({ name, runtime, parent, envVarsFile });
Expand Down

0 comments on commit 199d134

Please sign in to comment.