Skip to content

Commit

Permalink
test: Add get-latest-version test (#31)
Browse files Browse the repository at this point in the history
* refactor: Use core.info()
* test: Add getURL() test
* test: Add getLatestBrew() and getLatestGithub()
  • Loading branch information
peaceiris committed Jan 3, 2020
1 parent 17fabeb commit 258d379
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 19 deletions.
Empty file added __tests__/brew.json
Empty file.
1 change: 1 addition & 0 deletions __tests__/data/brew.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"mdbook","full_name":"mdbook","oldname":null,"aliases":[],"versioned_formulae":[],"desc":"Create modern online books from Markdown files","homepage":"https://rust-lang.github.io/mdBook/","versions":{"stable":"0.3.5","devel":null,"head":null,"bottle":true},"revision":0,"version_scheme":0,"bottle":{"stable":{"rebuild":0,"cellar":":any_skip_relocation","prefix":"/usr/local","root_url":"https://homebrew.bintray.com/bottles","files":{"catalina":{"url":"https://homebrew.bintray.com/bottles/mdbook-0.3.5.catalina.bottle.tar.gz","sha256":"0f164851ce201f257d69d4d55e50fa7707cea1ab268a5b239266a4662845beef"},"mojave":{"url":"https://homebrew.bintray.com/bottles/mdbook-0.3.5.mojave.bottle.tar.gz","sha256":"1a2b15f790541dc456294475b42927a152ea614857dfb49ca5f1a00aec1e9509"},"high_sierra":{"url":"https://homebrew.bintray.com/bottles/mdbook-0.3.5.high_sierra.bottle.tar.gz","sha256":"b2305bde8f2f620393c813fc208e9364e0681eff43d6535e7202841fc832ec9c"}}}},"keg_only":false,"bottle_disabled":false,"options":[],"build_dependencies":["rust"],"dependencies":[],"recommended_dependencies":[],"optional_dependencies":[],"uses_from_macos":[],"requirements":[],"conflicts_with":[],"caveats":null,"installed":[],"linked_keg":null,"pinned":false,"outdated":false,"analytics":{"install":{"30d":{"mdbook":62},"90d":{"mdbook":451},"365d":{"mdbook":774}},"install_on_request":{"30d":{"mdbook":62},"90d":{"mdbook":450},"365d":{"mdbook":774}},"build_error":{"30d":{"mdbook":0}}}}
142 changes: 142 additions & 0 deletions __tests__/data/github.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"url": "https://api.github.com/repos/rust-lang/mdBook/releases/21381829",
"assets_url": "https://api.github.com/repos/rust-lang/mdBook/releases/21381829/assets",
"upload_url": "https://uploads.github.com/repos/rust-lang/mdBook/releases/21381829/assets{?name,label}",
"html_url": "https://github.com/rust-lang/mdBook/releases/tag/v0.3.5",
"id": 21381829,
"node_id": "MDc6UmVsZWFzZTIxMzgxODI5",
"tag_name": "v0.3.5",
"target_commitish": "master",
"name": "v0.3.5",
"draft": false,
"author": {
"login": "ehuss",
"id": 43198,
"node_id": "MDQ6VXNlcjQzMTk4",
"avatar_url": "https://avatars0.githubusercontent.com/u/43198?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/ehuss",
"html_url": "https://github.com/ehuss",
"followers_url": "https://api.github.com/users/ehuss/followers",
"following_url": "https://api.github.com/users/ehuss/following{/other_user}",
"gists_url": "https://api.github.com/users/ehuss/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ehuss/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ehuss/subscriptions",
"organizations_url": "https://api.github.com/users/ehuss/orgs",
"repos_url": "https://api.github.com/users/ehuss/repos",
"events_url": "https://api.github.com/users/ehuss/events{/privacy}",
"received_events_url": "https://api.github.com/users/ehuss/received_events",
"type": "User",
"site_admin": false
},
"prerelease": false,
"created_at": "2019-11-11T21:36:13Z",
"published_at": "2019-11-11T21:37:26Z",
"assets": [
{
"url": "https://api.github.com/repos/rust-lang/mdBook/releases/assets/16096171",
"id": 16096171,
"node_id": "MDEyOlJlbGVhc2VBc3NldDE2MDk2MTcx",
"name": "mdbook-v0.3.5-x86_64-apple-darwin.tar.gz",
"label": "",
"uploader": {
"login": "github-actions[bot]",
"id": 41898282,
"node_id": "MDM6Qm90NDE4OTgyODI=",
"avatar_url": "https://avatars2.githubusercontent.com/in/15368?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github-actions%5Bbot%5D",
"html_url": "https://github.com/apps/github-actions",
"followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
"following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
"gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
"organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
"repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
"events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
"received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
"type": "Bot",
"site_admin": false
},
"content_type": "application/octet-stream",
"state": "uploaded",
"size": 3409794,
"download_count": 212,
"created_at": "2019-11-11T21:44:16Z",
"updated_at": "2019-11-11T21:44:17Z",
"browser_download_url": "https://github.com/rust-lang/mdBook/releases/download/v0.3.5/mdbook-v0.3.5-x86_64-apple-darwin.tar.gz"
},
{
"url": "https://api.github.com/repos/rust-lang/mdBook/releases/assets/16096347",
"id": 16096347,
"node_id": "MDEyOlJlbGVhc2VBc3NldDE2MDk2MzQ3",
"name": "mdbook-v0.3.5-x86_64-pc-windows-msvc.zip",
"label": "",
"uploader": {
"login": "github-actions[bot]",
"id": 41898282,
"node_id": "MDM6Qm90NDE4OTgyODI=",
"avatar_url": "https://avatars2.githubusercontent.com/in/15368?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github-actions%5Bbot%5D",
"html_url": "https://github.com/apps/github-actions",
"followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
"following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
"gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
"organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
"repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
"events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
"received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
"type": "Bot",
"site_admin": false
},
"content_type": "application/octet-stream",
"state": "uploaded",
"size": 3219324,
"download_count": 283,
"created_at": "2019-11-11T21:52:53Z",
"updated_at": "2019-11-11T21:52:53Z",
"browser_download_url": "https://github.com/rust-lang/mdBook/releases/download/v0.3.5/mdbook-v0.3.5-x86_64-pc-windows-msvc.zip"
},
{
"url": "https://api.github.com/repos/rust-lang/mdBook/releases/assets/16096247",
"id": 16096247,
"node_id": "MDEyOlJlbGVhc2VBc3NldDE2MDk2MjQ3",
"name": "mdbook-v0.3.5-x86_64-unknown-linux-gnu.tar.gz",
"label": "",
"uploader": {
"login": "github-actions[bot]",
"id": 41898282,
"node_id": "MDM6Qm90NDE4OTgyODI=",
"avatar_url": "https://avatars2.githubusercontent.com/in/15368?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github-actions%5Bbot%5D",
"html_url": "https://github.com/apps/github-actions",
"followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
"following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
"gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
"organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
"repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
"events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
"received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
"type": "Bot",
"site_admin": false
},
"content_type": "application/octet-stream",
"state": "uploaded",
"size": 3937087,
"download_count": 2131,
"created_at": "2019-11-11T21:48:20Z",
"updated_at": "2019-11-11T21:48:21Z",
"browser_download_url": "https://github.com/rust-lang/mdBook/releases/download/v0.3.5/mdbook-v0.3.5-x86_64-unknown-linux-gnu.tar.gz"
}
],
"tarball_url": "https://api.github.com/repos/rust-lang/mdBook/tarball/v0.3.5",
"zipball_url": "https://api.github.com/repos/rust-lang/mdBook/zipball/v0.3.5",
"body": "See https://github.com/rust-lang-nursery/mdBook/blob/master/CHANGELOG.md#mdbook-035 for a complete list of changes."
}
31 changes: 31 additions & 0 deletions __tests__/get-latest-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as target from '../src/get-latest-version';
import jsonTestBrew from './data/brew.json';
import jsonTestGithub from './data/github.json';

