Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to default inputs #139

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading