diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 850dd8b..642bc25 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,6 +1,11 @@ import * as main from '../src/main'; +const nock = require('nock'); +import {FetchError} from 'node-fetch'; +import jsonTestBrew from './data/brew.json'; +// import jsonTestGithub from './data/github.json'; jest.setTimeout(30000); +const repo: string = 'mdbook'; beforeEach(() => { jest.resetModules(); @@ -8,6 +13,7 @@ beforeEach(() => { afterEach(() => { delete process.env['INPUT_MDBOOK-VERSION']; + nock.cleanAll(); }); describe('Integration testing run()', () => { @@ -21,8 +27,60 @@ describe('Integration testing run()', () => { test('succeed in installing the latest version', async () => { const testVersion: string = 'latest'; process.env['INPUT_MDBOOK-VERSION'] = testVersion; + nock('https://formulae.brew.sh') + .get(`/api/formula/${repo}.json`) + .reply(200, jsonTestBrew); const result: main.actionResult = await main.run(); - expect(result.output).toMatch(/mdbook v/); + expect(result.output).toMatch('mdbook v0.3.5'); + }); + + test('fail to install a custom version due to 404 of tarball', async () => { + const testVersion: string = '0.3.4'; + process.env['INPUT_MDBOOK-VERSION'] = testVersion; + nock('https://github.com') + .get( + `/rust-lang/mdBook/releases/download/v${testVersion}/mdbook-v${testVersion}-x86_64-unknown-linux-gnu.tar.gz` + ) + .reply(404); + try { + const result: main.actionResult = await main.run(); + console.debug(result.output); + } catch (e) { + expect(e).toThrow(FetchError); + } + }); + + test('fail to install the latest version due to 404 of brew.sh', async () => { + const testVersion: string = 'latest'; + process.env['INPUT_MDBOOK-VERSION'] = testVersion; + nock('https://formulae.brew.sh') + .get(`/api/formula/${repo}.json`) + .reply(404); + try { + const result: main.actionResult = await main.run(); + console.debug(result.output); + } catch (e) { + expect(e).toThrow(FetchError); + } + }); + + test('fail to install the latest version due to 404 of tarball', async () => { + const testVersion: string = 'latest'; + process.env['INPUT_MDBOOK-VERSION'] = testVersion; + nock('https://formulae.brew.sh') + .get(`/api/formula/${repo}.json`) + .reply(200, jsonTestBrew); + nock('https://github.com') + .get( + `/rust-lang/mdBook/releases/download/v0.3.5/mdbook-v0.3.5-x86_64-unknown-linux-gnu.tar.gz` + ) + .reply(404); + try { + const result: main.actionResult = await main.run(); + console.debug(result.output); + } catch (e) { + expect(e).toThrow(FetchError); + } }); }); diff --git a/src/main.ts b/src/main.ts index a3614d5..ce0bd8d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -59,7 +59,8 @@ export async function run(): Promise { result = await showVersion('mdbook', ['--version']); return result; - } catch (error) { - core.setFailed(error.message); + } catch (e) { + core.setFailed(`Action failed with error ${e}`); + return e; } }