test: Add integration testing (#131)

* docs: Update description
* deps: Add nock
* chore: Add resolveJsonModule
* test: Add integration testing
* chore: Add @typescript-eslint/eslint-plugin
* refactor: Fix lint errors
* chore: Add eslint-plugin-jest
* refactor: Fix lint errors
* test: Add remove working files
* ci: Comment out cache steps
This commit is contained in:
Shohei Ueda
2020-01-18 10:29:06 +09:00
committed by GitHub
parent 477d977a96
commit 386980e22b
16 changed files with 1249 additions and 96 deletions

7
src/constants.ts Normal file
View File

@@ -0,0 +1,7 @@
export enum Tool {
Name = 'Hugo',
Org = 'gohugoio',
Repo = 'hugo',
CmdName = 'hugo',
CmdOptVersion = 'version'
}

View File

@@ -1,7 +1,7 @@
import fetch from 'node-fetch';
export function getURL(org: string, repo: string, api: string): string {
let url: string = '';
let url = '';
if (api === 'brew') {
url = `https://formulae.brew.sh/api/formula/${repo}.json`;
@@ -21,7 +21,7 @@ export async function getLatestVersion(
const url = getURL(org, repo, api);
const response = await fetch(url);
const json = await response.json();
let latestVersion: string = '';
let latestVersion = '';
if (api === 'brew') {
latestVersion = json.versions.stable;
} else if (api === 'github') {

View File

@@ -1,4 +1,4 @@
export default function getOS(platform: string) {
export default function getOS(platform: string): string {
if (platform === 'linux') {
return 'Linux';
} else if (platform === 'darwin') {

View File

@@ -3,7 +3,7 @@ export default function getURL(
extended: string,
version: string
): string {
const extendedStr = (extended: string) => {
const extendedStr = (extended: string): string => {
if (extended === 'true') {
return 'extended_';
} else {
@@ -13,7 +13,7 @@ export default function getURL(
}
};
const ext = (os: string) => {
const ext = (os: string): string => {
if (os === 'Windows') {
return 'zip';
} else {
@@ -21,11 +21,9 @@ export default function getURL(
}
};
const hugoName: string = `hugo_${extendedStr(
extended
)}${version}_${os}-64bit`;
const baseURL: string = 'https://github.com/gohugoio/hugo/releases/download';
const url: string = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
const hugoName = `hugo_${extendedStr(extended)}${version}_${os}-64bit`;
const baseURL = 'https://github.com/gohugoio/hugo/releases/download';
const url = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
return url;
}

View File

@@ -1,67 +1,3 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {getLatestVersion} from './get-latest-version';
import {installer} from './installer';
import * as main from './main';
export interface actionResult {
exitcode: number;
output: string;
}
export async function showVersion(
cmd: string,
args: string[]
): Promise<actionResult> {
try {
let result: actionResult = {
exitcode: 0,
output: ''
};
const options = {
listeners: {
stdout: (data: Buffer) => {
result.output += data.toString();
}
}
};
result.exitcode = await exec.exec(cmd, args, options);
core.debug(`
exit code: ${result.exitcode}
stdout: ${result.output}
`);
return result;
} catch (e) {
return e;
}
}
async function run() {
try {
const toolVersion: string = core.getInput('hugo-version');
let installVersion: string = '';
let result: actionResult = {
exitcode: 0,
output: ''
};
if (toolVersion === '' || toolVersion === 'latest') {
installVersion = await getLatestVersion('gohugoio', 'hugo', 'brew');
} else {
installVersion = toolVersion;
}
core.info(`hugo version: ${installVersion}`);
await installer(installVersion);
result = await showVersion('hugo', ['version']);
return result;
} catch (e) {
core.setFailed(`Action failed with error ${e}`);
return e;
}
}
run();
main.run();

View File

@@ -16,7 +16,7 @@ if (!tempDir) {
tempDir = path.join(baseTempLocation, 'tmp');
}
export async function installer(version: string) {
export async function installer(version: string): Promise<void> {
try {
const extended: string = core.getInput('extended');
console.log(`Hugo extended: ${extended}`);
@@ -40,7 +40,7 @@ export async function installer(version: string) {
// Download and extract Hugo binary
await io.mkdirP(tempDir);
const hugoAssets: string = await tc.downloadTool(hugoURL);
let hugoBin: string = '';
let hugoBin = '';
if (osName === 'Windows') {
const hugoExtractedFolder: string = await tc.extractZip(
hugoAssets,

65
src/main.ts Normal file
View File

@@ -0,0 +1,65 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {getLatestVersion} from './get-latest-version';
import {installer} from './installer';
import {Tool} from './constants';
export interface ActionResult {
exitcode: number;
output: string;
}
export async function showVersion(
cmd: string,
args: string[]
): Promise<ActionResult> {
const result: ActionResult = {
exitcode: 0,
output: ''
};
const options = {
listeners: {
stdout: (data: Buffer): void => {
result.output += data.toString();
}
}
};
try {
result.exitcode = await exec.exec(cmd, args, options);
} catch (e) {
return e;
}
core.debug(`command: ${cmd} ${args}`);
core.debug(`exit code: ${result.exitcode}`);
core.debug(`stdout: ${result.output}`);
return result;
}
export async function run(): Promise<ActionResult> {
try {
const toolVersion: string = core.getInput('hugo-version');
let installVersion = '';
let result: ActionResult = {
exitcode: 0,
output: ''
};
if (toolVersion === '' || toolVersion === 'latest') {
installVersion = await getLatestVersion(Tool.Org, Tool.Repo, 'brew');
} else {
installVersion = toolVersion;
}
core.info(`${Tool.Name} version: ${installVersion}`);
await installer(installVersion);
result = await showVersion(Tool.CmdName, [Tool.CmdOptVersion]);
return result;
} catch (e) {
core.setFailed(`Action failed with error ${e}`);
return e;
}
}