Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add getBooleanInput function
Browse files Browse the repository at this point in the history
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 actions#723
yi-Xu-0100 committed Feb 24, 2021

Verified

This commit was signed with the committer’s verified signature. The key has expired.
1 parent 383ec9f commit 88d67dd
Showing 2 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/core/__tests__/core.test.ts
Original file line number Diff line number Diff line change
@@ -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}`])
43 changes: 43 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
@@ -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.
*

0 comments on commit 88d67dd

Please sign in to comment.