Skip to content

Commit

Permalink
test: Add unit testing (get-latest-version) (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris committed Jan 18, 2020
1 parent 386980e commit 442aa4d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 21 deletions.
64 changes: 64 additions & 0 deletions __tests__/get-latest-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {getURL, getLatestVersion} from '../src/get-latest-version';
import nock from 'nock';
import {FetchError} from 'node-fetch';
import {Tool} from '../src/constants';
import jsonTestBrew from './data/brew.json';
import jsonTestGithub from './data/github.json';

beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
nock.cleanAll();
});

describe('getURL()', () => {
test('return expected URL', () => {
const urlBrewExpected = `https://formulae.brew.sh/api/formula/${Tool.Repo}.json`;
const urlBrew: string = getURL(Tool.Org, Tool.Repo, 'brew');
expect(urlBrew).toMatch(urlBrewExpected);

const urlGithubExpected = `https://api.github.com/repos/${Tool.Org}/${Tool.Repo}/releases/latest`;
const urlGithub: string = getURL(Tool.Org, Tool.Repo, 'github');
expect(urlGithub).toMatch(urlGithubExpected);
});
});

describe('getLatestVersion()', () => {
test('return latest version via brew', async () => {
nock('https://formulae.brew.sh')
.get(`/api/formula/${Tool.Repo}.json`)
.reply(200, jsonTestBrew);

const versionLatest: string = await getLatestVersion(
Tool.Org,
Tool.Repo,
'brew'
);
expect(versionLatest).toMatch(Tool.TestVersionLatest);
});

test('return latest version via GitHub', async () => {
nock('https://api.github.com')
.get(`/repos/${Tool.Org}/${Tool.Repo}/releases/latest`)
.reply(200, jsonTestGithub);

const versionLatest: string = await getLatestVersion(
Tool.Org,
Tool.Repo,
'github'
);
expect(versionLatest).toMatch(Tool.TestVersionLatest);
});

test('return exception 404', async () => {
nock('https://formulae.brew.sh')
.get(`/api/formula/${Tool.Repo}.json`)
.reply(404);

await expect(
getLatestVersion(Tool.Org, Tool.Repo, 'brew')
).rejects.toThrowError(FetchError);
});
});
5 changes: 3 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ describe('showVersion()', () => {
});

test('return not found', async () => {
result = await main.showVersion('gitgit', ['--version']);
expect(result.exitcode).not.toBe(0);
await expect(
main.showVersion('gitgit', ['--version'])
).rejects.toThrowError(Error);
});
});
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export enum Tool {
Org = 'gohugoio',
Repo = 'hugo',
CmdName = 'hugo',
CmdOptVersion = 'version'
CmdOptVersion = 'version',
TestVersionLatest = '0.62.2',
TestVersionSpec = '0.61.0'
}
22 changes: 9 additions & 13 deletions src/get-latest-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ export async function getLatestVersion(
repo: string,
api: string
): Promise<string> {
try {
const url = getURL(org, repo, api);
const response = await fetch(url);
const json = await response.json();
let latestVersion = '';
if (api === 'brew') {
latestVersion = json.versions.stable;
} else if (api === 'github') {
latestVersion = json.tag_name;
}
return latestVersion;
} catch (e) {
return e;
const url = getURL(org, repo, api);
const response = await fetch(url);
const json = await response.json();
let latestVersion = '';
if (api === 'brew') {
latestVersion = json.versions.stable;
} else if (api === 'github') {
latestVersion = json.tag_name;
}
return latestVersion;
}
6 changes: 1 addition & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export async function showVersion(
}
};

try {
result.exitcode = await exec.exec(cmd, args, options);
} catch (e) {
return e;
}
result.exitcode = await exec.exec(cmd, args, options);
core.debug(`command: ${cmd} ${args}`);
core.debug(`exit code: ${result.exitcode}`);
core.debug(`stdout: ${result.output}`);
Expand Down

0 comments on commit 442aa4d

Please sign in to comment.