Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect type from YAML parsing #305

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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