Skip to content

Commit 32a5295

Browse files
authored
Fix incorrect type from YAML parsing (#305)
Closes #302
1 parent cb8bcab commit 32a5295

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ export async function run(): Promise<void> {
122122
);
123123

124124
// Modify app.yaml if envvars were given.
125-
if (envVars || buildEnvVars) {
125+
if (
126+
(envVars && Object.keys(envVars).length > 0) ||
127+
(buildEnvVars && Object.keys(buildEnvVars).length > 0)
128+
) {
126129
logDebug(`Updating env_variables or build_env_variables`);
127130

128131
originalAppYamlPath = findAppYaml(deliverables);
@@ -300,16 +303,8 @@ export function findAppYaml(list: string[]): string {
300303
* @param existing The existing KEY=VALUE pairs to parse.
301304
* @param envVars The input environment variables.
302305
*/
303-
export function updateEnvVars(existing: string[], envVars: KVPair): KVPair {
304-
let existingKV: KVPair = {};
305-
if (existing) {
306-
for (const str of existing) {
307-
const line = parseKVString(str);
308-
existingKV = Object.assign(existingKV, line);
309-
}
310-
}
311-
312-
return Object.assign({}, existingKV, envVars);
306+
export function updateEnvVars(existing: KVPair, envVars: KVPair): KVPair {
307+
return Object.assign({}, existing, envVars);
313308
}
314309

315310
// Execute this as the entrypoint when requested.

tests/main.test.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import 'mocha';
1818
import { expect } from 'chai';
1919
import * as sinon from 'sinon';
2020

21+
import YAML from 'yaml';
22+
2123
import * as core from '@actions/core';
2224
import * as exec from '@actions/exec';
2325
import * as setupGcloud from '@google-github-actions/setup-cloud-sdk';
@@ -248,19 +250,19 @@ describe('#updateEnvVars', () => {
248250
const cases: {
249251
only?: boolean;
250252
name: string;
251-
existing: string[];
253+
existing: KVPair;
252254
envVars: KVPair;
253255
expected: KVPair;
254256
}[] = [
255257
{
256258
name: 'empty existing, empty input',
257-
existing: [],
259+
existing: {},
258260
envVars: {},
259261
expected: {},
260262
},
261263
{
262264
name: 'empty existing, given input',
263-
existing: [],
265+
existing: {},
264266
envVars: {
265267
FOO: 'bar',
266268
ZIP: 'zap',
@@ -272,7 +274,9 @@ describe('#updateEnvVars', () => {
272274
},
273275
{
274276
name: 'existing, given input',
275-
existing: ['EXISTING=one'],
277+
existing: {
278+
EXISTING: 'one',
279+
},
276280
envVars: {
277281
FOO: 'bar',
278282
ZIP: 'zap',
@@ -285,7 +289,9 @@ describe('#updateEnvVars', () => {
285289
},
286290
{
287291
name: 'overwrites',
288-
existing: ['FOO=bar'],
292+
existing: {
293+
FOO: 'bar',
294+
},
289295
envVars: {
290296
FOO: 'zip',
291297
},
@@ -301,7 +307,32 @@ describe('#updateEnvVars', () => {
301307
expect(updateEnvVars(tc.existing, tc.envVars)).to.eql(tc.expected);
302308
});
303309
});
310+
311+
it('handles an yaml with variables', async () => {
312+
const parsed = YAML.parse(`
313+
env_variables:
314+
FOO: 'bar'
315+
`);
316+
317+
console.log(typeof parsed.env_variables);
318+
const result = updateEnvVars(parsed.env_variables, { ZIP: 'zap' });
319+
expect(result).to.eql({
320+
FOO: 'bar',
321+
ZIP: 'zap',
322+
});
323+
});
324+
325+
it('handles an yaml without variables', async () => {
326+
const parsed = YAML.parse(`{}`);
327+
328+
console.log(typeof parsed.env_variables);
329+
const result = updateEnvVars(parsed.env_variables, { ZIP: 'zap' });
330+
expect(result).to.eql({
331+
ZIP: 'zap',
332+
});
333+
});
304334
});
335+
305336
async function expectError(fn: () => Promise<void>, want: string) {
306337
try {
307338
await fn();

0 commit comments

Comments
 (0)