From c1b77d8dd1212f8692f61d299e3ffdce5a4f81cc Mon Sep 17 00:00:00 2001 From: Nick Alteen Date: Tue, 7 Jan 2025 17:10:05 -0500 Subject: [PATCH] Fall back to default inputs --- __tests__/stubs/core/core.test.ts | 45 +++++++++++++++++++++++++++++++ package-lock.json | 4 +-- package.json | 2 +- src/stubs/core/core.ts | 22 +++++++++++++-- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/__tests__/stubs/core/core.test.ts b/__tests__/stubs/core/core.test.ts index 3d6a972..466f30c 100644 --- a/__tests__/stubs/core/core.test.ts +++ b/__tests__/stubs/core/core.test.ts @@ -178,6 +178,21 @@ describe('Core', () => { expect(getInput('test')).toEqual('test-lower') }) + it('Gets default inputs', () => { + delete process.env.INPUT_TEST + delete process.env.INPUT_test + + EnvMeta.inputs = { + test: { + description: 'test', + required: true, + default: 'default' + } + } + + expect(getInput('test')).toEqual('default') + }) + it('Returns an empty string', () => { expect(getInput('test-input-missing')).toEqual('') }) @@ -214,6 +229,21 @@ describe('Core', () => { ]) }) + it('Gets default inputs', () => { + delete process.env.INPUT_TEST + delete process.env.INPUT_test + + EnvMeta.inputs = { + test: { + description: 'test', + required: true, + default: 'default' + } + } + + expect(getMultilineInput('test')).toEqual(['default']) + }) + it('Returns an empty list if the input is not found', () => { expect(getMultilineInput('test-input-missing')).toMatchObject([]) }) @@ -246,6 +276,21 @@ describe('Core', () => { expect(getBooleanInput('test')).toBeFalsy() }) + it('Gets default inputs', () => { + delete process.env.INPUT_TEST + delete process.env.INPUT_test + + EnvMeta.inputs = { + test: { + description: 'test', + required: true, + default: 'false' + } + } + + expect(getBooleanInput('test')).toEqual(false) + }) + it('Throws an error if the input is required and not found', () => { expect(() => getBooleanInput('test-input-missing', { diff --git a/package-lock.json b/package-lock.json index e8a7467..aa53ac0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@github/local-action", - "version": "2.4.0", + "version": "2.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@github/local-action", - "version": "2.4.0", + "version": "2.5.0", "license": "MIT", "dependencies": { "@actions/artifact": "^2.2.0", diff --git a/package.json b/package.json index 6342ee8..ed8b39a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@github/local-action", "description": "Local Debugging for GitHub Actions", - "version": "2.4.0", + "version": "2.5.0", "type": "module", "author": "Nick Alteen ", "private": false, diff --git a/src/stubs/core/core.ts b/src/stubs/core/core.ts index a571934..74fdc92 100644 --- a/src/stubs/core/core.ts +++ b/src/stubs/core/core.ts @@ -200,6 +200,11 @@ export function getInput(name: string, options?: InputOptions): string { process.env[`INPUT_${name.replace(/ /g, '_')}`] || '' + // 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.toString() + // 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}`) @@ -224,7 +229,7 @@ export function getMultilineInput( options?: InputOptions ): string[] { // Get input by name, split by newline, and filter out empty strings - const input: string[] = ( + let input: string[] = ( process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || process.env[`INPUT_${name.replace(/ /g, '_')}`] || '' @@ -232,6 +237,14 @@ export function getMultilineInput( .split('\n') .filter(x => x !== '') + // If the input is not present in the environment variables, it has not been + // set. In that case, check the default value. + if (input.length === 0 && EnvMeta.inputs[name]?.default !== undefined) + input = EnvMeta.inputs[name].default + .toString() + .split('\n') + .filter(x => x !== '') + // Throw an error if the input is required and not supplied if (options && options.required === true && input.length === 0) throw new Error(`Input required and not supplied: ${name}`) @@ -257,12 +270,17 @@ export function getBooleanInput(name: string, options?: InputOptions): boolean { // using proxyquire's `callThru()` option. // Get input by name, or an empty string if not found - const input: string = ( + let input: string = ( process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || process.env[`INPUT_${name.replace(/ /g, '_')}`] || '' ).trim() + // 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() + // 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}`)