From 25c870be4dd26e41bb94c88b9912380fb18459e8 Mon Sep 17 00:00:00 2001 From: francisco souza Date: Sun, 1 Mar 2020 21:18:28 -0500 Subject: [PATCH] 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. --- __tests__/gobin.test.ts | 21 +++++++++++++++++++ __tests__/setup-go.test.ts | 42 +++++++++++++++++++++++--------------- dist/index.js | 42 ++++++++++++++++++++++++++++++++++++++ src/gobin.ts | 13 ++++++++++++ src/main.ts | 5 +++++ 5 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 __tests__/gobin.test.ts create mode 100644 src/gobin.ts diff --git a/__tests__/gobin.test.ts b/__tests__/gobin.test.ts new file mode 100644 index 0000000..8a578a4 --- /dev/null +++ b/__tests__/gobin.test.ts @@ -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'); + }); +}); diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 3d3e150..229188a 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -1,13 +1,10 @@ import * as tc from '@actions/tool-cache'; import * as core from '@actions/core'; -import fs = require('fs'); import osm = require('os'); import path = require('path'); import {run} from '../src/main'; -import * as httpm from '@actions/http-client'; import * as im from '../src/installer'; -import * as sys from '../src/system'; -import {ITypedResponse} from '@actions/http-client/interfaces'; +import * as gobin from '../src/gobin'; let goJsonData = require('./data/golang-dl.json'); @@ -25,6 +22,7 @@ describe('setup-go', () => { let dlSpy: jest.SpyInstance; let exSpy: jest.SpyInstance; let cacheSpy: jest.SpyInstance; + let getGOBINSpy: jest.SpyInstance; beforeEach(() => { // @actions/core @@ -46,15 +44,18 @@ describe('setup-go', () => { cacheSpy = jest.spyOn(tc, 'cacheDir'); getSpy = jest.spyOn(im, 'getVersions'); + // gobin + getGOBINSpy = jest.spyOn(gobin, 'getGOBIN'); + // writes cnSpy = jest.spyOn(process.stdout, 'write'); logSpy = jest.spyOn(console, 'log'); getSpy.mockImplementation(() => goJsonData); - cnSpy.mockImplementation(line => { + cnSpy.mockImplementation(_line => { // uncomment to debug // process.stderr.write('write:' + line + '\n'); }); - logSpy.mockImplementation(line => { + logSpy.mockImplementation(_line => { // uncomment to debug // process.stderr.write('log:' + line + '\n'); }); @@ -181,16 +182,6 @@ describe('setup-go', () => { 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 () => { inputs['go-version'] = '1.13.0'; 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}`); }); + 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 () => { let errMsg = 'unhandled error message'; inputs['go-version'] = '1.13.0'; @@ -265,7 +274,6 @@ describe('setup-go', () => { }); it('reports empty query results', async () => { - let errMsg = 'unhandled download message'; os.platform = 'linux'; os.arch = 'x64'; diff --git a/dist/index.js b/dist/index.js index 21783e6..1a9859a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1279,6 +1279,7 @@ const core = __importStar(__webpack_require__(470)); const tc = __importStar(__webpack_require__(533)); const installer = __importStar(__webpack_require__(749)); const path = __importStar(__webpack_require__(622)); +const gobin = __importStar(__webpack_require__(517)); function run() { return __awaiter(this, void 0, void 0, function* () { try { @@ -1302,6 +1303,9 @@ function run() { core.exportVariable('GOROOT', installDir); core.addPath(path.join(installDir, 'bin')); console.log('Added go to the path'); + const gobinDir = yield gobin.getGOBIN(installDir); + core.exportVariable('GOBIN', gobinDir); + core.addPath(gobinDir); } else { throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`); @@ -3245,6 +3249,44 @@ function getState(name) { exports.getState = getState; //# 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: diff --git a/src/gobin.ts b/src/gobin.ts new file mode 100644 index 0000000..d711cfd --- /dev/null +++ b/src/gobin.ts @@ -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 { + const goExecutable = path.join(installDir, 'bin', 'go'); + + const result = await exec(`${goExecutable} env GOPATH`); + const gopath = result.stdout; + return path.join(gopath, 'bin'); +} diff --git a/src/main.ts b/src/main.ts index 132be9b..6850474 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; import * as installer from './installer'; import * as path from 'path'; +import * as gobin from './gobin'; export async function run() { try { @@ -34,6 +35,10 @@ export async function run() { core.exportVariable('GOROOT', installDir); core.addPath(path.join(installDir, 'bin')); console.log('Added go to the path'); + + const gobinDir = await gobin.getGOBIN(installDir); + core.exportVariable('GOBIN', gobinDir); + core.addPath(gobinDir); } else { throw new Error( `Could not find a version that satisfied version spec: ${versionSpec}`