1
0
Fork 0

add core method to saveState and getState.

pull/149/head
Tingluo Huang 2019-09-19 22:02:45 -04:00
parent e2358e2973
commit b62614fa25
2 changed files with 43 additions and 7 deletions

View File

@ -17,7 +17,10 @@ const testEnvVars = {
INPUT_MY_INPUT: 'val', INPUT_MY_INPUT: 'val',
INPUT_MISSING: '', INPUT_MISSING: '',
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ', 'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces' INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
// Save inputs
STATE_state_1: 'state_val'
} }
describe('@actions/core', () => { describe('@actions/core', () => {
@ -93,17 +96,17 @@ describe('@actions/core', () => {
}) })
it('getInput gets required input', () => { it('getInput gets required input', () => {
expect(core.getInput('my input', {required: true})).toBe('val') expect(core.getInput('my input', { required: true })).toBe('val')
}) })
it('getInput throws on missing required input', () => { it('getInput throws on missing required input', () => {
expect(() => core.getInput('missing', {required: true})).toThrow( expect(() => core.getInput('missing', { required: true })).toThrow(
'Input required and not supplied: missing' 'Input required and not supplied: missing'
) )
}) })
it('getInput does not throw on missing non-required input', () => { it('getInput does not throw on missing non-required input', () => {
expect(core.getInput('missing', {required: false})).toBe('') expect(core.getInput('missing', { required: false })).toBe('')
}) })
it('getInput is case insensitive', () => { it('getInput is case insensitive', () => {
@ -194,6 +197,15 @@ describe('@actions/core', () => {
core.debug('\r\ndebug\n') core.debug('\r\ndebug\n')
assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`]) assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`])
}) })
it('saveState produces the correct command', () => {
core.saveState('state_1', 'some value')
assertWriteCalls([`::save-state name=state_1,::some value${os.EOL}`])
})
it('getState gets wrapper action state', () => {
expect(core.getState('state_1')).toBe('state_val')
})
}) })
// Assert that process.stdout.write calls called only with the given arguments. // Assert that process.stdout.write calls called only with the given arguments.

View File

@ -1,4 +1,4 @@
import {issue, issueCommand} from './command' import { issue, issueCommand } from './command'
import * as os from 'os' import * as os from 'os'
import * as path from 'path' import * as path from 'path'
@ -37,7 +37,7 @@ export enum ExitCode {
*/ */
export function exportVariable(name: string, val: string): void { export function exportVariable(name: string, val: string): void {
process.env[name] = val process.env[name] = val
issueCommand('set-env', {name}, val) issueCommand('set-env', { name }, val)
} }
/** /**
@ -86,7 +86,7 @@ export function getInput(name: string, options?: InputOptions): string {
* @param value value to store * @param value value to store
*/ */
export function setOutput(name: string, value: string): void { export function setOutput(name: string, value: string): void {
issueCommand('set-output', {name}, value) issueCommand('set-output', { name }, value)
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -178,3 +178,27 @@ export async function group<T>(name: string, fn: () => Promise<T>): Promise<T> {
return result return result
} }
//-----------------------------------------------------------------------
// Wrapper action state
//-----------------------------------------------------------------------
/**
* Saves state for current action, the state can only be retrieved by this action's post job execution.
*
* @param name name of the state to store
* @param value value to store
*/
export function saveState(name: string, value: string): void {
issueCommand('save-state', { name }, value)
}
/**
* Gets the value of an state set by this action's main execution.
*
* @param name name of the state to get
* @returns string
*/
export function getState(name: string): string {
return process.env[`STATE_${name}`] || ''
}