Prioritize with over env for inputs

This commit is contained in:
Shivam Mathur 2020-10-03 01:39:43 +05:30
parent ebba1db2c3
commit 8cb01730c7
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
3 changed files with 60 additions and 15 deletions

View File

@ -17,13 +17,20 @@ async function cleanup(path: string): Promise<void> {
} }
describe('Utils tests', () => { 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 () => { it('checking getInput', async () => {
process.env['test'] = 'setup-php'; process.env['test'] = 'setup-php';
process.env['undefined'] = '';
expect(await utils.getInput('test', false)).toBe('setup-php'); 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('setup-php', false)).toBe('setup-php');
expect(await utils.getInput('DoesNotExist', false)).toBe(''); 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 () => { it('checking parseVersion', async () => {

33
dist/index.js vendored
View File

@ -1097,10 +1097,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); 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 fs = __importStar(__webpack_require__(747));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const core = __importStar(__webpack_require__(470)); 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. * Function to get inputs from both with and env annotations.
* *
@ -1108,13 +1123,17 @@ const core = __importStar(__webpack_require__(470));
* @param mandatory * @param mandatory
*/ */
async function getInput(name, mandatory) { async function getInput(name, mandatory) {
const input = process.env[name]; const input = core.getInput(name);
switch (input) { const env_input = await readEnv(name);
case '': switch (true) {
case undefined: case input != '':
return core.getInput(name, { required: mandatory });
default:
return 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; exports.getInput = getInput;

View File

@ -2,6 +2,21 @@ import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as core from '@actions/core'; 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<string> {
const value = process.env[property];
switch (value) {
case undefined:
return '';
default:
return value;
}
}
/** /**
* Function to get inputs from both with and env annotations. * Function to get inputs from both with and env annotations.
* *
@ -12,13 +27,17 @@ export async function getInput(
name: string, name: string,
mandatory: boolean mandatory: boolean
): Promise<string> { ): Promise<string> {
const input = process.env[name]; const input = core.getInput(name);
switch (input) { const env_input = await readEnv(name);
case '': switch (true) {
case undefined: case input != '':
return core.getInput(name, {required: mandatory});
default:
return 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 '';
} }
} }