describe('Test', () => {
test('getURL()', () => {
const org: string = 'rust-lang';
const repo: string = 'mdbook';
const urlBrewExpected: string = `https://formulae.brew.sh/api/formula/${repo}.json`;
const urlGithubExpected: string = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
const urlBrew: string = target.getURL(org, repo, 'brew');
const urlGithub: string = target.getURL(org, repo, 'github');

expect(urlBrew).toMatch(urlBrewExpected);
expect(urlGithub).toMatch(urlGithubExpected);
});

let versionLatest: string = '0.3.5';

test('getLatestBrew()', () => {
const jsonBrew: target.JsonBrew = jsonTestBrew;
const versionBrew: string = target.getLatestBrew(jsonBrew);
expect(versionBrew).toMatch(versionLatest);
});

test('getLatestGithub()', () => {
const jsonGithub: target.JsonGithub = jsonTestGithub;
const versionGithub: string = target.getLatestGithub(jsonGithub);
expect(versionGithub).toMatch(versionLatest);
});
});
37 changes: 22 additions & 15 deletions src/get-latest-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ interface BrewVersions {
stable: string;
}

interface Json {
tag_name: string;
export interface JsonBrew {
versions: BrewVersions;
}

export interface JsonGithub {
tag_name: string;
}

const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

function getURL(org: string, repo: string, api: string): string {
export function getURL(org: string, repo: string, api: string): string {
let url: string = '';

if (api === 'brew') {
Expand All @@ -25,18 +28,16 @@ function getURL(org: string, repo: string, api: string): string {
return url;
}

function getLatest(api: string, data: Json): string {
export function getLatestBrew(data: JsonBrew): string {
let latestVersion: string = '';
latestVersion = data.versions.stable;
return latestVersion;
}

if (api === 'brew') {
latestVersion = data.versions.stable;
} else if (api === 'github') {
latestVersion = data.tag_name;
latestVersion = latestVersion.replace('v', '');
} else {
core.setFailed(`Source API ${api} is not supported.`);
}

export function getLatestGithub(data: JsonGithub): string {
let latestVersion: string = '';
latestVersion = data.tag_name;
latestVersion = latestVersion.replace('v', '');
return latestVersion;
}

Expand All @@ -51,8 +52,14 @@ export default async function getLatestVersion(
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
const result: Json = JSON.parse(xhr.responseText);
const latestVersion: string = getLatest(api, result);
let latestVersion: string = '';
if (api === 'brew') {
const result: JsonBrew = JSON.parse(xhr.responseText);
latestVersion = getLatestBrew(result);
} else if (api === 'github') {
const result: JsonGithub = JSON.parse(xhr.responseText);
latestVersion = getLatestGithub(result);
}
resolve(latestVersion);
};
xhr.onerror = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ if (!tempDir) {

export default async function installer(version: string) {
const osName: string = getOS(process.platform);
console.log(`Operating System: ${osName}`);
core.info(`Operating System: ${osName}`);

const toolURL: string = getURL(osName, version);
core.debug(`toolURL: ${toolURL}`);
core.info(`toolURL: ${toolURL}`);

let baseLocation: string;
if (process.platform === 'win32') {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function run(): Promise<any> {
installVersion = toolVersion;
}

console.log(`mdbook version: ${installVersion}`);
core.info(`mdbook version: ${installVersion}`);
await installer(installVersion);
result = await showVersion('mdbook', ['--version']);

Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

Expand All @@ -58,6 +58,8 @@
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

"resolveJsonModule": true,
},
"exclude": ["node_modules", "**/*.test.ts"]
}

0 comments on commit 258d379

Please sign in to comment.