From 265edc1beb822df98f8606cd052a5af00d64271d Mon Sep 17 00:00:00 2001 From: So Jomura Date: Thu, 12 May 2022 17:04:39 +0900 Subject: [PATCH] Add go-version-file option (#62) --- .github/workflows/versions.yml | 16 +++++++++ README.md | 15 ++++++++ __tests__/data/go.mod | 12 +++++++ __tests__/setup-go.test.ts | 66 ++++++++++++++++++++++++++++++++++ action.yml | 2 ++ dist/index.js | 31 ++++++++++++++-- src/installer.ts | 12 +++++++ src/main.ts | 28 ++++++++++++++- 8 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 __tests__/data/go.mod diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 79683ec..12c711e 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -50,6 +50,22 @@ jobs: - name: Verify Go run: go version + go-version-file: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + steps: + - uses: actions/checkout@v3 + - name: Setup Go and check latest + uses: ./ + with: + go-version-file: __tests__/data/go.mod + - name: verify go + run: __tests__/verify-go.sh 1.14 + shell: bash + setup-versions-from-manifest: name: Setup ${{ matrix.go }} ${{ matrix.os }} runs-on: ${{ matrix.os }} diff --git a/README.md b/README.md index 20440a4..64ecf7a 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,21 @@ steps: check-latest: true - run: go run hello.go ``` +## Getting go version from the go.mod file + +The `go-version-file` input accepts a path to a `go.mod` file containing the version of Go to be used by a project. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the latest available patch version sequentially in the runner's directory with the cached tools, in the [version-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers. + +If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used. +> The action will search for the `go.mod` file relative to the repository root + +```yaml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-go@v3 + with: + go-version-file: 'path/to/go.mod' +- run: go version +``` ## Matrix testing diff --git a/__tests__/data/go.mod b/__tests__/data/go.mod new file mode 100644 index 0000000..dea3480 --- /dev/null +++ b/__tests__/data/go.mod @@ -0,0 +1,12 @@ +module example.com/mymodule + +go 1.14 + +require ( + example.com/othermodule v1.2.3 + example.com/thismodule v1.2.3 + example.com/thatmodule v1.2.3 +) + +replace example.com/thatmodule => ../thatmodule +exclude example.com/thismodule v1.3.0 diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index a0c2bea..75cbbe4 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -33,6 +33,7 @@ describe('setup-go', () => { let dbgSpy: jest.SpyInstance; let whichSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance; + let readFileSpy: jest.SpyInstance; let mkdirpSpy: jest.SpyInstance; let execSpy: jest.SpyInstance; let getManifestSpy: jest.SpyInstance; @@ -71,6 +72,7 @@ describe('setup-go', () => { // io whichSpy = jest.spyOn(io, 'which'); existsSpy = jest.spyOn(fs, 'existsSync'); + readFileSpy = jest.spyOn(fs, 'readFileSync'); mkdirpSpy = jest.spyOn(io, 'mkdirP'); // gets @@ -774,4 +776,68 @@ describe('setup-go', () => { expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); }); }); + + describe('go-version-file', () => { + const goModContents = `module example.com/mymodule + +go 1.14 + +require ( + example.com/othermodule v1.2.3 + example.com/thismodule v1.2.3 + example.com/thatmodule v1.2.3 +) + +replace example.com/thatmodule => ../thatmodule +exclude example.com/thismodule v1.3.0 +`; + + it('reads version from go.mod', async () => { + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(path => true); + readFileSpy.mockImplementation(() => Buffer.from(goModContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.14'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.14...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.14...'); + }); + + it('reads version from .go-version', async () => { + inputs['go-version-file'] = '.go-version'; + existsSpy.mockImplementation(path => true); + readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.0'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.0...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.13.0...'); + }); + + it('is overwritten by go-version', async () => { + inputs['go-version'] = '1.13.1'; + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(path => true); + readFileSpy.mockImplementation(() => Buffer.from(goModContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.1'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.1...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.13.1...'); + }); + + it('reports a read failure', async () => { + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(path => false); + + await main.run(); + + expect(cnSpy).toHaveBeenCalledWith( + `::error::The specified go version file at: go.mod does not exist${osm.EOL}` + ); + }); + }); }); diff --git a/action.yml b/action.yml index ab0fdca..40d6621 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,8 @@ author: 'GitHub' inputs: go-version: description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.' + go-version-file: + description: 'Path to the go.mod file.' check-latest: description: 'Set this option to true if you want the action to always check for the latest available version that satisfies the version spec' default: false diff --git a/dist/index.js b/dist/index.js index a6028e8..1de0dc4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2074,7 +2074,7 @@ function run() { // versionSpec is optional. If supplied, install / use from the tool cache // If not supplied then problem matchers will still be setup. Useful for self-hosted. // - let versionSpec = core.getInput('go-version'); + const versionSpec = resolveVersionInput(); core.info(`Setup go version spec ${versionSpec}`); if (versionSpec) { let token = core.getInput('token'); @@ -2154,6 +2154,23 @@ function parseGoVersion(versionString) { return versionString.split(' ')[2].slice('go'.length); } exports.parseGoVersion = parseGoVersion; +function resolveVersionInput() { + let version = core.getInput('go-version'); + const versionFilePath = core.getInput('go-version-file'); + if (version && versionFilePath) { + core.warning('Both go-version and go-version-file inputs are specified, only go-version will be used'); + } + if (version) { + return version; + } + if (versionFilePath) { + if (!fs_1.default.existsSync(versionFilePath)) { + throw new Error(`The specified go version file at: ${versionFilePath} does not exist`); + } + version = installer.parseGoVersionFile(versionFilePath); + } + return version; +} //# sourceMappingURL=main.js.map /***/ }), @@ -5894,13 +5911,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.extractGoArchive = exports.getGo = void 0; +exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.extractGoArchive = exports.getGo = void 0; const tc = __importStar(__webpack_require__(533)); const core = __importStar(__webpack_require__(470)); const path = __importStar(__webpack_require__(622)); const semver = __importStar(__webpack_require__(280)); const httpm = __importStar(__webpack_require__(539)); const sys = __importStar(__webpack_require__(737)); +const fs_1 = __importDefault(__webpack_require__(747)); const os_1 = __importDefault(__webpack_require__(87)); function getGo(versionSpec, checkLatest, auth) { return __awaiter(this, void 0, void 0, function* () { @@ -6119,6 +6137,15 @@ function makeSemver(version) { return fullVersion; } exports.makeSemver = makeSemver; +function parseGoVersionFile(versionFilePath) { + const contents = fs_1.default.readFileSync(versionFilePath).toString(); + if (path.basename(versionFilePath) === 'go.mod') { + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; + } + return contents.trim(); +} +exports.parseGoVersionFile = parseGoVersionFile; //# sourceMappingURL=installer.js.map /***/ }), diff --git a/src/installer.ts b/src/installer.ts index 46eecc3..c1a1e39 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -4,6 +4,7 @@ import * as path from 'path'; import * as semver from 'semver'; import * as httpm from '@actions/http-client'; import * as sys from './system'; +import fs from 'fs'; import os from 'os'; type InstallationType = 'dist' | 'manifest'; @@ -298,3 +299,14 @@ export function makeSemver(version: string): string { } return fullVersion; } + +export function parseGoVersionFile(versionFilePath: string): string { + const contents = fs.readFileSync(versionFilePath).toString(); + + if (path.basename(versionFilePath) === 'go.mod') { + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; + } + + return contents.trim(); +} diff --git a/src/main.ts b/src/main.ts index 2f79add..a44c8f4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,7 +13,7 @@ export async function run() { // versionSpec is optional. If supplied, install / use from the tool cache // If not supplied then problem matchers will still be setup. Useful for self-hosted. // - let versionSpec = core.getInput('go-version'); + const versionSpec = resolveVersionInput(); core.info(`Setup go version spec ${versionSpec}`); @@ -104,3 +104,29 @@ export function parseGoVersion(versionString: string): string { // expecting go for runtime.Version() return versionString.split(' ')[2].slice('go'.length); } + +function resolveVersionInput(): string { + let version = core.getInput('go-version'); + const versionFilePath = core.getInput('go-version-file'); + + if (version && versionFilePath) { + core.warning( + 'Both go-version and go-version-file inputs are specified, only go-version will be used' + ); + } + + if (version) { + return version; + } + + if (versionFilePath) { + if (!fs.existsSync(versionFilePath)) { + throw new Error( + `The specified go version file at: ${versionFilePath} does not exist` + ); + } + version = installer.parseGoVersionFile(versionFilePath); + } + + return version; +}