This commit is contained in:
Bryan MacFarlane 2020-02-09 00:29:21 -05:00
parent 4282769cc0
commit dc575ee3b3
7 changed files with 108 additions and 91 deletions

View File

@ -7,7 +7,7 @@ import {run} from '../src/main';
import * as httpm from '@actions/http-client'; import * as httpm from '@actions/http-client';
import * as im from '../src/installer'; import * as im from '../src/installer';
import * as sys from '../src/system'; import * as sys from '../src/system';
import { ITypedResponse } from '@actions/http-client/interfaces'; import {ITypedResponse} from '@actions/http-client/interfaces';
let goJsonData = require('./data/golang-dl.json'); let goJsonData = require('./data/golang-dl.json');
@ -28,12 +28,15 @@ describe('setup-go', () => {
cnSpy = jest.spyOn(process.stdout, 'write'); cnSpy = jest.spyOn(process.stdout, 'write');
platSpy = jest.spyOn(sys, 'getPlatform'); platSpy = jest.spyOn(sys, 'getPlatform');
archSpy = jest.spyOn(sys, 'getArch'); archSpy = jest.spyOn(sys, 'getArch');
dlSpy = jest.spyOn(tc, "downloadTool"); dlSpy = jest.spyOn(tc, 'downloadTool');
exSpy = jest.spyOn(tc, "extractTar"); exSpy = jest.spyOn(tc, 'extractTar');
getSpy = jest.spyOn(http, 'getJson'); getSpy = jest.spyOn(http, 'getJson');
getSpy.mockImplementation(() => <ITypedResponse<im.IGoVersion[]>>{ getSpy.mockImplementation(
result: goJsonData () =>
}); <ITypedResponse<im.IGoVersion[]>>{
result: goJsonData
}
);
cnSpy.mockImplementation(line => { cnSpy.mockImplementation(line => {
// uncomment to debug // uncomment to debug
//process.stderr.write('write2:' + line + '\n'); //process.stderr.write('write2:' + line + '\n');
@ -49,7 +52,7 @@ describe('setup-go', () => {
afterAll(async () => {}, 100000); afterAll(async () => {}, 100000);
it('finds a version of go already in the cache', async () => { it('finds a version of go already in the cache', async () => {
inSpy.mockImplementation(() => '1.13.0') inSpy.mockImplementation(() => '1.13.0');
let toolPath = path.normalize('/cache/go/1.13.0/x64'); let toolPath = path.normalize('/cache/go/1.13.0/x64');
tcSpy.mockImplementation(() => toolPath); tcSpy.mockImplementation(() => toolPath);
await run(); await run();
@ -79,14 +82,16 @@ describe('setup-go', () => {
}); });
it('can mock go versions query', async () => { it('can mock go versions query', async () => {
let r: ITypedResponse<im.IGoVersion[]> = await http.getJson<im.IGoVersion[]>('https://asite.notexist.com/path'); let r: ITypedResponse<im.IGoVersion[]> = await http.getJson<
im.IGoVersion[]
>('https://asite.notexist.com/path');
expect(r).toBeDefined(); expect(r).toBeDefined();
let versions = r.result; let versions = r.result;
expect(versions).toBeDefined(); expect(versions).toBeDefined();
let l:number = versions? versions.length: 0; let l: number = versions ? versions.length : 0;
expect(l).toBe(76); expect(l).toBe(76);
}); });
it('finds stable match for exact version', async () => { it('finds stable match for exact version', async () => {
platSpy.mockImplementation(() => 'linux'); platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'amd64'); archSpy.mockImplementation(() => 'amd64');
@ -97,10 +102,10 @@ describe('setup-go', () => {
expect(match).toBeDefined(); expect(match).toBeDefined();
let version: string = match ? match.version : ''; let version: string = match ? match.version : '';
expect(version).toBe('go1.13.1'); expect(version).toBe('go1.13.1');
let fileName = match ? match.files[0].filename: ''; let fileName = match ? match.files[0].filename : '';
expect(fileName).toBe('go1.13.1.linux-amd64.tar.gz'); expect(fileName).toBe('go1.13.1.linux-amd64.tar.gz');
}); });
it('finds stable match for exact dot zero version', async () => { it('finds stable match for exact dot zero version', async () => {
platSpy.mockImplementation(() => 'linux'); platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'amd64'); archSpy.mockImplementation(() => 'amd64');
@ -110,10 +115,10 @@ describe('setup-go', () => {
expect(match).toBeDefined(); expect(match).toBeDefined();
let version: string = match ? match.version : ''; let version: string = match ? match.version : '';
expect(version).toBe('go1.13'); expect(version).toBe('go1.13');
let fileName = match ? match.files[0].filename: ''; let fileName = match ? match.files[0].filename : '';
expect(fileName).toBe('go1.13.linux-amd64.tar.gz'); expect(fileName).toBe('go1.13.linux-amd64.tar.gz');
}); });
it('finds latest patch version for minor version spec', async () => { it('finds latest patch version for minor version spec', async () => {
platSpy.mockImplementation(() => 'linux'); platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'amd64'); archSpy.mockImplementation(() => 'amd64');
@ -123,10 +128,10 @@ describe('setup-go', () => {
expect(match).toBeDefined(); expect(match).toBeDefined();
let version: string = match ? match.version : ''; let version: string = match ? match.version : '';
expect(version).toBe('go1.13.7'); expect(version).toBe('go1.13.7');
let fileName = match ? match.files[0].filename: ''; let fileName = match ? match.files[0].filename : '';
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz'); expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
}); });
it('finds latest patch version for caret version spec', async () => { it('finds latest patch version for caret version spec', async () => {
platSpy.mockImplementation(() => 'linux'); platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'amd64'); archSpy.mockImplementation(() => 'amd64');
@ -136,10 +141,10 @@ describe('setup-go', () => {
expect(match).toBeDefined(); expect(match).toBeDefined();
let version: string = match ? match.version : ''; let version: string = match ? match.version : '';
expect(version).toBe('go1.13.7'); expect(version).toBe('go1.13.7');
let fileName = match ? match.files[0].filename: ''; let fileName = match ? match.files[0].filename : '';
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz'); expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
}); });
it('finds latest version for major version spec', async () => { it('finds latest version for major version spec', async () => {
platSpy.mockImplementation(() => 'linux'); platSpy.mockImplementation(() => 'linux');
archSpy.mockImplementation(() => 'amd64'); archSpy.mockImplementation(() => 'amd64');
@ -149,7 +154,7 @@ describe('setup-go', () => {
expect(match).toBeDefined(); expect(match).toBeDefined();
let version: string = match ? match.version : ''; let version: string = match ? match.version : '';
expect(version).toBe('go1.13.7'); expect(version).toBe('go1.13.7');
let fileName = match ? match.files[0].filename: ''; let fileName = match ? match.files[0].filename : '';
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz'); expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
}); });
}); });

8
dist/index.js vendored
View File

@ -1287,7 +1287,7 @@ function run() {
// If not supplied then problem matchers will still be setup. Useful for self-hosted. // If not supplied then problem matchers will still be setup. Useful for self-hosted.
// //
let versionSpec = core.getInput('go-version'); let versionSpec = core.getInput('go-version');
let stable = (core.getInput('stable') || '').toUpperCase() == "TRUE"; let stable = (core.getInput('stable') || '').toUpperCase() == 'TRUE';
if (versionSpec) { if (versionSpec) {
let installDir = tc.find('go', versionSpec); let installDir = tc.find('go', versionSpec);
if (!installDir) { if (!installDir) {
@ -4591,8 +4591,9 @@ function downloadGo(versionSpec, stable) {
let downloadUrl = `https://storage.googleapis.com/golang/${match.files[0]}`; let downloadUrl = `https://storage.googleapis.com/golang/${match.files[0]}`;
let downloadPath = yield tc.downloadTool(downloadUrl); let downloadPath = yield tc.downloadTool(downloadUrl);
// extract // extract
let extPath = sys.getPlatform() == 'windows' ? let extPath = sys.getPlatform() == 'windows'
yield tc.extractZip(downloadPath) : yield tc.extractTar(downloadPath); ? yield tc.extractZip(downloadPath)
: yield tc.extractTar(downloadPath);
// extracts with a root folder that matches the fileName downloaded // extracts with a root folder that matches the fileName downloaded
const toolRoot = path.join(extPath, 'go'); const toolRoot = path.join(extPath, 'go');
toolPath = yield tc.cacheDir(toolRoot, 'go', versionSpec); toolPath = yield tc.cacheDir(toolRoot, 'go', versionSpec);
@ -4638,7 +4639,6 @@ function findMatch(versionSpec, stable) {
} }
} }
} }
;
if (match && goFile) { if (match && goFile) {
match.files = [goFile]; match.files = [goFile];
} }

View File

@ -30,8 +30,9 @@ function downloadGo(versionSpec, stable) {
let downloadUrl = `https://storage.googleapis.com/golang/${match.files[0]}`; let downloadUrl = `https://storage.googleapis.com/golang/${match.files[0]}`;
let downloadPath = yield tc.downloadTool(downloadUrl); let downloadPath = yield tc.downloadTool(downloadUrl);
// extract // extract
let extPath = sys.getPlatform() == 'windows' ? let extPath = sys.getPlatform() == 'windows'
yield tc.extractZip(downloadPath) : yield tc.extractTar(downloadPath); ? yield tc.extractZip(downloadPath)
: yield tc.extractTar(downloadPath);
// extracts with a root folder that matches the fileName downloaded // extracts with a root folder that matches the fileName downloaded
const toolRoot = path.join(extPath, 'go'); const toolRoot = path.join(extPath, 'go');
toolPath = yield tc.cacheDir(toolRoot, 'go', versionSpec); toolPath = yield tc.cacheDir(toolRoot, 'go', versionSpec);
@ -77,7 +78,6 @@ function findMatch(versionSpec, stable) {
} }
} }
} }
;
if (match && goFile) { if (match && goFile) {
match.files = [goFile]; match.files = [goFile];
} }

View File

@ -1,10 +1,13 @@
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as httpm from '@actions/http-client' import * as httpm from '@actions/http-client';
import * as sys from './system' import * as sys from './system';
export async function downloadGo(versionSpec: string, stable: boolean): Promise<string | undefined> { export async function downloadGo(
versionSpec: string,
stable: boolean
): Promise<string | undefined> {
let toolPath: string | undefined; let toolPath: string | undefined;
try { try {
@ -16,8 +19,10 @@ export async function downloadGo(versionSpec: string, stable: boolean): Promise<
let downloadPath: string = await tc.downloadTool(downloadUrl); let downloadPath: string = await tc.downloadTool(downloadUrl);
// extract // extract
let extPath: string = sys.getPlatform() == 'windows'? let extPath: string =
await tc.extractZip(downloadPath): await tc.extractTar(downloadPath); sys.getPlatform() == 'windows'
? await tc.extractZip(downloadPath)
: await tc.extractTar(downloadPath);
// extracts with a root folder that matches the fileName downloaded // extracts with a root folder that matches the fileName downloaded
const toolRoot = path.join(extPath, 'go'); const toolRoot = path.join(extPath, 'go');
@ -31,10 +36,10 @@ export async function downloadGo(versionSpec: string, stable: boolean): Promise<
} }
export interface IGoVersionFile { export interface IGoVersionFile {
filename: string, filename: string;
// darwin, linux, windows // darwin, linux, windows
os: string, os: string;
arch: string arch: string;
} }
export interface IGoVersion { export interface IGoVersion {
@ -43,26 +48,31 @@ export interface IGoVersion {
files: IGoVersionFile[]; files: IGoVersionFile[];
} }
export async function findMatch(versionSpec: string, stable: boolean): Promise<IGoVersion | undefined> { export async function findMatch(
versionSpec: string,
stable: boolean
): Promise<IGoVersion | undefined> {
let archFilter = sys.getArch(); let archFilter = sys.getArch();
let platFilter = sys.getPlatform(); let platFilter = sys.getPlatform();
let match: IGoVersion| undefined; let match: IGoVersion | undefined;
const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all'; const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all';
// this returns versions descending so latest is first // this returns versions descending so latest is first
let http: httpm.HttpClient = new httpm.HttpClient('setup-go'); let http: httpm.HttpClient = new httpm.HttpClient('setup-go');
let candidates: IGoVersion[] | null = (await http.getJson<IGoVersion[]>(dlUrl)).result; let candidates: IGoVersion[] | null = (await http.getJson<IGoVersion[]>(
dlUrl
)).result;
if (!candidates) { if (!candidates) {
throw new Error(`golang download url did not return results: ${dlUrl}`); throw new Error(`golang download url did not return results: ${dlUrl}`);
} }
let goFile: IGoVersionFile | undefined; let goFile: IGoVersionFile | undefined;
for (let i=0; i < candidates.length; i++) { for (let i = 0; i < candidates.length; i++) {
let candidate: IGoVersion = candidates[i]; let candidate: IGoVersion = candidates[i];
let version = candidate.version.replace('go', ''); let version = candidate.version.replace('go', '');
// 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0 // 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0
// since a semver of 1.13 would match latest 1.13 // since a semver of 1.13 would match latest 1.13
let parts: string[] = version.split('.'); let parts: string[] = version.split('.');
@ -81,10 +91,10 @@ export async function findMatch(versionSpec: string, stable: boolean): Promise<I
break; break;
} }
} }
}; }
if (match && goFile) { if (match && goFile) {
match.files = [ goFile ]; match.files = [goFile];
} }
return match; return match;

View File

@ -10,22 +10,24 @@ export async function run() {
// If not supplied then problem matchers will still be setup. Useful for self-hosted. // If not supplied then problem matchers will still be setup. Useful for self-hosted.
// //
let versionSpec = core.getInput('go-version'); let versionSpec = core.getInput('go-version');
let stable: boolean = (core.getInput('stable') || '').toUpperCase() == "TRUE"; let stable: boolean =
(core.getInput('stable') || '').toUpperCase() == 'TRUE';
if (versionSpec) { if (versionSpec) {
let installDir: string | undefined = tc.find('go', versionSpec); let installDir: string | undefined = tc.find('go', versionSpec);
if (!installDir) { if (!installDir) {
installDir = await installer.downloadGo(versionSpec, stable); installDir = await installer.downloadGo(versionSpec, stable);
} }
if (installDir) { if (installDir) {
core.exportVariable('GOROOT', installDir); core.exportVariable('GOROOT', installDir);
core.addPath(path.join(installDir, 'bin')); core.addPath(path.join(installDir, 'bin'));
} } else {
else { throw new Error(
throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`); `Could not find a version that satisfied version spec: ${versionSpec}`
} );
}
} }
// add problem matchers // add problem matchers
@ -34,4 +36,4 @@ export async function run() {
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
} }
} }

View File

@ -1,3 +1,3 @@
import {run} from './main' import {run} from './main';
run(); run();

View File

@ -1,37 +1,37 @@
import * as os from 'os'; import * as os from 'os';
export function getPlatform(): string { export function getPlatform(): string {
// darwin and linux match already // darwin and linux match already
// freebsd not supported yet but future proofed. // freebsd not supported yet but future proofed.
// 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32' // 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'
let plat: string = os.platform(); let plat: string = os.platform();
// wants 'darwin', 'freebsd', 'linux', 'windows' // wants 'darwin', 'freebsd', 'linux', 'windows'
if (plat === 'win32') { if (plat === 'win32') {
plat = 'windows'; plat = 'windows';
}
return plat;
} }
export function getArch(): string { return plat;
// 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'. }
let arch: string = os.arch();
export function getArch(): string {
// wants amd64, 386, arm64, armv61, ppc641e, s390x // 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'.
// currently not supported by runner but future proofed mapping let arch: string = os.arch();
switch (arch) {
case 'x64': // wants amd64, 386, arm64, armv61, ppc641e, s390x
arch = 'amd64'; // currently not supported by runner but future proofed mapping
break; switch (arch) {
case 'ppc': case 'x64':
arch = 'ppc64'; arch = 'amd64';
break; break;
case 'x32': case 'ppc':
arch = '386'; arch = 'ppc64';
break; break;
} case 'x32':
arch = '386';
return arch; break;
} }
return arch;
}