From 283bc47636b79858da429c7c2f16a97f4ec00638 Mon Sep 17 00:00:00 2001 From: Shohei Ueda <30958501+peaceiris@users.noreply.github.com> Date: Sat, 18 Jan 2020 12:07:13 +0900 Subject: [PATCH] refactor: main and installer (#133) * refactor: installer * test: Fix workDir cleanup --- __tests__/main.test.ts | 13 +++--- src/constants.ts | 5 +++ src/installer.ts | 98 ++++++++++++++++++++++++------------------ 3 files changed, 69 insertions(+), 47 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 311d564..7b95c7a 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -2,12 +2,12 @@ import * as main from '../src/main'; import * as io from '@actions/io'; import path from 'path'; import nock from 'nock'; +import {Tool, Action} from '../src/constants'; // import {FetchError} from 'node-fetch'; import jsonTestBrew from './data/brew.json'; // import jsonTestGithub from './data/github.json'; jest.setTimeout(30000); -const repo = 'hugo'; beforeEach(() => { jest.resetModules(); @@ -20,11 +20,12 @@ afterEach(() => { describe('Integration testing run()', () => { afterEach(async () => { - await io.rmRF(path.join(`${process.env.HOME}`, 'tmp')); + const workDir = path.join(`${process.env.HOME}`, Action.WorkDirName); + await io.rmRF(workDir); }); test('succeed in installing a custom version', async () => { - const testVersion = '0.61.0'; + const testVersion = Tool.TestVersionSpec; process.env['INPUT_HUGO-VERSION'] = testVersion; const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); @@ -35,11 +36,13 @@ describe('Integration testing run()', () => { const testVersion = 'latest'; process.env['INPUT_HUGO-VERSION'] = testVersion; nock('https://formulae.brew.sh') - .get(`/api/formula/${repo}.json`) + .get(`/api/formula/${Tool.Repo}.json`) .reply(200, jsonTestBrew); const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); - expect(result.output).toMatch('Hugo Static Site Generator v0.62.2'); + expect(result.output).toMatch( + `Hugo Static Site Generator v${Tool.TestVersionLatest}` + ); }); }); diff --git a/src/constants.ts b/src/constants.ts index 7078254..b971d5e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -7,3 +7,8 @@ export enum Tool { TestVersionLatest = '0.62.2', TestVersionSpec = '0.61.0' } + +export enum Action { + WorkDirName = 'actions_hugo', + TempDirName = '_temp' +} diff --git a/src/installer.ts b/src/installer.ts index fb9179e..48f53e9 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -4,58 +4,72 @@ import * as io from '@actions/io'; import getOS from './get-os'; import getURL from './get-url'; import * as path from 'path'; +import {Tool, Action} from './constants'; + +export function getHomeDir(): string { + let homedir = ''; -let tempDir: string = process.env['RUNNER_TEMPDIRECTORY'] || ''; -if (!tempDir) { - let baseTempLocation: string; if (process.platform === 'win32') { - baseTempLocation = process.env['USERPROFILE'] || 'C:\\'; + homedir = process.env['USERPROFILE'] || 'C:\\'; } else { - baseTempLocation = `${process.env.HOME}`; + homedir = `${process.env.HOME}`; } - tempDir = path.join(baseTempLocation, 'tmp'); + + core.debug(`homeDir: ${homedir}`); + + return homedir; +} + +export async function createWorkDir(): Promise { + const workDir = path.join(getHomeDir(), Action.WorkDirName); + await io.mkdirP(workDir); + core.debug(`workDir: ${workDir}`); + return workDir; +} + +export async function createTempDir(workDir: string): Promise { + const tempDir = path.join(workDir, Action.TempDirName); + await io.mkdirP(tempDir); + core.debug(`tempDir: ${tempDir}`); + return tempDir; +} + +export async function createBinDir(workDir: string): Promise { + const binDir = path.join(workDir, 'bin'); + await io.mkdirP(binDir); + core.addPath(binDir); + core.debug(`binDir: ${binDir}`); + return binDir; } export async function installer(version: string): Promise { - try { - const extended: string = core.getInput('extended'); - console.log(`Hugo extended: ${extended}`); + const extended: string = core.getInput('extended'); + core.debug(`Hugo extended: ${extended}`); - const osName: string = getOS(process.platform); - console.log(`Operating System: ${osName}`); + const osName: string = getOS(process.platform); + core.debug(`Operating System: ${osName}`); - const hugoURL: string = getURL(osName, extended, version); - core.debug(`hugoURL: ${hugoURL}`); + const toolURL: string = getURL(osName, extended, version); + core.debug(`toolURL: ${toolURL}`); - let baseLocation: string; - if (process.platform === 'win32') { - baseLocation = process.env['USERPROFILE'] || 'C:\\'; - } else { - baseLocation = `${process.env.HOME}`; - } - const hugoPath: string = path.join(baseLocation, 'hugobin'); - await io.mkdirP(hugoPath); - core.addPath(hugoPath); + const workDir = await createWorkDir(); + const binDir = await createBinDir(workDir); + const tempDir = await createTempDir(workDir); - // Download and extract Hugo binary - await io.mkdirP(tempDir); - const hugoAssets: string = await tc.downloadTool(hugoURL); - let hugoBin = ''; - if (osName === 'Windows') { - const hugoExtractedFolder: string = await tc.extractZip( - hugoAssets, - tempDir - ); - hugoBin = `${hugoExtractedFolder}/hugo.exe`; - } else { - const hugoExtractedFolder: string = await tc.extractTar( - hugoAssets, - tempDir - ); - hugoBin = `${hugoExtractedFolder}/hugo`; - } - await io.mv(hugoBin, hugoPath); - } catch (error) { - core.setFailed(error.message); + const toolAssets: string = await tc.downloadTool(toolURL); + let toolBin = ''; + if (process.platform === 'win32') { + const toolExtractedFolder: string = await tc.extractZip( + toolAssets, + tempDir + ); + toolBin = `${toolExtractedFolder}/${Tool.CmdName}.exe`; + } else { + const toolExtractedFolder: string = await tc.extractTar( + toolAssets, + tempDir + ); + toolBin = `${toolExtractedFolder}/${Tool.CmdName}`; } + await io.mv(toolBin, binDir); }