mirror of
https://github.com/status-im/actions-hugo.git
synced 2025-02-04 17:03:26 +00:00
test: Add unit testing (get-latest-version) (#132)
This commit is contained in:
parent
386980e22b
commit
442aa4dbd4
64
__tests__/get-latest-version.test.ts
Normal file
64
__tests__/get-latest-version.test.ts
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
});
|
@ -56,7 +56,8 @@ describe('showVersion()', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('return not found', async () => {
|
test('return not found', async () => {
|
||||||
result = await main.showVersion('gitgit', ['--version']);
|
await expect(
|
||||||
expect(result.exitcode).not.toBe(0);
|
main.showVersion('gitgit', ['--version'])
|
||||||
|
).rejects.toThrowError(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -3,5 +3,7 @@ export enum Tool {
|
|||||||
Org = 'gohugoio',
|
Org = 'gohugoio',
|
||||||
Repo = 'hugo',
|
Repo = 'hugo',
|
||||||
CmdName = 'hugo',
|
CmdName = 'hugo',
|
||||||
CmdOptVersion = 'version'
|
CmdOptVersion = 'version',
|
||||||
|
TestVersionLatest = '0.62.2',
|
||||||
|
TestVersionSpec = '0.61.0'
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ export async function getLatestVersion(
|
|||||||
repo: string,
|
repo: string,
|
||||||
api: string
|
api: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
try {
|
|
||||||
const url = getURL(org, repo, api);
|
const url = getURL(org, repo, api);
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
@ -28,7 +27,4 @@ export async function getLatestVersion(
|
|||||||
latestVersion = json.tag_name;
|
latestVersion = json.tag_name;
|
||||||
}
|
}
|
||||||
return latestVersion;
|
return latestVersion;
|
||||||
} catch (e) {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,7 @@ export async function showVersion(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
|
||||||
result.exitcode = await exec.exec(cmd, args, options);
|
result.exitcode = await exec.exec(cmd, args, options);
|
||||||
} catch (e) {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
core.debug(`command: ${cmd} ${args}`);
|
core.debug(`command: ${cmd} ${args}`);
|
||||||
core.debug(`exit code: ${result.exitcode}`);
|
core.debug(`exit code: ${result.exitcode}`);
|
||||||
core.debug(`stdout: ${result.output}`);
|
core.debug(`stdout: ${result.output}`);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user