Skip to content

Commit

Permalink
Fix incorrect type from YAML parsing (#305)
Browse files Browse the repository at this point in the history
Closes
#302
  • Loading branch information
sethvargo authored Mar 10, 2023
1 parent cb8bcab commit 32a5295
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

17 changes: 6 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export async function run(): Promise<void> {
);

// Modify app.yaml if envvars were given.
if (envVars || buildEnvVars) {
if (
(envVars && Object.keys(envVars).length > 0) ||
(buildEnvVars && Object.keys(buildEnvVars).length > 0)
) {
logDebug(`Updating env_variables or build_env_variables`);

originalAppYamlPath = findAppYaml(deliverables);
Expand Down Expand Up @@ -300,16 +303,8 @@ export function findAppYaml(list: string[]): string {
* @param existing The existing KEY=VALUE pairs to parse.
* @param envVars The input environment variables.
*/
export function updateEnvVars(existing: string[], envVars: KVPair): KVPair {
let existingKV: KVPair = {};
if (existing) {
for (const str of existing) {
const line = parseKVString(str);
existingKV = Object.assign(existingKV, line);
}
}

return Object.assign({}, existingKV, envVars);
export function updateEnvVars(existing: KVPair, envVars: KVPair): KVPair {
return Object.assign({}, existing, envVars);
}

// Execute this as the entrypoint when requested.
Expand Down
41 changes: 36 additions & 5 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import 'mocha';
import { expect } from 'chai';
import * as sinon from 'sinon';

import YAML from 'yaml';

import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as setupGcloud from '@google-github-actions/setup-cloud-sdk';
Expand Down Expand Up @@ -248,19 +250,19 @@ describe('#updateEnvVars', () => {
const cases: {
only?: boolean;
name: string;
existing: string[];
existing: KVPair;
envVars: KVPair;
expected: KVPair;
}[] = [
{
name: 'empty existing, empty input',
existing: [],
existing: {},
envVars: {},
expected: {},
},
{
name: 'empty existing, given input',
existing: [],
existing: {},
envVars: {
FOO: 'bar',
ZIP: 'zap',
Expand All @@ -272,7 +274,9 @@ describe('#updateEnvVars', () => {
},
{
name: 'existing, given input',
existing: ['EXISTING=one'],
existing: {
EXISTING: 'one',
},
envVars: {
FOO: 'bar',
ZIP: 'zap',
Expand All @@ -285,7 +289,9 @@ describe('#updateEnvVars', () => {
},
{
name: 'overwrites',
existing: ['FOO=bar'],
existing: {
FOO: 'bar',
},
envVars: {
FOO: 'zip',
},
Expand All @@ -301,7 +307,32 @@ describe('#updateEnvVars', () => {
expect(updateEnvVars(tc.existing, tc.envVars)).to.eql(tc.expected);
});
});

it('handles an yaml with variables', async () => {
const parsed = YAML.parse(`
env_variables:
FOO: 'bar'
`);

console.log(typeof parsed.env_variables);
const result = updateEnvVars(parsed.env_variables, { ZIP: 'zap' });
expect(result).to.eql({
FOO: 'bar',
ZIP: 'zap',
});
});

it('handles an yaml without variables', async () => {
const parsed = YAML.parse(`{}`);

console.log(typeof parsed.env_variables);
const result = updateEnvVars(parsed.env_variables, { ZIP: 'zap' });
expect(result).to.eql({
ZIP: 'zap',
});
});
});

async function expectError(fn: () => Promise<void>, want: string) {
try {
await fn();
Expand Down

0 comments on commit 32a5295

Please sign in to comment.