refactor: main and installer (#133)

* refactor: installer
* test: Fix workDir cleanup
This commit is contained in:
Shohei Ueda 2020-01-18 12:07:13 +09:00 committed by GitHub
parent 442aa4dbd4
commit 283bc47636
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 47 deletions

View file

@ -2,12 +2,12 @@ import * as main from '../src/main';
import * as io from '@actions/io'; import * as io from '@actions/io';
import path from 'path'; import path from 'path';
import nock from 'nock'; import nock from 'nock';
import {Tool, Action} from '../src/constants';
// import {FetchError} from 'node-fetch'; // import {FetchError} from 'node-fetch';
import jsonTestBrew from './data/brew.json'; import jsonTestBrew from './data/brew.json';
// import jsonTestGithub from './data/github.json'; // import jsonTestGithub from './data/github.json';
jest.setTimeout(30000); jest.setTimeout(30000);
const repo = 'hugo';
beforeEach(() => { beforeEach(() => {
jest.resetModules(); jest.resetModules();
@ -20,11 +20,12 @@ afterEach(() => {
describe('Integration testing run()', () => { describe('Integration testing run()', () => {
afterEach(async () => { 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 () => { test('succeed in installing a custom version', async () => {
const testVersion = '0.61.0'; const testVersion = Tool.TestVersionSpec;
process.env['INPUT_HUGO-VERSION'] = testVersion; process.env['INPUT_HUGO-VERSION'] = testVersion;
const result: main.ActionResult = await main.run(); const result: main.ActionResult = await main.run();
expect(result.exitcode).toBe(0); expect(result.exitcode).toBe(0);
@ -35,11 +36,13 @@ describe('Integration testing run()', () => {
const testVersion = 'latest'; const testVersion = 'latest';
process.env['INPUT_HUGO-VERSION'] = testVersion; process.env['INPUT_HUGO-VERSION'] = testVersion;
nock('https://formulae.brew.sh') nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`) .get(`/api/formula/${Tool.Repo}.json`)
.reply(200, jsonTestBrew); .reply(200, jsonTestBrew);
const result: main.ActionResult = await main.run(); const result: main.ActionResult = await main.run();
expect(result.exitcode).toBe(0); 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}`
);
}); });
}); });

View file

@ -7,3 +7,8 @@ export enum Tool {
TestVersionLatest = '0.62.2', TestVersionLatest = '0.62.2',
TestVersionSpec = '0.61.0' TestVersionSpec = '0.61.0'
} }
export enum Action {
WorkDirName = 'actions_hugo',
TempDirName = '_temp'
}

View file

@ -4,58 +4,72 @@ import * as io from '@actions/io';
import getOS from './get-os'; import getOS from './get-os';
import getURL from './get-url'; import getURL from './get-url';
import * as path from 'path'; 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') { if (process.platform === 'win32') {
baseTempLocation = process.env['USERPROFILE'] || 'C:\\'; homedir = process.env['USERPROFILE'] || 'C:\\';
} else { } 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<string> {
const workDir = path.join(getHomeDir(), Action.WorkDirName);
await io.mkdirP(workDir);
core.debug(`workDir: ${workDir}`);
return workDir;
}
export async function createTempDir(workDir: string): Promise<string> {
const tempDir = path.join(workDir, Action.TempDirName);
await io.mkdirP(tempDir);
core.debug(`tempDir: ${tempDir}`);
return tempDir;
}
export async function createBinDir(workDir: string): Promise<string> {
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<void> { export async function installer(version: string): Promise<void> {
try {
const extended: string = core.getInput('extended'); const extended: string = core.getInput('extended');
console.log(`Hugo extended: ${extended}`); core.debug(`Hugo extended: ${extended}`);
const osName: string = getOS(process.platform); const osName: string = getOS(process.platform);
console.log(`Operating System: ${osName}`); core.debug(`Operating System: ${osName}`);
const hugoURL: string = getURL(osName, extended, version); const toolURL: string = getURL(osName, extended, version);
core.debug(`hugoURL: ${hugoURL}`); core.debug(`toolURL: ${toolURL}`);
let baseLocation: string; const workDir = await createWorkDir();
const binDir = await createBinDir(workDir);
const tempDir = await createTempDir(workDir);
const toolAssets: string = await tc.downloadTool(toolURL);
let toolBin = '';
if (process.platform === 'win32') { if (process.platform === 'win32') {
baseLocation = process.env['USERPROFILE'] || 'C:\\'; const toolExtractedFolder: string = await tc.extractZip(
} else { toolAssets,
baseLocation = `${process.env.HOME}`;
}
const hugoPath: string = path.join(baseLocation, 'hugobin');
await io.mkdirP(hugoPath);
core.addPath(hugoPath);
// 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 tempDir
); );
hugoBin = `${hugoExtractedFolder}/hugo.exe`; toolBin = `${toolExtractedFolder}/${Tool.CmdName}.exe`;
} else { } else {
const hugoExtractedFolder: string = await tc.extractTar( const toolExtractedFolder: string = await tc.extractTar(
hugoAssets, toolAssets,
tempDir tempDir
); );
hugoBin = `${hugoExtractedFolder}/hugo`; toolBin = `${toolExtractedFolder}/${Tool.CmdName}`;
}
await io.mv(hugoBin, hugoPath);
} catch (error) {
core.setFailed(error.message);
} }
await io.mv(toolBin, binDir);
} }