1
0
Fork 0

DRY up asserts on process.stdout.write

pull/4/head
Jonathan Clem 2019-05-21 12:34:47 -04:00
parent 406242718f
commit f30996e6bb
No known key found for this signature in database
GPG Key ID: 48C5B22E9FD6E80F
1 changed files with 30 additions and 52 deletions

View File

@ -29,67 +29,50 @@ describe('@actions/core', () => {
it('exportVariable produces the correct command and sets the env', () => { it('exportVariable produces the correct command and sets the env', () => {
core.exportVariable('my var', 'var val') core.exportVariable('my var', 'var val')
expect(process.env['my var']).toBe('var val') assertWriteCalls([`##[set-variable name=my var;]var val${os.EOL}`])
expect(process.stdout.write).toHaveBeenCalledWith(
`##[set-variable name=my var;]var val${os.EOL}`
)
}) })
it('exportVariable escapes variable names', () => { it('exportVariable escapes variable names', () => {
core.exportVariable('special char var \r\n];', 'special val') core.exportVariable('special char var \r\n];', 'special val')
expect(process.env['special char var \r\n];']).toBe('special val') expect(process.env['special char var \r\n];']).toBe('special val')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([
`##[set-variable name=special char var %0D%0A%5D%3B;]special val${os.EOL}` `##[set-variable name=special char var %0D%0A%5D%3B;]special val${os.EOL}`
) ])
}) })
it('exportVariable escapes variable values', () => { it('exportVariable escapes variable values', () => {
core.exportVariable('my var2', 'var val\r\n') core.exportVariable('my var2', 'var val\r\n')
expect(process.env['my var2']).toBe('var val\r\n') expect(process.env['my var2']).toBe('var val\r\n')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[set-variable name=my var2;]var val%0D%0A${os.EOL}`])
`##[set-variable name=my var2;]var val%0D%0A${os.EOL}`
)
}) })
it('setSecret produces the correct commands and sets the env', () => { it('setSecret produces the correct commands and sets the env', () => {
core.setSecret('my secret', 'secret val') core.setSecret('my secret', 'secret val')
expect(process.env['my secret']).toBe('secret val') expect(process.env['my secret']).toBe('secret val')
expect(process.stdout.write).toHaveBeenNthCalledWith( assertWriteCalls([
1, `##[set-variable name=my secret;]secret val${os.EOL}`,
`##[set-variable name=my secret;]secret val${os.EOL}`
)
expect(process.stdout.write).toHaveBeenNthCalledWith(
2,
`##[set-secret]secret val${os.EOL}` `##[set-secret]secret val${os.EOL}`
) ])
}) })
it('setSecret escapes secret names', () => { it('setSecret escapes secret names', () => {
core.setSecret('special char secret \r\n];', 'special secret val') core.setSecret('special char secret \r\n];', 'special secret val')
expect(process.env['special char secret \r\n];']).toBe('special secret val') expect(process.env['special char secret \r\n];']).toBe('special secret val')
expect(process.stdout.write).toHaveBeenNthCalledWith( assertWriteCalls([
1,
`##[set-variable name=special char secret %0D%0A%5D%3B;]special secret val${ `##[set-variable name=special char secret %0D%0A%5D%3B;]special secret val${
os.EOL os.EOL
}` }`,
)
expect(process.stdout.write).toHaveBeenNthCalledWith(
2,
`##[set-secret]special secret val${os.EOL}` `##[set-secret]special secret val${os.EOL}`
) ])
}) })
it('setSecret escapes secret values', () => { it('setSecret escapes secret values', () => {
core.setSecret('my secret2', 'secret val\r\n') core.setSecret('my secret2', 'secret val\r\n')
expect(process.env['my secret2']).toBe('secret val\r\n') expect(process.env['my secret2']).toBe('secret val\r\n')
expect(process.stdout.write).toHaveBeenNthCalledWith( assertWriteCalls([
1, `##[set-variable name=my secret2;]secret val%0D%0A${os.EOL}`,
`##[set-variable name=my secret2;]secret val%0D%0A${os.EOL}`
)
expect(process.stdout.write).toHaveBeenNthCalledWith(
2,
`##[set-secret]secret val%0D%0A${os.EOL}` `##[set-secret]secret val%0D%0A${os.EOL}`
) ])
}) })
it('getInput gets non-required input', () => { it('getInput gets non-required input', () => {
@ -126,56 +109,51 @@ describe('@actions/core', () => {
it('setFailure sets the correct exit code and failure message', () => { it('setFailure sets the correct exit code and failure message', () => {
core.setFailed('Failure message') core.setFailed('Failure message')
expect(process.exitCode).toBe(1) expect(process.exitCode).toBe(1)
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[error]Failure message${os.EOL}`])
`##[error]Failure message${os.EOL}`
)
}) })
it('setFailure escapes the failure message', () => { it('setFailure escapes the failure message', () => {
core.setFailed('Failure \r\n\nmessage\r') core.setFailed('Failure \r\n\nmessage\r')
expect(process.exitCode).toBe(1) expect(process.exitCode).toBe(1)
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[error]Failure %0D%0A%0Amessage%0D${os.EOL}`])
`##[error]Failure %0D%0A%0Amessage%0D${os.EOL}`
)
}) })
it('error sets the correct error message', () => { it('error sets the correct error message', () => {
core.error('Error message') core.error('Error message')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[error]Error message${os.EOL}`])
`##[error]Error message${os.EOL}`
)
}) })
it('error escapes the error message', () => { it('error escapes the error message', () => {
core.error('Error message\r\n\n') core.error('Error message\r\n\n')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[error]Error message%0D%0A%0A${os.EOL}`])
`##[error]Error message%0D%0A%0A${os.EOL}`
)
}) })
it('warning sets the correct message', () => { it('warning sets the correct message', () => {
core.warning('Warning') core.warning('Warning')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[warning]Warning${os.EOL}`])
`##[warning]Warning${os.EOL}`
)
}) })
it('warning escapes the message', () => { it('warning escapes the message', () => {
core.warning('\r\nwarning\n') core.warning('\r\nwarning\n')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[warning]%0D%0Awarning%0A${os.EOL}`])
`##[warning]%0D%0Awarning%0A${os.EOL}`
)
}) })
it('debug sets the correct message', () => { it('debug sets the correct message', () => {
core.debug('Debug') core.debug('Debug')
expect(process.stdout.write).toHaveBeenCalledWith(`##[debug]Debug${os.EOL}`) assertWriteCalls([`##[debug]Debug${os.EOL}`])
}) })
it('debug escapes the message', () => { it('debug escapes the message', () => {
core.debug('\r\ndebug\n') core.debug('\r\ndebug\n')
expect(process.stdout.write).toHaveBeenCalledWith( assertWriteCalls([`##[debug]%0D%0Adebug%0A${os.EOL}`])
`##[debug]%0D%0Adebug%0A${os.EOL}`
)
}) })
}) })
// Assert that process.stdout.write calls called only with the given arguments.
function assertWriteCalls(calls: string[]) {
expect(process.stdout.write).toHaveBeenCalledTimes(calls.length)
for (let i = 0; i < calls.length; i++) {
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i])
}
}