Add $GOBIN

- Set $GOBIN to $(go env GOPATH)/bin
- Add $GOBIN to the PATH

Should make the setup of tools like golangci-lint or golint work with a
simple `go get`.

Using $GOBIN instead of $GOPATH/bin because the goal is to have GOPATH
not being directly referenced. Also, in the future, GOBIN will have a
default value too, so we would not need to manually set it, just add it
to the path (see discussion in golang/go#23439).

Closes #14.
This commit is contained in:
francisco souza 2020-03-01 21:18:28 -05:00
parent 2096a2c66a
commit 25c870be4d
No known key found for this signature in database
GPG key ID: 3F6AA3B701F20B3E
5 changed files with 106 additions and 17 deletions

21
__tests__/gobin.test.ts Normal file
View file

@ -0,0 +1,21 @@
import * as gobin from '../src/gobin';
jest.mock('child_process');
describe('gobin', () => {
const childProcess = require('child_process');
let execSpy: jest.SpyInstance;
beforeEach(() => {
execSpy = jest.spyOn(childProcess, 'exec');
execSpy.mockImplementation((_command, callback) => {
callback('', {stdout: '/home/user/go', stderr: ''});
});
});
it('should return ${GOPATH}/bin', async () => {
const gobinPath = await gobin.getGOBIN('...');
expect(gobinPath).toBe('/home/user/go/bin');
});
});

View file

@ -1,13 +1,10 @@
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 osm = 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 im from '../src/installer'; import * as im from '../src/installer';
import * as sys from '../src/system'; import * as gobin from '../src/gobin';
import {ITypedResponse} from '@actions/http-client/interfaces';
let goJsonData = require('./data/golang-dl.json'); let goJsonData = require('./data/golang-dl.json');
@ -25,6 +22,7 @@ describe('setup-go', () => {
let dlSpy: jest.SpyInstance; let dlSpy: jest.SpyInstance;
let exSpy: jest.SpyInstance; let exSpy: jest.SpyInstance;
let cacheSpy: jest.SpyInstance; let cacheSpy: jest.SpyInstance;
let getGOBINSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
// @actions/core // @actions/core
@ -46,15 +44,18 @@ describe('setup-go', () => {
cacheSpy = jest.spyOn(tc, 'cacheDir'); cacheSpy = jest.spyOn(tc, 'cacheDir');
getSpy = jest.spyOn(im, 'getVersions'); getSpy = jest.spyOn(im, 'getVersions');
// gobin
getGOBINSpy = jest.spyOn(gobin, 'getGOBIN');
// writes // writes
cnSpy = jest.spyOn(process.stdout, 'write'); cnSpy = jest.spyOn(process.stdout, 'write');
logSpy = jest.spyOn(console, 'log'); 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('write:' + line + '\n'); // process.stderr.write('write:' + line + '\n');
}); });
logSpy.mockImplementation(line => { logSpy.mockImplementation(_line => {
// uncomment to debug // uncomment to debug
// process.stderr.write('log:' + line + '\n'); // process.stderr.write('log:' + line + '\n');
}); });
@ -181,16 +182,6 @@ describe('setup-go', () => {
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`); expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
}); });
it('finds a version of go already in the cache', async () => {
inputs['go-version'] = '1.13.0';
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
let expPath = path.join(toolPath, 'bin');
});
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'; 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');
@ -201,6 +192,24 @@ describe('setup-go', () => {
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
}); });
it('finds a version in the cache, sets GOBIN and adds it to the path', async () => {
inputs['go-version'] = '1.13.0';
let goroot = '/cache/go/1.13.0/x64';
let gorootBin = path.join(goroot, 'bin');
findSpy.mockImplementation(() => goroot);
let gobinPath = '/home/user/go/bin';
getGOBINSpy.mockImplementation(() => gobinPath);
await run();
expect(cnSpy.mock.calls).toEqual([
[`::set-env name=GOROOT::${goroot}${osm.EOL}`],
[`::add-path::${gorootBin}${osm.EOL}`],
[`::set-env name=GOBIN::${gobinPath}${osm.EOL}`],
[`::add-path::${gobinPath}${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';
inputs['go-version'] = '1.13.0'; inputs['go-version'] = '1.13.0';
@ -265,7 +274,6 @@ describe('setup-go', () => {
}); });
it('reports empty query results', async () => { it('reports empty query results', async () => {
let errMsg = 'unhandled download message';
os.platform = 'linux'; os.platform = 'linux';
os.arch = 'x64'; os.arch = 'x64';

42
dist/index.js vendored
View file

@ -1279,6 +1279,7 @@ const core = __importStar(__webpack_require__(470));
const tc = __importStar(__webpack_require__(533)); const tc = __importStar(__webpack_require__(533));
const installer = __importStar(__webpack_require__(749)); const installer = __importStar(__webpack_require__(749));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const gobin = __importStar(__webpack_require__(517));
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
@ -1302,6 +1303,9 @@ function run() {
core.exportVariable('GOROOT', installDir); core.exportVariable('GOROOT', installDir);
core.addPath(path.join(installDir, 'bin')); core.addPath(path.join(installDir, 'bin'));
console.log('Added go to the path'); console.log('Added go to the path');
const gobinDir = yield gobin.getGOBIN(installDir);
core.exportVariable('GOBIN', gobinDir);
core.addPath(gobinDir);
} }
else { else {
throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`); throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`);
@ -3245,6 +3249,44 @@ function getState(name) {
exports.getState = getState; exports.getState = getState;
//# sourceMappingURL=core.js.map //# sourceMappingURL=core.js.map
/***/ }),
/***/ 517:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const childProcess = __importStar(__webpack_require__(129));
const path = __importStar(__webpack_require__(622));
const util_1 = __webpack_require__(669);
const exec = util_1.promisify(childProcess.exec);
function getGOBIN(installDir) {
return __awaiter(this, void 0, void 0, function* () {
const goExecutable = path.join(installDir, 'bin', 'go');
const result = yield exec(`${goExecutable} env GOPATH`);
const gopath = result.stdout;
return path.join(gopath, 'bin');
});
}
exports.getGOBIN = getGOBIN;
/***/ }), /***/ }),
/***/ 533: /***/ 533:

13
src/gobin.ts Normal file
View file

@ -0,0 +1,13 @@
import * as childProcess from 'child_process';
import * as path from 'path';
import {promisify} from 'util';
const exec = promisify(childProcess.exec);
export async function getGOBIN(installDir: string): Promise<string> {
const goExecutable = path.join(installDir, 'bin', 'go');
const result = await exec(`${goExecutable} env GOPATH`);
const gopath = result.stdout;
return path.join(gopath, 'bin');
}

View file

@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as installer from './installer'; import * as installer from './installer';
import * as path from 'path'; import * as path from 'path';
import * as gobin from './gobin';
export async function run() { export async function run() {
try { try {
@ -34,6 +35,10 @@ export async function run() {
core.exportVariable('GOROOT', installDir); core.exportVariable('GOROOT', installDir);
core.addPath(path.join(installDir, 'bin')); core.addPath(path.join(installDir, 'bin'));
console.log('Added go to the path'); console.log('Added go to the path');
const gobinDir = await gobin.getGOBIN(installDir);
core.exportVariable('GOBIN', gobinDir);
core.addPath(gobinDir);
} 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}`