refactor: main and installer (#133)
* refactor: installer * test: Fix workDir cleanup
This commit is contained in:
parent
442aa4dbd4
commit
283bc47636
3 changed files with 69 additions and 47 deletions
|
@ -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}`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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'
|
||||||
|
}
|
||||||
|
|
|
@ -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');
|
core.debug(`Hugo extended: ${extended}`);
|
||||||
console.log(`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();
|
||||||
if (process.platform === 'win32') {
|
const binDir = await createBinDir(workDir);
|
||||||
baseLocation = process.env['USERPROFILE'] || 'C:\\';
|
const tempDir = await createTempDir(workDir);
|
||||||
} else {
|
|
||||||
baseLocation = `${process.env.HOME}`;
|
|
||||||
}
|
|
||||||
const hugoPath: string = path.join(baseLocation, 'hugobin');
|
|
||||||
await io.mkdirP(hugoPath);
|
|
||||||
core.addPath(hugoPath);
|
|
||||||
|
|
||||||
// Download and extract Hugo binary
|
const toolAssets: string = await tc.downloadTool(toolURL);
|
||||||
await io.mkdirP(tempDir);
|
let toolBin = '';
|
||||||
const hugoAssets: string = await tc.downloadTool(hugoURL);
|
if (process.platform === 'win32') {
|
||||||
let hugoBin = '';
|
const toolExtractedFolder: string = await tc.extractZip(
|
||||||
if (osName === 'Windows') {
|
toolAssets,
|
||||||
const hugoExtractedFolder: string = await tc.extractZip(
|
tempDir
|
||||||
hugoAssets,
|
);
|
||||||
tempDir
|
toolBin = `${toolExtractedFolder}/${Tool.CmdName}.exe`;
|
||||||
);
|
} else {
|
||||||
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
|
const toolExtractedFolder: string = await tc.extractTar(
|
||||||
} else {
|
toolAssets,
|
||||||
const hugoExtractedFolder: string = await tc.extractTar(
|
tempDir
|
||||||
hugoAssets,
|
);
|
||||||
tempDir
|
toolBin = `${toolExtractedFolder}/${Tool.CmdName}`;
|
||||||
);
|
|
||||||
hugoBin = `${hugoExtractedFolder}/hugo`;
|
|
||||||
}
|
|
||||||
await io.mv(hugoBin, hugoPath);
|
|
||||||
} catch (error) {
|
|
||||||
core.setFailed(error.message);
|
|
||||||
}
|
}
|
||||||
|
await io.mv(toolBin, binDir);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue