From 2491fa05f6a1e39faefefec6ae530339e7e4b91f Mon Sep 17 00:00:00 2001 From: Theo Ephraim Date: Tue, 7 Jan 2025 16:12:53 -0800 Subject: [PATCH] fix boolean handling --- __tests__/stubs/core/core.test.ts | 24 ++++++++++++++++++++++++ src/stubs/core/core.ts | 19 ++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/__tests__/stubs/core/core.test.ts b/__tests__/stubs/core/core.test.ts index 466f30c..c753915 100644 --- a/__tests__/stubs/core/core.test.ts +++ b/__tests__/stubs/core/core.test.ts @@ -291,6 +291,23 @@ 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', { @@ -298,6 +315,13 @@ describe('Core', () => { }) ).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' diff --git a/src/stubs/core/core.ts b/src/stubs/core/core.ts index 74fdc92..357db85 100644 --- a/src/stubs/core/core.ts +++ b/src/stubs/core/core.ts @@ -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. @@ -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