refactor: enhance error handling

This commit is contained in:
peaceiris 2019-09-21 02:47:29 +09:00
parent 67d5fb9f50
commit 8df3468b4d
4 changed files with 54 additions and 33 deletions

View file

@ -34,8 +34,8 @@ jobs:
needs: test needs: test
strategy: strategy:
matrix: matrix:
os: ['ubuntu-18.04'] # os: ['ubuntu-18.04', 'macOS-10.14']
# os: ['ubuntu-18.04', 'macOS-10.14', 'windows-2019'] os: ['ubuntu-18.04', 'macOS-10.14', 'windows-2019']
# hugo-version: ['latest'] # hugo-version: ['latest']
# extended: [true] # extended: [true]
hugo-version: ['latest', '0.58.2'] hugo-version: ['latest', '0.58.2']

View file

@ -2,14 +2,23 @@ const getURL = require("../lib/get-url");
describe("getURL()", () => { describe("getURL()", () => {
test("test", () => { test("test", () => {
const baseURL = "https://github.com/gohugoio/hugo/releases/download/v0.58.2"; const baseURL =
"https://github.com/gohugoio/hugo/releases/download/v0.58.2";
const urlLinux = `${baseURL}/hugo_0.58.2_Linux-64bit.tar.gz`; const urlLinux = `${baseURL}/hugo_0.58.2_Linux-64bit.tar.gz`;
const urlLinuxExtended = `${baseURL}/hugo_extended_0.58.2_Linux-64bit.tar.gz`; const urlLinuxExtended = `${baseURL}/hugo_extended_0.58.2_Linux-64bit.tar.gz`;
const urlMacOS = `${baseURL}/hugo_0.58.2_macOS-64bit.tar.gz`; const urlMacOS = `${baseURL}/hugo_0.58.2_macOS-64bit.tar.gz`;
const urlWindows = `${baseURL}/hugo_0.58.2_Windows-64bit.zip`; const urlWindows = `${baseURL}/hugo_0.58.2_Windows-64bit.zip`;
expect(getURL("Linux", false, "0.58.2")).toBe(urlLinux); expect(getURL("Linux", false, "0.58.2")).toBe(urlLinux);
expect(getURL("Linux", true, "0.58.2")).toBe(urlLinuxExtended); expect(getURL("Linux", true, "0.58.2")).toBe(urlLinuxExtended);
expect(getURL("Linux", "false", "0.58.2")).toBe(urlLinux);
expect(getURL("Linux", "true", "0.58.2")).toBe(urlLinux);
expect(getURL("macOS", false, "0.58.2")).toBe(urlMacOS); expect(getURL("macOS", false, "0.58.2")).toBe(urlMacOS);
expect(getURL("Windows", false, "0.58.2")).toBe(urlWindows); expect(getURL("Windows", false, "0.58.2")).toBe(urlWindows);
}); });
// test("test exception", () => {
// expect(() => {
// getURL("Linux", "hoge", "0.58.2");
// }).toThrowError("Invalid input (extended): hoge");
// });
}); });

View file

@ -1,17 +1,25 @@
function getURL(os, extended, version) { function getURL(os, extended, version) {
let extendedStr = ""; const extendedStr = extended => {
let ext = "tar.gz";
if (extended === true) { if (extended === true) {
extendedStr = "extended_"; return "extended_";
} else {
return "";
// } else {
// throw new Error(`Invalid input (extended): ${extended}`);
} }
};
const ext = os => {
if (os === "Windows") { if (os === "Windows") {
ext = "zip"; return "zip";
} else {
return "tar.gz";
} }
};
const hugoName = `hugo_${extendedStr}${version}_${os}-64bit`; const hugoName = `hugo_${extendedStr(extended)}${version}_${os}-64bit`;
const url = `https://github.com/gohugoio/hugo/releases/download/v${version}/${hugoName}.${ext}`; const baseURL = "https://github.com/gohugoio/hugo/releases/download";
const url = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
return url; return url;
} }

View file

@ -5,6 +5,7 @@ const getOS = require("./get-os");
const getURL = require("./get-url"); const getURL = require("./get-url");
async function installer(version) { async function installer(version) {
try {
const extended = core.getInput("extended"); const extended = core.getInput("extended");
console.log(`Hugo extended: ${extended}`); console.log(`Hugo extended: ${extended}`);
@ -29,6 +30,9 @@ async function installer(version) {
hugoBin = `${hugoExtractedFolder}/hugo`; hugoBin = `${hugoExtractedFolder}/hugo`;
} }
await io.mv(hugoBin, hugoPath); await io.mv(hugoBin, hugoPath);
} catch (error) {
core.setFailed(error.message);
}
} }
module.exports = installer; module.exports = installer;