Skip to content

Commit

Permalink
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 #723
  • Loading branch information
yi-Xu-0100 committed Feb 24, 2021
1 parent 383ec9f commit 88d67dd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/core/__tests__/core.test.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -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}`])
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/core.ts
Expand Up @@ -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.
*
Expand Down

0 comments on commit 88d67dd

Please sign in to comment.