From 1d577b5f5004c2a14e48b6e05e5ed15f1a5a03d2 Mon Sep 17 00:00:00 2001 From: Alif Rachmawadi Date: Sat, 17 Aug 2019 01:31:27 +0700 Subject: [PATCH] more .x handling --- __tests__/installer.test.ts | 40 +++++++++++++++++++++++++++++++++---- lib/installer.js | 29 ++++++++++++++++++++++----- src/installer.ts | 29 ++++++++++++++++++++++----- 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 963c0b1..00933c4 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -30,8 +30,8 @@ describe('installer tests', () => { }, 100000); it('Acquires version of go if no matching version is installed', async () => { - await installer.getGo('1.10'); - const goDir = path.join(toolDir, 'go', '1.10.0', os.arch()); + await installer.getGo('1.10.8'); + const goDir = path.join(toolDir, 'go', '1.10.8', os.arch()); expect(fs.existsSync(`${goDir}.complete`)).toBe(true); if (IS_WINDOWS) { @@ -41,7 +41,23 @@ describe('installer tests', () => { } }, 100000); - it('Acquires latest release version of go if using .x syntax and no matching version is installed', async () => { + it('Acquires latest release version of go 1.10 if using 1.10 and no matching version is installed', async () => { + nock('https://api.github.com') + .get('/repos/golang/go/git/refs/tags') + .replyWithFile(200, path.join(dataDir, 'golang-tags.json')); + + await installer.getGo('1.10'); + const goDir = path.join(toolDir, 'go', '1.10.8', os.arch()); + + expect(fs.existsSync(`${goDir}.complete`)).toBe(true); + if (IS_WINDOWS) { + expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true); + } else { + expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true); + } + }, 100000); + + it('Acquires latest release version of go 1.10 if using 1.10.x and no matching version is installed', async () => { nock('https://api.github.com') .get('/repos/golang/go/git/refs/tags') .replyWithFile(200, path.join(dataDir, 'golang-tags.json')); @@ -57,6 +73,22 @@ describe('installer tests', () => { } }, 100000); + it('Acquires latest release version of go if using 1.x and no matching version is installed', async () => { + nock('https://api.github.com') + .get('/repos/golang/go/git/refs/tags') + .replyWithFile(200, path.join(dataDir, 'golang-tags.json')); + + await installer.getGo('1.x'); + const goDir = path.join(toolDir, 'go', '1.13.0-beta1', os.arch()); + + expect(fs.existsSync(`${goDir}.complete`)).toBe(true); + if (IS_WINDOWS) { + expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true); + } else { + expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true); + } + }, 100000); + it('Throws if no location contains correct go version', async () => { let thrown = false; try { @@ -72,7 +104,7 @@ describe('installer tests', () => { await io.mkdirP(goDir); fs.writeFileSync(`${goDir}.complete`, 'hello'); // This will throw if it doesn't find it in the cache (because no such version exists) - await installer.getGo('250.0'); + await installer.getGo('250.0.0'); return; }); diff --git a/lib/installer.js b/lib/installer.js index 230eec6..b585d9e 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -44,7 +44,10 @@ if (!tempDirectory) { } function getGo(version) { return __awaiter(this, void 0, void 0, function* () { - version = yield determineVersion(version); + const selected = yield determineVersion(version); + if (selected) { + version = selected; + } // check cache let toolPath; toolPath = tc.find('go', normalizeVersion(version)); @@ -129,7 +132,7 @@ function normalizeVersion(version) { //append minor and patch version if not available return version.concat('.0.0'); } - else if (versionPart[2] == null) { + else { // handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) { versionPart[1] = versionPart[1] @@ -137,15 +140,32 @@ function normalizeVersion(version) { .replace('rc', '.0-rc'); return versionPart.join('.'); } + } + if (versionPart[2] == null) { //append patch version if not available return version.concat('.0'); } + else { + // handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 + if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) { + versionPart[2] = versionPart[2] + .replace('beta', '-beta') + .replace('rc', '-rc'); + return versionPart.join('.'); + } + } return version; } function determineVersion(version) { return __awaiter(this, void 0, void 0, function* () { if (!version.endsWith('.x')) { - return version; + const versionPart = version.split('.'); + if (versionPart[1] == null || versionPart[2] == null) { + return yield getLatestVersion(version.concat('.x')); + } + else { + return version; + } } return yield getLatestVersion(version); }); @@ -157,8 +177,7 @@ function getLatestVersion(version) { const versions = yield getPossibleVersions(trimmedVersion); core.debug(`evaluating ${versions.length} versions`); if (version.length === 0) { - core.debug('match not found'); - return trimmedVersion; + throw new Error('unable to get latest version'); } core.debug(`matched: ${versions[0]}`); return versions[0]; diff --git a/src/installer.ts b/src/installer.ts index b007545..acafe9f 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -27,7 +27,10 @@ if (!tempDirectory) { } export async function getGo(version: string) { - version = await determineVersion(version); + const selected = await determineVersion(version); + if (selected) { + version = selected; + } // check cache let toolPath: string; @@ -126,7 +129,7 @@ function normalizeVersion(version: string): string { if (versionPart[1] == null) { //append minor and patch version if not available return version.concat('.0.0'); - } else if (versionPart[2] == null) { + } else { // handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) { versionPart[1] = versionPart[1] @@ -134,16 +137,33 @@ function normalizeVersion(version: string): string { .replace('rc', '.0-rc'); return versionPart.join('.'); } + } + if (versionPart[2] == null) { //append patch version if not available return version.concat('.0'); + } else { + // handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 + if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) { + versionPart[2] = versionPart[2] + .replace('beta', '-beta') + .replace('rc', '-rc'); + return versionPart.join('.'); + } } + return version; } async function determineVersion(version: string): Promise { if (!version.endsWith('.x')) { - return version; + const versionPart = version.split('.'); + + if (versionPart[1] == null || versionPart[2] == null) { + return await getLatestVersion(version.concat('.x')); + } else { + return version; + } } return await getLatestVersion(version); @@ -158,8 +178,7 @@ async function getLatestVersion(version: string): Promise { core.debug(`evaluating ${versions.length} versions`); if (version.length === 0) { - core.debug('match not found'); - return trimmedVersion; + throw new Error('unable to get latest version'); } core.debug(`matched: ${versions[0]}`);