From 368f190119bff55e49674e9e96d03f2d251e2834 Mon Sep 17 00:00:00 2001 From: peaceiris <30958501+peaceiris@users.noreply.github.com> Date: Sun, 5 Jan 2020 14:40:47 +0000 Subject: [PATCH] test: Add exception test --- __tests__/main.test.ts | 20 +++++++++++-------- src/main.ts | 45 ++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 4514722..af0e603 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -10,15 +10,15 @@ afterEach(() => { delete process.env['INPUT_MDBOOK-VERSION']; }); -describe('run()', () => { - test('Integration testing (custom version)', async () => { +describe('Integration testing run()', () => { + test('should install custom version', async () => { const testVersion: string = '0.3.4'; process.env['INPUT_MDBOOK-VERSION'] = testVersion; const result: main.actionResult = await main.run(); expect(result.output).toMatch(`mdbook v${testVersion}`); }); - test('Integration testing (latest version)', async () => { + test('should install latest version', async () => { const testVersion: string = 'latest'; process.env['INPUT_MDBOOK-VERSION'] = testVersion; const result: main.actionResult = await main.run(); @@ -29,16 +29,20 @@ describe('run()', () => { describe('showVersion()', () => { let result: main.actionResult = { exitcode: 0, - output: '', - error: '' + output: '' }; - test('Success case', async () => { + test('should return version', async () => { result = await main.showVersion('git', ['--version']); expect(result.exitcode).toBe(0); expect(result.output).toMatch(/git version/); }); - // test('Failure case', async () => { - // }); + test('should return exception', async () => { + try { + result = await main.showVersion('gitgit', ['--version']); + } catch (e) { + expect(e).toThrow(Error); + } + }); }); diff --git a/src/main.ts b/src/main.ts index 5aff9dd..a3614d5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,37 +6,35 @@ import {installer} from './installer'; export interface actionResult { exitcode: number; output: string; - error: string; } export async function showVersion( cmd: string, args: string[] ): Promise { - let result: actionResult = { - exitcode: 0, - output: '', - error: '' - }; + try { + let result: actionResult = { + exitcode: 0, + output: '' + }; - const options = { - listeners: { - stdout: (data: Buffer) => { - result.output += data.toString(); - }, - stderr: (data: Buffer) => { - result.error += data.toString(); + const options = { + listeners: { + stdout: (data: Buffer) => { + result.output += data.toString(); + } } - } - }; + }; - result.exitcode = await exec.exec(cmd, args, options); - core.debug(` - exit code: ${result.exitcode} - stdout: ${result.output} - stderr: ${result.error} - `); - return result; + result.exitcode = await exec.exec(cmd, args, options); + core.debug(` + exit code: ${result.exitcode} + stdout: ${result.output} + `); + return result; + } catch (e) { + return e; + } } // most @actions toolkit packages have async methods @@ -47,8 +45,7 @@ export async function run(): Promise { let result: actionResult = { exitcode: 0, - output: '', - error: '' + output: '' }; if (toolVersion === '' || toolVersion === 'latest') {