Fix to make utils.readEnv more resilient to user input

This commit is contained in:
Shivam Mathur 2021-07-23 16:19:00 +05:30
parent 7bd9f10bef
commit 4bb4f1812c
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
3 changed files with 22 additions and 14 deletions

View File

@ -19,7 +19,11 @@ async function cleanup(path: string): Promise<void> {
describe('Utils tests', () => {
it('checking readEnv', async () => {
process.env['test'] = 'setup-php';
process.env['test-hyphen'] = 'setup-php';
expect(await utils.readEnv('test')).toBe('setup-php');
expect(await utils.readEnv('TEST')).toBe('setup-php');
expect(await utils.readEnv('test_hyphen')).toBe('setup-php');
expect(await utils.readEnv('TEST_HYPHEN')).toBe('setup-php');
expect(await utils.readEnv('undefined')).toBe('');
});

15
dist/index.js vendored
View File

@ -877,13 +877,14 @@ const path = __importStar(__nccwpck_require__(622));
const url = __importStar(__nccwpck_require__(835));
const core = __importStar(__nccwpck_require__(186));
async function readEnv(property) {
const value = process.env[property];
switch (value) {
case undefined:
return '';
default:
return value;
}
const property_lc = property.toLowerCase();
const property_uc = property.toUpperCase();
return (process.env[property] ||
process.env[property_lc] ||
process.env[property_uc] ||
process.env[property_lc.replace('_', '-')] ||
process.env[property_uc.replace('_', '-')] ||
'');
}
exports.readEnv = readEnv;
async function getInput(name, mandatory) {

View File

@ -11,13 +11,16 @@ import * as core from '@actions/core';
* @param property
*/
export async function readEnv(property: string): Promise<string> {
const value = process.env[property];
switch (value) {
case undefined:
return '';
default:
return value;
}
const property_lc: string = property.toLowerCase();
const property_uc: string = property.toUpperCase();
return (
process.env[property] ||
process.env[property_lc] ||
process.env[property_uc] ||
process.env[property_lc.replace('_', '-')] ||
process.env[property_uc.replace('_', '-')] ||
''
);
}
/**