mirror of
https://github.com/peaceiris/actions-hugo.git
synced 2026-01-24 10:47:28 +01:00
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:
7
src/constants.ts
Normal file
7
src/constants.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export enum Tool {
|
||||
Name = 'Hugo',
|
||||
Org = 'gohugoio',
|
||||
Repo = 'hugo',
|
||||
CmdName = 'hugo',
|
||||
CmdOptVersion = 'version'
|
||||
}
|
||||
@@ -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') {
|
||||
|
||||
@@ -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') {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
68
src/index.ts
68
src/index.ts
@@ -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();
|
||||
|
||||
@@ -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
65
src/main.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user