Skip to content

Commit

Permalink
fix boolean handling (#140)
Browse files Browse the repository at this point in the history
fixes 2 issues, although you may want to adjust how its done...

1 - optional booleans were throwing an error when empty. Not sure if you
want to return undefined, or just default to false if empty.

2 - boolean defaults were causing issues when unquoted in the yaml file
  • Loading branch information
ncalteen authored Jan 8, 2025
2 parents 268ee33 + 0f30c97 commit ccda8ef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
26 changes: 26 additions & 0 deletions __tests__/stubs/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,39 @@ describe('Core', () => {
expect(getBooleanInput('test')).toEqual(false)
})

it('Gets default inputs - with an unquoted boolean', () => {
delete process.env.INPUT_TEST
delete process.env.INPUT_test

EnvMeta.inputs = {
test: {
description: 'test',
required: true,
// while the spec says that the default value should be a string
// the yaml parser will pass an unquoted `true` or `false` through as a boolean
default: false as any // eslint-disable-line @typescript-eslint/no-explicit-any
}
}

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('Throws an error if the input is NOT required and not found', () => {
// ideally this would not throw - and either coerce to false or return undefined
// but this will require upstream changes. See discussion at https://github.com/github/local-action/pull/140
expect(() =>
getBooleanInput('test-input-missing', {
required: false
})
).toThrow()
})

it('Returns true or false for valid YAML boolean values', () => {
process.env.INPUT_TEST = 'true'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@github/local-action",
"description": "Local Debugging for GitHub Actions",
"version": "2.5.0",
"version": "2.5.1",
"type": "module",
"author": "Nick Alteen <ncalteen@github.com>",
"private": false,
Expand Down
14 changes: 10 additions & 4 deletions src/stubs/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,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 {
// here is where we would ideally return either `false` or `undefined` but we'll need upstream changes
}

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

0 comments on commit ccda8ef

Please sign in to comment.