diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index e0f796e3..504ac12e 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -17,13 +17,20 @@ async function cleanup(path: string): Promise { } describe('Utils tests', () => { + it('checking readEnv', async () => { + process.env['test'] = 'setup-php'; + expect(await utils.readEnv('test')).toBe('setup-php'); + expect(await utils.readEnv('undefined')).toBe(''); + }); + it('checking getInput', async () => { process.env['test'] = 'setup-php'; - process.env['undefined'] = ''; expect(await utils.getInput('test', false)).toBe('setup-php'); - expect(await utils.getInput('undefined', false)).toBe(''); expect(await utils.getInput('setup-php', false)).toBe('setup-php'); expect(await utils.getInput('DoesNotExist', false)).toBe(''); + expect(async () => { + await utils.getInput('DoesNotExist', true); + }).rejects.toThrow('Input required and not supplied: DoesNotExist'); }); it('checking parseVersion', async () => { diff --git a/dist/index.js b/dist/index.js index 0a720549..8fdfceb7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1097,10 +1097,25 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.customPackage = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseVersion = exports.getInput = void 0; +exports.customPackage = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseVersion = exports.getInput = exports.readEnv = void 0; const fs = __importStar(__webpack_require__(747)); const path = __importStar(__webpack_require__(622)); const core = __importStar(__webpack_require__(470)); +/** + * Function to read environment variable and return a string value. + * + * @param property + */ +async function readEnv(property) { + const value = process.env[property]; + switch (value) { + case undefined: + return ''; + default: + return value; + } +} +exports.readEnv = readEnv; /** * Function to get inputs from both with and env annotations. * @@ -1108,13 +1123,17 @@ const core = __importStar(__webpack_require__(470)); * @param mandatory */ async function getInput(name, mandatory) { - const input = process.env[name]; - switch (input) { - case '': - case undefined: - return core.getInput(name, { required: mandatory }); - default: + const input = core.getInput(name); + const env_input = await readEnv(name); + switch (true) { + case input != '': return input; + case input == '' && env_input != '': + return env_input; + case input == '' && env_input == '' && mandatory: + throw new Error(`Input required and not supplied: ${name}`); + default: + return ''; } } exports.getInput = getInput; diff --git a/src/utils.ts b/src/utils.ts index c54e8e58..1f1460db 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,21 @@ import * as fs from 'fs'; import * as path from 'path'; import * as core from '@actions/core'; +/** + * Function to read environment variable and return a string value. + * + * @param property + */ +export async function readEnv(property: string): Promise { + const value = process.env[property]; + switch (value) { + case undefined: + return ''; + default: + return value; + } +} + /** * Function to get inputs from both with and env annotations. * @@ -12,13 +27,17 @@ export async function getInput( name: string, mandatory: boolean ): Promise { - const input = process.env[name]; - switch (input) { - case '': - case undefined: - return core.getInput(name, {required: mandatory}); - default: + const input = core.getInput(name); + const env_input = await readEnv(name); + switch (true) { + case input != '': return input; + case input == '' && env_input != '': + return env_input; + case input == '' && env_input == '' && mandatory: + throw new Error(`Input required and not supplied: ${name}`); + default: + return ''; } }