1
0
Fork 0

Core: Add trimWhitespace to getInput (#802)

* Add option to not trim whitespace from inputs

* Fix typos

* Add doc clarification

* Rename options
pull/808/head
Luke Tomlinson 2021-05-11 13:51:36 -04:00 committed by GitHub
parent cac7db2d19
commit b33912b7cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -27,6 +27,7 @@ const testEnvVars = {
INPUT_BOOLEAN_INPUT_FALSE2: 'False', INPUT_BOOLEAN_INPUT_FALSE2: 'False',
INPUT_BOOLEAN_INPUT_FALSE3: 'FALSE', INPUT_BOOLEAN_INPUT_FALSE3: 'FALSE',
INPUT_WRONG_BOOLEAN_INPUT: 'wrong', INPUT_WRONG_BOOLEAN_INPUT: 'wrong',
INPUT_WITH_TRAILING_WHITESPACE: ' some val ',
// Save inputs // Save inputs
STATE_TEST_1: 'state_val', STATE_TEST_1: 'state_val',
@ -165,6 +166,22 @@ describe('@actions/core', () => {
) )
}) })
it('getInput trims whitespace by default', () => {
expect(core.getInput('with trailing whitespace')).toBe('some val')
})
it('getInput trims whitespace when option is explicitly true', () => {
expect(
core.getInput('with trailing whitespace', {trimWhitespace: true})
).toBe('some val')
})
it('getInput does not trim whitespace when option is false', () => {
expect(
core.getInput('with trailing whitespace', {trimWhitespace: false})
).toBe(' some val ')
})
it('getInput gets non-required boolean input', () => { it('getInput gets non-required boolean input', () => {
expect(core.getBooleanInput('boolean input')).toBe(true) expect(core.getBooleanInput('boolean input')).toBe(true)
}) })

View File

@ -11,6 +11,9 @@ import * as path from 'path'
export interface InputOptions { export interface InputOptions {
/** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */ /** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */
required?: boolean required?: boolean
/** Optional. Whether leading/trailing whitespace will be trimmed for the input. Defaults to true */
trimWhitespace?: boolean
} }
/** /**
@ -88,6 +91,10 @@ export function getInput(name: string, options?: InputOptions): string {
throw new Error(`Input required and not supplied: ${name}`) throw new Error(`Input required and not supplied: ${name}`)
} }
if (options && options.trimWhitespace === false) {
return val
}
return val.trim() return val.trim()
} }