From a526749ce96e66c4e8c32ddc43f099c6830d0345 Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Fri, 17 May 2019 10:23:39 -0400 Subject: [PATCH] Add tests --- packages/core/__tests__/lib.test.ts | 104 ++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 30 deletions(-) diff --git a/packages/core/__tests__/lib.test.ts b/packages/core/__tests__/lib.test.ts index 012d0963..bae8f580 100644 --- a/packages/core/__tests__/lib.test.ts +++ b/packages/core/__tests__/lib.test.ts @@ -1,49 +1,93 @@ 'use strict'; -import * as core from '../src/lib' +import * as core from '../src/core'; +import * as os from 'os'; describe('@actions/core', () => { - it('tests exportVariable', () => { - // TODO + beforeAll(() => { + // Set inputs + process.env['INPUT_MY_INPUT'] = 'val'; + process.env['INPUT_MISSING'] = ''; + process.env['INPUT_SPECIAL_CHARS_\'\t\"\\'] = '\'\t\"\\ repsonse '; }); - it('tests getInput', () => { - // TODO + it('getInput gets non-required input', () => { + expect(core.getInput('my input')).toBe('val'); }); - it('tests setFailure', () => { - // TODO + it('getInput gets required input', () => { + expect(core.getInput('my input', {required: true})).toBe('val'); }); - it('tests error', () => { - // TODO + it('getInput throws on missing required input', () => { + expect(() => core.getInput('missing', {required: true})).toThrow('Failed to find input missing'); }); - it('tests warning', () => { - // TODO + it('getInput doesnt throw on missing non-required input', () => { + expect(core.getInput('missing', {required: false})).toBe(''); }); - it('tests debug', () => { - // TODO + it('getInput is case insensitive', () => { + expect(core.getInput('My InPuT')).toBe('val'); }); -}); + it('getInput handles special characters', () => { + expect(core.getInput('special chars_\'\t\"\\')).toBe('\'\t\"\\ repsonse'); + }); -// it('exits successfully', () => { -// jest.spyOn(process, 'exit').mockImplementation() -// core.fail('testing fail'); -// exit.success() -// expect(process.exit).toHaveBeenCalledWith(0) -// }) + it('setNeutral sets the correct exit code', () => { + core.setFailed('Failure message'); + expect(process.exitCode).toBe(1); + }); -// it('exits as a failure', () => { -// jest.spyOn(process, 'exit').mockImplementation() -// exit.failure() -// expect(process.exit).toHaveBeenCalledWith(1) -// }) + it('setFailure sets the correct exit code and failure message', () => { + // Override stdout and append to output so that we capture the command that is sent + let output = ''; + process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => { + output += p1; + return true; + } -// it('exits neutrally', () => { -// jest.spyOn(process, 'exit').mockImplementation() -// exit.neutral() -// expect(process.exit).toHaveBeenCalledWith(78) -// }) + core.setFailed('Failure message'); + expect(process.exitCode).toBe(1); + expect(output).toBe('##[error]Failure message' + os.EOL); + }); + + it('error sets the correct error message', () => { + // Override stdout and append to output so that we capture the command that is sent + let output = ''; + process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => { + output += p1; + return true; + } + + core.error('Error message'); + expect(output).toBe('##[error]Error message' + os.EOL); + }); + + it('warning sets the correct message', () => { + // Override stdout and append to output so that we capture the command that is sent + let output = ''; + process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => { + output += p1; + return true; + } + + core.warning('Warning'); + expect(output).toBe('##[warning]Warning' + os.EOL); + }); + + it('debug sets the correct message', () => { + // Override stdout and append to output so that we capture the command that is sent + let output = ''; + process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => { + output += p1; + return true; + } + + core.debug('Debug'); + expect(output).toBe('##[debug]Debug' + os.EOL); + }); + + // TODO - test escaping for all commands +}); \ No newline at end of file