Skip to content

Commit

Permalink
fix boolean handling
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Jan 8, 2025
1 parent 268ee33 commit 2491fa0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
24 changes: 24 additions & 0 deletions __tests__/stubs/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,37 @@ describe('Core', () => {
expect(getBooleanInput('test')).toEqual(false)
})

it('Gets default inputs', () => {
delete process.env.INPUT_TEST
delete process.env.INPUT_test

EnvMeta.inputs = {
test: {
description: 'test',
required: true,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
default: false
}
}

expect(getBooleanInput('test')).toEqual(false)
})

it('Throws an error if the input is required and not found', () => {
expect(() =>
getBooleanInput('test-input-missing', {
required: true
})
).toThrow()
})
it('Does NOT throws an error if the input is not required and not found', () => {
expect(() =>
getBooleanInput('test-input-missing', {
required: false
})
).not.toThrow()
})

it('Returns true or false for valid YAML boolean values', () => {
process.env.INPUT_TEST = 'true'
Expand Down
19 changes: 14 additions & 5 deletions src/stubs/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ export function getMultilineInput(
* @param options The options for the input
* @returns The value of the input
*/
export function getBooleanInput(name: string, options?: InputOptions): boolean {
export function getBooleanInput(
name: string,
options?: InputOptions
): boolean | undefined {
// This is effectively a copy of the actual `getInput` function, instead of
// using proxyquire's `callThru()` option.

Expand All @@ -278,12 +281,18 @@ export function getBooleanInput(name: string, options?: InputOptions): boolean {

// If the input is not present in the environment variables, it has not been
// set. In that case, check the default value.
if (input === '' && EnvMeta.inputs[name]?.default !== undefined)
input = EnvMeta.inputs[name].default.trim()
if (input === '' && EnvMeta.inputs[name]?.default !== undefined) {
// we call .toString in case its a boolean
input = EnvMeta.inputs[name].default.toString().trim()
}

// Throw an error if the input is required and not supplied
if (options && options.required === true && input === '')
throw new Error(`Input required and not supplied: ${name}`)
if (input === '')
if (options && options.required === true) {
throw new Error(`Input required and not supplied: ${name}`)
} else {
return undefined
}

if (['true', 'True', 'TRUE'].includes(input)) return true
if (['false', 'False', 'FALSE'].includes(input)) return false
Expand Down

0 comments on commit 2491fa0

Please sign in to comment.