2019-05-21 14:11:39 +00:00
|
|
|
import * as os from 'os'
|
2019-06-05 02:00:25 +00:00
|
|
|
import * as path from 'path'
|
2019-05-21 15:09:41 +00:00
|
|
|
import * as core from '../src/core'
|
2019-05-16 20:40:21 +00:00
|
|
|
|
2019-05-21 21:08:25 +00:00
|
|
|
/* eslint-disable @typescript-eslint/unbound-method */
|
|
|
|
|
2019-05-21 16:14:15 +00:00
|
|
|
const testEnvVars = {
|
|
|
|
'my var': '',
|
|
|
|
'special char var \r\n];': '',
|
|
|
|
'my var2': '',
|
|
|
|
'my secret': '',
|
|
|
|
'special char secret \r\n];': '',
|
|
|
|
'my secret2': '',
|
2019-06-05 02:00:25 +00:00
|
|
|
PATH: `path1${path.delimiter}path2`,
|
2019-05-21 16:14:15 +00:00
|
|
|
|
|
|
|
// Set inputs
|
|
|
|
INPUT_MY_INPUT: 'val',
|
|
|
|
INPUT_MISSING: '',
|
2019-09-12 17:41:11 +00:00
|
|
|
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
|
2019-09-20 02:02:45 +00:00
|
|
|
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
|
|
|
|
|
|
|
|
// Save inputs
|
2019-09-20 02:14:12 +00:00
|
|
|
STATE_TEST_1: 'state_val'
|
2019-05-21 16:14:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 20:40:21 +00:00
|
|
|
describe('@actions/core', () => {
|
2019-05-21 14:11:39 +00:00
|
|
|
beforeEach(() => {
|
2019-05-21 16:14:15 +00:00
|
|
|
for (const key in testEnvVars)
|
|
|
|
process.env[key] = testEnvVars[key as keyof typeof testEnvVars]
|
2019-05-21 16:27:43 +00:00
|
|
|
|
|
|
|
process.stdout.write = jest.fn()
|
2019-05-21 16:14:15 +00:00
|
|
|
})
|
2019-05-21 14:11:39 +00:00
|
|
|
|
2019-05-21 16:14:15 +00:00
|
|
|
afterEach(() => {
|
|
|
|
for (const key in testEnvVars) Reflect.deleteProperty(testEnvVars, key)
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('exportVariable produces the correct command and sets the env', () => {
|
|
|
|
core.exportVariable('my var', 'var val')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::set-env name=my var,::var val${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('exportVariable escapes variable names', () => {
|
|
|
|
core.exportVariable('special char var \r\n];', 'special val')
|
|
|
|
expect(process.env['special char var \r\n];']).toBe('special val')
|
2019-05-21 16:34:47 +00:00
|
|
|
assertWriteCalls([
|
2019-09-09 15:46:17 +00:00
|
|
|
`::set-env name=special char var %0D%0A%5D%3B,::special val${os.EOL}`
|
2019-05-21 16:34:47 +00:00
|
|
|
])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('exportVariable escapes variable values', () => {
|
|
|
|
core.exportVariable('my var2', 'var val\r\n')
|
|
|
|
expect(process.env['my var2']).toBe('var val\r\n')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::set-env name=my var2,::var val%0D%0A${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
2019-10-01 21:13:05 +00:00
|
|
|
it('setSecret produces the correct command', () => {
|
|
|
|
core.setSecret('secret val')
|
|
|
|
assertWriteCalls([`::add-mask::secret val${os.EOL}`])
|
|
|
|
})
|
2019-05-21 14:11:39 +00:00
|
|
|
|
2019-06-05 02:00:25 +00:00
|
|
|
it('prependPath produces the correct commands and sets the env', () => {
|
|
|
|
core.addPath('myPath')
|
|
|
|
expect(process.env['PATH']).toBe(
|
|
|
|
`myPath${path.delimiter}path1${path.delimiter}path2`
|
|
|
|
)
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::add-path::myPath${os.EOL}`])
|
2019-06-05 02:00:25 +00:00
|
|
|
})
|
|
|
|
|
2019-05-21 14:11:39 +00:00
|
|
|
it('getInput gets non-required input', () => {
|
|
|
|
expect(core.getInput('my input')).toBe('val')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('getInput gets required input', () => {
|
|
|
|
expect(core.getInput('my input', {required: true})).toBe('val')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('getInput throws on missing required input', () => {
|
|
|
|
expect(() => core.getInput('missing', {required: true})).toThrow(
|
|
|
|
'Input required and not supplied: missing'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-08-21 19:31:44 +00:00
|
|
|
it('getInput does not throw on missing non-required input', () => {
|
2019-05-21 14:11:39 +00:00
|
|
|
expect(core.getInput('missing', {required: false})).toBe('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('getInput is case insensitive', () => {
|
|
|
|
expect(core.getInput('My InPuT')).toBe('val')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('getInput handles special characters', () => {
|
2019-08-21 19:31:44 +00:00
|
|
|
expect(core.getInput('special chars_\'\t"\\')).toBe('\'\t"\\ response')
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
2019-09-12 17:41:11 +00:00
|
|
|
it('getInput handles multiple spaces', () => {
|
|
|
|
expect(core.getInput('multiple spaces variable')).toBe(
|
|
|
|
'I have multiple spaces'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-24 20:14:47 +00:00
|
|
|
it('setOutput produces the correct command', () => {
|
|
|
|
core.setOutput('some output', 'some value')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::set-output name=some output,::some value${os.EOL}`])
|
2019-06-24 20:14:47 +00:00
|
|
|
})
|
|
|
|
|
2019-05-21 14:11:39 +00:00
|
|
|
it('setFailure sets the correct exit code and failure message', () => {
|
|
|
|
core.setFailed('Failure message')
|
2019-06-25 17:54:41 +00:00
|
|
|
expect(process.exitCode).toBe(core.ExitCode.Failure)
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::error::Failure message${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('setFailure escapes the failure message', () => {
|
|
|
|
core.setFailed('Failure \r\n\nmessage\r')
|
2019-06-25 17:54:41 +00:00
|
|
|
expect(process.exitCode).toBe(core.ExitCode.Failure)
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::error::Failure %0D%0A%0Amessage%0D${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('error sets the correct error message', () => {
|
|
|
|
core.error('Error message')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::error::Error message${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('error escapes the error message', () => {
|
|
|
|
core.error('Error message\r\n\n')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::error::Error message%0D%0A%0A${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('warning sets the correct message', () => {
|
|
|
|
core.warning('Warning')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::warning::Warning${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('warning escapes the message', () => {
|
|
|
|
core.warning('\r\nwarning\n')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::warning::%0D%0Awarning%0A${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
2019-08-29 02:35:27 +00:00
|
|
|
it('startGroup starts a new group', () => {
|
|
|
|
core.startGroup('my-group')
|
2019-09-09 15:57:17 +00:00
|
|
|
assertWriteCalls([`::group::my-group${os.EOL}`])
|
2019-08-29 02:35:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('endGroup ends new group', () => {
|
|
|
|
core.endGroup()
|
2019-09-09 15:57:17 +00:00
|
|
|
assertWriteCalls([`::endgroup::${os.EOL}`])
|
2019-08-29 02:35:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('group wraps an async call in a group', async () => {
|
2019-08-29 02:38:30 +00:00
|
|
|
const result = await core.group('mygroup', async () => {
|
2019-08-29 02:35:27 +00:00
|
|
|
process.stdout.write('in my group\n')
|
2019-08-29 02:38:30 +00:00
|
|
|
return true
|
2019-08-29 02:35:27 +00:00
|
|
|
})
|
2019-08-29 02:38:30 +00:00
|
|
|
expect(result).toBe(true)
|
2019-08-29 02:35:27 +00:00
|
|
|
assertWriteCalls([
|
2019-09-09 15:57:17 +00:00
|
|
|
`::group::mygroup${os.EOL}`,
|
2019-08-29 02:35:27 +00:00
|
|
|
'in my group\n',
|
2019-09-09 15:57:17 +00:00
|
|
|
`::endgroup::${os.EOL}`
|
2019-08-29 02:35:27 +00:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2019-05-21 14:11:39 +00:00
|
|
|
it('debug sets the correct message', () => {
|
|
|
|
core.debug('Debug')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::debug::Debug${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('debug escapes the message', () => {
|
|
|
|
core.debug('\r\ndebug\n')
|
2019-09-09 15:46:17 +00:00
|
|
|
assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`])
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
2019-09-20 02:02:45 +00:00
|
|
|
|
|
|
|
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', () => {
|
2019-09-20 02:14:12 +00:00
|
|
|
expect(core.getState('TEST_1')).toBe('state_val')
|
2019-09-20 02:02:45 +00:00
|
|
|
})
|
2019-05-21 14:11:39 +00:00
|
|
|
})
|
2019-05-21 16:34:47 +00:00
|
|
|
|
|
|
|
// Assert that process.stdout.write calls called only with the given arguments.
|
2019-05-21 21:08:25 +00:00
|
|
|
function assertWriteCalls(calls: string[]): void {
|
2019-05-21 16:34:47 +00:00
|
|
|
expect(process.stdout.write).toHaveBeenCalledTimes(calls.length)
|
|
|
|
|
|
|
|
for (let i = 0; i < calls.length; i++) {
|
|
|
|
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i])
|
|
|
|
}
|
|
|
|
}
|