Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add failure testing #49

Merged
merged 2 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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();
});

afterEach(() => {
delete process.env['INPUT_MDBOOK-VERSION'];
nock.cleanAll();
});

describe('Integration testing run()', () => {
Expand All @@ -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);
}
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export async function run(): Promise<any> {
result = await showVersion('mdbook', ['--version']);

return result;
} catch (error) {
core.setFailed(error.message);
} catch (e) {
core.setFailed(`Action failed with error ${e}`);
return e;
}
}