From 8bd8aaa7d63ae22f3f441665dd73b309ad2b7a50 Mon Sep 17 00:00:00 2001 From: Shohei Ueda <30958501+peaceiris@users.noreply.github.com> Date: Fri, 3 Jan 2020 16:18:51 +0900 Subject: [PATCH] test: Add test for showVersion() (#28) * test: Rename index.test.ts to main.test.ts * test: Add test for showVersion() --- __tests__/{index.test.ts => main.test.ts} | 17 +++++++++++++++++ src/main.ts | 17 +++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) rename __tests__/{index.test.ts => main.test.ts} (67%) diff --git a/__tests__/index.test.ts b/__tests__/main.test.ts similarity index 67% rename from __tests__/index.test.ts rename to __tests__/main.test.ts index 94a3909..d1215c9 100644 --- a/__tests__/index.test.ts +++ b/__tests__/main.test.ts @@ -25,3 +25,20 @@ describe('run()', () => { expect(result.output).toMatch(/mdbook v/); }); }); + +describe('showVersion()', () => { + let result: main.actionResult = { + exitcode: 0, + output: '', + error: '' + }; + + test('Success case', async () => { + result = await main.showVersion('git', ['--version']); + expect(result.exitcode).toBe(0); + expect(result.output).toMatch(/git version/); + }); + + // test('Failure case', async () => { + // }); +}); diff --git a/src/main.ts b/src/main.ts index 8a857b1..9125935 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,12 +4,17 @@ import getLatestVersion from './get-latest-version'; import installer from './installer'; export interface actionResult { + exitcode: number; output: string; error: string; } -async function showVersion(cmd: string, args: string[]): Promise { +export async function showVersion( + cmd: string, + args: string[] +): Promise { let result: actionResult = { + exitcode: 0, output: '', error: '' }; @@ -25,7 +30,10 @@ async function showVersion(cmd: string, args: string[]): Promise { } }; - await exec.exec(cmd, args, options); + result.exitcode = await exec.exec(cmd, args, options); + core.debug(`exit code: ${result.exitcode}`); + core.debug(`stdout: ${result.output}`); + core.debug(`stderr: ${result.error}`); return result; } @@ -35,6 +43,7 @@ export async function run(): Promise { const mdbookVersion: string = core.getInput('mdbook-version'); let result: actionResult = { + exitcode: 0, output: '', error: '' }; @@ -47,13 +56,13 @@ export async function run(): Promise { ); console.log(`mdbook version: ${latestVersion} (${mdbookVersion})`); await installer(latestVersion); - result = await showVersion('mdbook', ['--version']); } else { console.log(`mdbook version: ${mdbookVersion}`); await installer(mdbookVersion); - result = await showVersion('mdbook', ['--version']); } + result = await showVersion('mdbook', ['--version']); + return result; } catch (error) { core.setFailed(error.message);