From 88d67ddf695288415755f18ecdbf56f6f926ad0a Mon Sep 17 00:00:00 2001 From: yi_Xu Date: Wed, 24 Feb 2021 12:47:14 +0800 Subject: [PATCH] feat(core): add getBooleanInput function Gets the input value of the boolean type in the YAML specification. The return value is also in boolean type. ref: https://yaml.org/type/bool.html close #723 --- packages/core/__tests__/core.test.ts | 12 ++++++++ packages/core/src/core.ts | 43 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index 0e52e9b2cf..e3441df0be 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -19,6 +19,8 @@ const testEnvVars = { INPUT_MISSING: '', 'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ', INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces', + INPUT_BOOLEAN_INPUT: 'true', + INPUT_WRONG_BOOLEAN_INPUT: 'wrong', // Save inputs STATE_TEST_1: 'state_val', @@ -157,6 +159,16 @@ describe('@actions/core', () => { ) }) + it('getBooleanInput handles boolean input', () => { + expect(core.getBooleanInput('boolean input')).toBe(true) + }) + + it('getBooleanInput handles wrong boolean input', () => { + expect(() => core.getBooleanInput('wrong boolean input')).toThrow( + 'Input does not meet YAML specifications: wrong boolean input' + ) + }) + it('setOutput produces the correct command', () => { core.setOutput('some output', 'some value') assertWriteCalls([`::set-output name=some output::some value${os.EOL}`]) diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index e7e366c501..16cccecfda 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -91,6 +91,49 @@ export function getInput(name: string, options?: InputOptions): string { return val.trim() } +/** + * Gets the input value of the boolean type in the YAML specification. + * The return value is also in boolean type. + * ref: https://yaml.org/type/bool.html + * + * @param name name of the input to get + * @returns boolean + */ +export function getBooleanInput(name: string): boolean { + const trueValue = [ + 'true', + 'True', + 'TRUE', + 'yes', + 'Yes', + 'YES', + 'y', + 'Y', + 'on', + 'On', + 'ON' + ] + const falseValue = [ + 'false', + 'False', + 'FALSE', + 'no', + 'No', + 'NO', + 'n', + 'N', + 'off', + 'Off', + 'OFF' + ] + const val: string = ( + process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || '' + ).trim() + if (trueValue.includes(val)) return true + if (falseValue.includes(val)) return false + throw new TypeError(`Input does not meet YAML specifications: ${name}`) +} + /** * Sets the value of an output. *