3
0
Fork 0
mirror of https://gitea.com/actions/setup-go.git synced 2024-11-25 19:19:33 +01:00

CR feedback

This commit is contained in:
Bryan MacFarlane 2020-02-10 15:21:04 -05:00
parent 7ea80d8e5f
commit 43880314e9
4 changed files with 95 additions and 42 deletions

View file

@ -1,7 +1,7 @@
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import fs = require('fs'); import fs = require('fs');
import os = require('os'); import osm = require('os');
import path = require('path'); import path = require('path');
import {run} from '../src/main'; import {run} from '../src/main';
import * as httpm from '@actions/http-client'; import * as httpm from '@actions/http-client';
@ -12,9 +12,13 @@ import {ITypedResponse} from '@actions/http-client/interfaces';
let goJsonData = require('./data/golang-dl.json'); let goJsonData = require('./data/golang-dl.json');
describe('setup-go', () => { describe('setup-go', () => {
let inputs = {} as any;
let os = {} as any;
let inSpy: jest.SpyInstance; let inSpy: jest.SpyInstance;
let findSpy: jest.SpyInstance; let findSpy: jest.SpyInstance;
let cnSpy: jest.SpyInstance; let cnSpy: jest.SpyInstance;
let logSpy: jest.SpyInstance;
let getSpy: jest.SpyInstance; let getSpy: jest.SpyInstance;
let platSpy: jest.SpyInstance; let platSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance; let archSpy: jest.SpyInstance;
@ -23,19 +27,36 @@ describe('setup-go', () => {
let cacheSpy: jest.SpyInstance; let cacheSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
findSpy = jest.spyOn(tc, 'find'); // @actions/core
inputs = {};
inSpy = jest.spyOn(core, 'getInput'); inSpy = jest.spyOn(core, 'getInput');
cnSpy = jest.spyOn(process.stdout, 'write'); inSpy.mockImplementation(name => inputs[name]);
platSpy = jest.spyOn(os, 'platform');
archSpy = jest.spyOn(os, 'arch'); // node 'os'
os = {};
platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => os['platform']);
archSpy = jest.spyOn(osm, 'arch');
archSpy.mockImplementation(() => os['arch']);
// @actions/tool-cache
findSpy = jest.spyOn(tc, 'find');
dlSpy = jest.spyOn(tc, 'downloadTool'); dlSpy = jest.spyOn(tc, 'downloadTool');
exSpy = jest.spyOn(tc, 'extractTar'); exSpy = jest.spyOn(tc, 'extractTar');
cacheSpy = jest.spyOn(tc, 'cacheDir'); cacheSpy = jest.spyOn(tc, 'cacheDir');
getSpy = jest.spyOn(im, 'getVersions'); getSpy = jest.spyOn(im, 'getVersions');
// writes
cnSpy = jest.spyOn(process.stdout, 'write');
logSpy = jest.spyOn(console, 'log');
getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData); getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData);
cnSpy.mockImplementation(line => { cnSpy.mockImplementation(line => {
// uncomment to debug // uncomment to debug
// process.stderr.write('write2:' + line + '\n'); // process.stderr.write('write:' + line + '\n');
});
logSpy.mockImplementation(line => {
// uncomment to debug
// process.stderr.write('log:' + line + '\n');
}); });
}); });
@ -47,8 +68,8 @@ describe('setup-go', () => {
afterAll(async () => {}, 100000); afterAll(async () => {}, 100000);
it('finds stable match for exact dot zero version', async () => { it('finds stable match for exact dot zero version', async () => {
platSpy.mockImplementation(() => 'darwin'); os.platform = 'darwin';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
// spec: 1.13.0 => 1.13 // spec: 1.13.0 => 1.13
let match: im.IGoVersion | undefined = await im.findMatch('1.13.0', true); let match: im.IGoVersion | undefined = await im.findMatch('1.13.0', true);
@ -60,9 +81,8 @@ describe('setup-go', () => {
}); });
it('finds latest patch version for minor version spec', async () => { it('finds latest patch version for minor version spec', async () => {
platSpy.mockImplementation(() => 'linux'); os.platform = 'linux';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
core.debug('plat mocks ok');
// spec: 1.13 => 1.13.7 (latest) // spec: 1.13 => 1.13.7 (latest)
let match: im.IGoVersion | undefined = await im.findMatch('1.13', true); let match: im.IGoVersion | undefined = await im.findMatch('1.13', true);
@ -74,8 +94,8 @@ describe('setup-go', () => {
}); });
it('finds latest patch version for caret version spec', async () => { it('finds latest patch version for caret version spec', async () => {
platSpy.mockImplementation(() => 'linux'); os.platform = 'linux';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
// spec: ^1.13.6 => 1.13.7 // spec: ^1.13.6 => 1.13.7
let match: im.IGoVersion | undefined = await im.findMatch('^1.13.6', true); let match: im.IGoVersion | undefined = await im.findMatch('^1.13.6', true);
@ -87,8 +107,8 @@ describe('setup-go', () => {
}); });
it('finds latest version for major version spec', async () => { it('finds latest version for major version spec', async () => {
platSpy.mockImplementation(() => 'windows'); os.platform = 'win32';
archSpy.mockImplementation(() => 'x32'); os.arch = 'x32';
// spec: 1 => 1.13.7 (latest) // spec: 1 => 1.13.7 (latest)
let match: im.IGoVersion | undefined = await im.findMatch('1', true); let match: im.IGoVersion | undefined = await im.findMatch('1', true);
@ -99,8 +119,32 @@ describe('setup-go', () => {
expect(fileName).toBe('go1.13.7.windows-386.zip'); expect(fileName).toBe('go1.13.7.windows-386.zip');
}); });
it('evaluates to stable with input as true', async () => {
inputs['go-version'] = '1.13.0';
inputs.stable = 'true';
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
});
it('evaluates to stable with no input', async () => {
inputs['go-version'] = '1.13.0';
inSpy.mockImplementation(name => inputs[name]);
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
});
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'); inputs['go-version'] = '1.13.0';
let toolPath = path.normalize('/cache/go/1.13.0/x64'); let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath); findSpy.mockImplementation(() => toolPath);
await run(); await run();
@ -109,30 +153,32 @@ describe('setup-go', () => {
}); });
it('finds a version in the cache and adds it to the path', async () => { it('finds a version in the cache and adds it to the path', async () => {
inputs['go-version'] = '1.13.0';
let toolPath = path.normalize('/cache/go/1.13.0/x64'); let toolPath = path.normalize('/cache/go/1.13.0/x64');
inSpy.mockImplementation(() => '1.13.0');
findSpy.mockImplementation(() => toolPath); findSpy.mockImplementation(() => toolPath);
await run(); await run();
let expPath = path.join(toolPath, 'bin'); let expPath = path.join(toolPath, 'bin');
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`); expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
}); });
it('handles unhandled error and reports error', async () => { it('handles unhandled error and reports error', async () => {
let errMsg = 'unhandled error message'; let errMsg = 'unhandled error message';
inSpy.mockImplementation(() => '1.13.0'); inputs['go-version'] = '1.13.0';
findSpy.mockImplementation(() => { findSpy.mockImplementation(() => {
throw new Error(errMsg); throw new Error(errMsg);
}); });
await run(); await run();
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + os.EOL); expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + osm.EOL);
}); });
it('downloads a version not in the cache', async () => { it('downloads a version not in the cache', async () => {
platSpy.mockImplementation(() => 'linux'); os.platform = 'linux';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
inputs['go-version'] = '1.13.1';
inSpy.mockImplementation(() => '1.13.1');
findSpy.mockImplementation(() => ''); findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(() => '/some/temp/path'); dlSpy.mockImplementation(() => '/some/temp/path');
let toolPath = path.normalize('/cache/go/1.13.0/x64'); let toolPath = path.normalize('/cache/go/1.13.0/x64');
@ -144,28 +190,30 @@ describe('setup-go', () => {
expect(dlSpy).toHaveBeenCalled(); expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled(); expect(exSpy).toHaveBeenCalled();
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${os.EOL}`); expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
}); });
it('does not find a version that does not exist', async () => { it('does not find a version that does not exist', async () => {
platSpy.mockImplementation(() => 'linux'); os.platform = 'linux';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
inputs['go-version'] = '9.99.9';
inSpy.mockImplementation(() => '9.99.9');
findSpy.mockImplementation(() => ''); findSpy.mockImplementation(() => '');
await run(); await run();
expect(cnSpy).toHaveBeenCalledWith( expect(cnSpy).toHaveBeenCalledWith(
`::error::Could not find a version that satisfied version spec: 9.99.9${os.EOL}` `::error::Could not find a version that satisfied version spec: 9.99.9${osm.EOL}`
); );
}); });
it('reports a failed download', async () => { it('reports a failed download', async () => {
let errMsg = 'unhandled download message'; let errMsg = 'unhandled download message';
platSpy.mockImplementation(() => 'linux'); os.platform = 'linux';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
inputs['go-version'] = '1.13.1';
inSpy.mockImplementation(() => '1.13.1');
findSpy.mockImplementation(() => ''); findSpy.mockImplementation(() => '');
dlSpy.mockImplementation(() => { dlSpy.mockImplementation(() => {
throw new Error(errMsg); throw new Error(errMsg);
@ -173,22 +221,23 @@ describe('setup-go', () => {
await run(); await run();
expect(cnSpy).toHaveBeenCalledWith( expect(cnSpy).toHaveBeenCalledWith(
`::error::Failed to download version 1.13.1: Error: ${errMsg}${os.EOL}` `::error::Failed to download version 1.13.1: Error: ${errMsg}${osm.EOL}`
); );
}); });
it('reports empty query results', async () => { it('reports empty query results', async () => {
let errMsg = 'unhandled download message'; let errMsg = 'unhandled download message';
platSpy.mockImplementation(() => 'linux'); os.platform = 'linux';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
inputs['go-version'] = '1.13.1';
inSpy.mockImplementation(() => '1.13.1');
findSpy.mockImplementation(() => ''); findSpy.mockImplementation(() => '');
getSpy.mockImplementation(() => null); getSpy.mockImplementation(() => null);
await run(); await run();
expect(cnSpy).toHaveBeenCalledWith( expect(cnSpy).toHaveBeenCalledWith(
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${os.EOL}` `::error::Failed to download version 1.13.1: Error: golang download url did not return results${osm.EOL}`
); );
}); });
@ -202,8 +251,8 @@ describe('setup-go', () => {
}); });
it('finds stable match for exact version', async () => { it('finds stable match for exact version', async () => {
platSpy.mockImplementation(() => 'win32'); os.platform = 'win32';
archSpy.mockImplementation(() => 'x64'); os.arch = 'x64';
// get request is already mocked // get request is already mocked
// spec: 1.13.7 => 1.13.7 (exact) // spec: 1.13.7 => 1.13.7 (exact)

5
dist/index.js vendored
View file

@ -1289,7 +1289,7 @@ function run() {
let versionSpec = core.getInput('go-version'); let versionSpec = core.getInput('go-version');
// stable will be true unless false is the exact input // stable will be true unless false is the exact input
// since getting unstable versions should be explicit // since getting unstable versions should be explicit
let stable = Boolean(core.getInput('stable') || 'true'); let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
console.log(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`); console.log(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
if (versionSpec) { if (versionSpec) {
let installDir = tc.find('go', versionSpec); let installDir = tc.find('go', versionSpec);
@ -4633,7 +4633,8 @@ function findMatch(versionSpec, stable) {
version = version + '.0'; version = version + '.0';
} }
core_1.debug(`check ${version} satisfies ${versionSpec}`); core_1.debug(`check ${version} satisfies ${versionSpec}`);
if (semver.satisfies(version, versionSpec) && candidate.stable == stable) { if (semver.satisfies(version, versionSpec) &&
(!stable || candidate.stable === stable)) {
goFile = candidate.files.find(file => { goFile = candidate.files.find(file => {
core_1.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`); core_1.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
return file.arch === archFilter && file.os === platFilter; return file.arch === archFilter && file.os === platFilter;

View file

@ -84,7 +84,10 @@ export async function findMatch(
} }
debug(`check ${version} satisfies ${versionSpec}`); debug(`check ${version} satisfies ${versionSpec}`);
if (semver.satisfies(version, versionSpec) && candidate.stable == stable) { if (
semver.satisfies(version, versionSpec) &&
(!stable || candidate.stable === stable)
) {
goFile = candidate.files.find(file => { goFile = candidate.files.find(file => {
debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`); debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
return file.arch === archFilter && file.os === platFilter; return file.arch === archFilter && file.os === platFilter;

View file

@ -13,7 +13,7 @@ export async function run() {
// stable will be true unless false is the exact input // stable will be true unless false is the exact input
// since getting unstable versions should be explicit // since getting unstable versions should be explicit
let stable = Boolean(core.getInput('stable') || 'true'); let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
console.log( console.log(
`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}` `Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`