Prioritize with over env for inputs

This commit is contained in:
Shivam Mathur 2020-10-05 13:03:30 +05:30
parent 11e062bf23
commit c43f842cf3
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
3 changed files with 60 additions and 16 deletions

View File

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

33
dist/index.js vendored
View File

@ -1096,10 +1096,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.getInput = void 0;
exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = 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.
*
@ -1107,13 +1122,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;

View File

@ -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<string> {
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<string> {
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 '';
}
}