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

Core: Add trimWhiteSpace to getInput #802

Merged
merged 4 commits into from May 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/core/__tests__/core.test.ts
Expand Up @@ -27,6 +27,7 @@ const testEnvVars = {
INPUT_BOOLEAN_INPUT_FALSE2: 'False',
INPUT_BOOLEAN_INPUT_FALSE3: 'FALSE',
INPUT_WRONG_BOOLEAN_INPUT: 'wrong',
INPUT_WITH_TRAILING_WHITESPACE: ' some val ',

// Save inputs
STATE_TEST_1: 'state_val',
Expand Down Expand Up @@ -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', () => {
expect(core.getBooleanInput('boolean input')).toBe(true)
})
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/core.ts
Expand Up @@ -11,6 +11,9 @@ import * as path from 'path'
export interface InputOptions {
/** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */
required?: boolean

/** Optional. Whether leading/trailing whitespace will be trimmed for the input. Defaults to true */
trimWhitespace?: boolean
}

/**
Expand Down Expand Up @@ -88,6 +91,10 @@ export function getInput(name: string, options?: InputOptions): string {
throw new Error(`Input required and not supplied: ${name}`)
}

if (options && options.trimWhitespace === false) {
return val
}

return val.trim()
}

Expand Down