Skip to content

Commit

Permalink
Fall back to default inputs (#139)
Browse files Browse the repository at this point in the history
This PR adds a check so that, if an input is not present in the
environment variables, the default value is used from the
`action.yml`/`action.yaml` file (if a default is present).
  • Loading branch information
ncalteen authored Jan 7, 2025
2 parents 6b7a9eb + c1b77d8 commit 268ee33
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
45 changes: 45 additions & 0 deletions __tests__/stubs/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
})
Expand Down Expand Up @@ -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([])
})
Expand Down Expand Up @@ -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', {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.4.0",
"version": "2.5.0",
"type": "module",
"author": "Nick Alteen <[email protected]>",
"private": false,
Expand Down
22 changes: 20 additions & 2 deletions src/stubs/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -224,14 +229,22 @@ 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, '_')}`] ||
''
)
.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}`)
Expand All @@ -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}`)
Expand Down

0 comments on commit 268ee33

Please sign in to comment.