Skip to content

Commit

Permalink
feat: prevent overwriting existing env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
allenevans committed Sep 22, 2019
1 parent 65b53f8 commit fcdf704
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ branding:
runs:
using: 'node12'
main: 'dist/set-env.js'
inputs:
overwrite:
description: 'Overwrite existing environment variables'
default: false
required: false
55 changes: 47 additions & 8 deletions src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core';

jest.mock('@actions/core', () => ({
exportVariable: jest.fn(),
getInput: jest.fn(),
setFailed: jest.fn(),
}));

Expand Down Expand Up @@ -36,19 +37,57 @@ describe('set-env', () => {
expect(core.exportVariable).toHaveBeenCalledWith('C', '3');
});

it('should report exceptions', () => {
(<jest.Mock>core.exportVariable).mockImplementation(() => {
throw new Error('FAIL');
});
it('should allow environment variables to be overwritten', () => {
jest.spyOn(process, 'exit').mockImplementation((code?: number): never => {
throw new Error(`${code}`);
});

process.env.INPUT_TEST = 'test';
process.env.PROTECTED = 'do-not-change';
process.env.INPUT_PROTECTED = 'overwrite';

(<jest.Mock>core.getInput).mockImplementation(() => 'true');

jest.isolateModules(() => expect(() => require('./main')).not.toThrowError());

expect(core.exportVariable).toHaveBeenCalledWith('PROTECTED', 'overwrite');
});

describe('exceptions', () => {
beforeEach(() => {
jest.spyOn(process, 'exit').mockImplementation((code?: number): never => {
throw new Error(`${code}`);
});
});
it('should report exceptions', () => {
(<jest.Mock>core.exportVariable).mockImplementation(() => {
throw new Error('FAIL');
});
jest.spyOn(process, 'exit').mockImplementation((code?: number): never => {
throw new Error(`${code}`);
});

jest.isolateModules(() => expect(() => require('./main')).toThrowError('1'));
process.env.INPUT_TEST = 'test';

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenCalledWith('FAIL');
jest.isolateModules(() => expect(() => require('./main')).toThrowError());

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenCalledWith('FAIL');
});

it('should not allow existing environment variables to be overwritten', () => {
jest.spyOn(process, 'exit').mockImplementation((code?: number): never => {
throw new Error(`${code}`);
});

process.env.PROTECTED = 'do-not-change';
process.env.INPUT_PROTECTED = 'overwrite';

(<jest.Mock>core.getInput).mockImplementation(() => 'false');

jest.isolateModules(() => expect(() => require('./main')).toThrowError('1'));

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenCalledWith('Unable to overwrite existing environment variable PROTECTED');
});
});
});
15 changes: 13 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import * as core from '@actions/core';

const PROTECTED_INPUT_VARS = ['INPUT_OVERWRITE'];

const allowOverwrite = () => core.getInput('overwrite') === 'true';

const setEnvironmentVariable = (key: string, value: string) => {
if (process.env[key] !== undefined && !allowOverwrite()) {
throw new Error(`Unable to overwrite existing environment variable ${key}`);
}
core.exportVariable(key, value);
};

(function run() {
try {
Object.keys(process.env)
.filter((key) => /^INPUT_/.test(key))
.filter((key) => /^INPUT_/.test(key) && !PROTECTED_INPUT_VARS.includes(key))
.forEach((key) => {
core.exportVariable(key.replace(/^INPUT_/, ''), `${process.env[key]}`);
setEnvironmentVariable(key.replace(/^INPUT_/, ''), `${process.env[key]}`);
});
} catch (error) {
core.setFailed(error.message);
Expand Down

0 comments on commit fcdf704

Please sign in to comment.