From 5cc6b8eb5bf374d4ce25c02ed126bef0e76c8dd8 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 7 May 2020 12:11:19 +0200 Subject: [PATCH] Cleanup code and tests --- README.md | 2 +- __tests__/github.test.ts | 12 ++++++++++-- __tests__/installer.test.ts | 6 ++++-- dist/index.js | 16 ++++++++++------ jest.config.js | 4 ++-- package.json | 2 +- src/installer.ts | 16 +++++++++------- src/main.ts | 2 +- 8 files changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a9a6d96..7b07ade 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Following inputs can be used as `step.with` keys | Name | Type | Default | Description | |---------------|---------|-----------|-------------------------------| -| `version` | String | `latest` | UPX version. Example: `3.95` | +| `version` | String | `latest` | UPX version. Example: `v3.95` | | `file` | String | | File to compress | | `args` | String | | Arguments to pass to UPX | diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index d9b4579..3c131d1 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -1,8 +1,16 @@ import * as github from '../src/github'; describe('github', () => { - it('returns 3.96 GitHub release', async () => { - const release = await github.getRelease('3.96'); + it('returns latest UPX GitHub release', async () => { + const release = await github.getRelease('latest'); + console.log(release); + expect(release).not.toBeNull(); + expect(release?.tag_name).not.toEqual(''); + }); + + it('returns v3.96 GoReleaser GitHub release', async () => { + const release = await github.getRelease('v3.96'); + console.log(release); expect(release).not.toBeNull(); expect(release?.tag_name).toEqual('v3.96'); }); diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 829688a..2e4ac4f 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -2,13 +2,15 @@ import fs = require('fs'); import * as installer from '../src/installer'; describe('installer', () => { - it('acquires 3.95 version of UPX', async () => { - const upx = await installer.getUPX('3.95'); + it('acquires v3.95 version of UPX', async () => { + const upx = await installer.getUPX('v3.95'); + console.log(upx); expect(fs.existsSync(upx)).toBe(true); }, 100000); it('acquires latest version of UPX', async () => { const upx = await installer.getUPX('latest'); + console.log(upx); expect(fs.existsSync(upx)).toBe(true); }, 100000); }); diff --git a/dist/index.js b/dist/index.js index 3354863..ac452b8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1305,7 +1305,6 @@ function run() { } }); } -exports.run = run; run(); //# sourceMappingURL=main.js.map @@ -4620,8 +4619,8 @@ const util = __importStar(__webpack_require__(669)); const github = __importStar(__webpack_require__(824)); const core = __importStar(__webpack_require__(470)); const tc = __importStar(__webpack_require__(533)); -let osPlat = os.platform(); -let osArch = os.arch(); +const osPlat = os.platform(); +const osArch = os.arch(); function getUPX(version) { return __awaiter(this, void 0, void 0, function* () { const release = yield github.getRelease(version); @@ -4629,7 +4628,6 @@ function getUPX(version) { throw new Error(`Cannot find UPX ${version} release`); } const semver = release.tag_name.replace(/^v/, ''); - core.debug(`Semver is ${semver}`); core.info(`✅ UPX version found: ${semver}`); const filename = util.format('%s.%s', getName(semver), osPlat == 'win32' ? 'zip' : 'tar.xz'); const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', semver, filename); @@ -4637,9 +4635,15 @@ function getUPX(version) { const downloadPath = yield tc.downloadTool(downloadUrl); core.debug(`Downloaded to ${downloadPath}`); core.info('📦 Extracting UPX...'); - const extPath = osPlat == 'win32' ? yield tc.extractZip(downloadPath) : yield tc.extractTar(downloadPath, undefined, 'x'); + let extPath; + if (osPlat == 'win32') { + extPath = yield tc.extractZip(downloadPath); + } + else { + extPath = yield tc.extractTar(downloadPath, undefined, 'x'); + } core.debug(`Extracted to ${extPath}`); - const cachePath = yield tc.cacheDir(extPath, 'ghaction-upx', version); + const cachePath = yield tc.cacheDir(extPath, 'ghaction-upx', semver); core.debug(`Cached to ${cachePath}`); const exePath = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx'); core.debug(`Exe path is ${exePath}`); diff --git a/jest.config.js b/jest.config.js index 563d4cc..3871c8d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,5 +7,5 @@ module.exports = { transform: { '^.+\\.ts$': 'ts-jest' }, - verbose: true -} \ No newline at end of file + verbose: false +} diff --git a/package.json b/package.json index af96977..46a9d60 100644 --- a/package.json +++ b/package.json @@ -39,4 +39,4 @@ "typescript": "^3.8.3", "typescript-formatter": "^7.2.2" } -} +} \ No newline at end of file diff --git a/src/installer.ts b/src/installer.ts index ab08108..be90768 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -5,17 +5,15 @@ import * as github from './github'; import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; -let osPlat: string = os.platform(); -let osArch: string = os.arch(); +const osPlat: string = os.platform(); +const osArch: string = os.arch(); export async function getUPX(version: string): Promise { const release: github.GitHubRelease | null = await github.getRelease(version); if (!release) { throw new Error(`Cannot find UPX ${version} release`); } - const semver: string = release.tag_name.replace(/^v/, ''); - core.debug(`Semver is ${semver}`); core.info(`✅ UPX version found: ${semver}`); const filename = util.format('%s.%s', getName(semver), osPlat == 'win32' ? 'zip' : 'tar.xz'); @@ -26,11 +24,15 @@ export async function getUPX(version: string): Promise { core.debug(`Downloaded to ${downloadPath}`); core.info('📦 Extracting UPX...'); - const extPath: string = - osPlat == 'win32' ? await tc.extractZip(downloadPath) : await tc.extractTar(downloadPath, undefined, 'x'); + let extPath: string; + if (osPlat == 'win32') { + extPath = await tc.extractZip(downloadPath); + } else { + extPath = await tc.extractTar(downloadPath, undefined, 'x'); + } core.debug(`Extracted to ${extPath}`); - const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', version); + const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', semver); core.debug(`Cached to ${cachePath}`); const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx'); diff --git a/src/main.ts b/src/main.ts index f5753de..e196865 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import * as installer from './installer'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; -export async function run() { +async function run(): Promise { try { if (os.platform() == 'darwin') { core.setFailed('Not supported on darwin platform');