3
0
Fork 0
mirror of https://gitea.com/actions/setup-go.git synced 2024-11-22 01:29:32 +01:00

Add go-version-file option (#62)

This commit is contained in:
So Jomura 2022-05-12 17:04:39 +09:00 committed by GitHub
parent 193b404f8a
commit 265edc1beb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 179 additions and 3 deletions

View file

@ -50,6 +50,22 @@ jobs:
- name: Verify Go - name: Verify Go
run: go version 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: setup-versions-from-manifest:
name: Setup ${{ matrix.go }} ${{ matrix.os }} name: Setup ${{ matrix.go }} ${{ matrix.os }}
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}

View file

@ -94,6 +94,21 @@ steps:
check-latest: true check-latest: true
- run: go run hello.go - 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 ## Matrix testing

12
__tests__/data/go.mod Normal file
View file

@ -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

View file

@ -33,6 +33,7 @@ describe('setup-go', () => {
let dbgSpy: jest.SpyInstance; let dbgSpy: jest.SpyInstance;
let whichSpy: jest.SpyInstance; let whichSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance;
let readFileSpy: jest.SpyInstance;
let mkdirpSpy: jest.SpyInstance; let mkdirpSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance; let execSpy: jest.SpyInstance;
let getManifestSpy: jest.SpyInstance; let getManifestSpy: jest.SpyInstance;
@ -71,6 +72,7 @@ describe('setup-go', () => {
// io // io
whichSpy = jest.spyOn(io, 'which'); whichSpy = jest.spyOn(io, 'which');
existsSpy = jest.spyOn(fs, 'existsSync'); existsSpy = jest.spyOn(fs, 'existsSync');
readFileSpy = jest.spyOn(fs, 'readFileSync');
mkdirpSpy = jest.spyOn(io, 'mkdirP'); mkdirpSpy = jest.spyOn(io, 'mkdirP');
// gets // gets
@ -774,4 +776,68 @@ describe('setup-go', () => {
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); 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}`
);
});
});
}); });

View file

@ -4,6 +4,8 @@ author: 'GitHub'
inputs: inputs:
go-version: go-version:
description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.' 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: 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' 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 default: false

31
dist/index.js vendored
View file

@ -2074,7 +2074,7 @@ function run() {
// versionSpec is optional. If supplied, install / use from the tool cache // 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. // 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}`); core.info(`Setup go version spec ${versionSpec}`);
if (versionSpec) { if (versionSpec) {
let token = core.getInput('token'); let token = core.getInput('token');
@ -2154,6 +2154,23 @@ function parseGoVersion(versionString) {
return versionString.split(' ')[2].slice('go'.length); return versionString.split(' ')[2].slice('go'.length);
} }
exports.parseGoVersion = parseGoVersion; 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 //# sourceMappingURL=main.js.map
/***/ }), /***/ }),
@ -5894,13 +5911,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); 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 tc = __importStar(__webpack_require__(533));
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(280)); const semver = __importStar(__webpack_require__(280));
const httpm = __importStar(__webpack_require__(539)); const httpm = __importStar(__webpack_require__(539));
const sys = __importStar(__webpack_require__(737)); const sys = __importStar(__webpack_require__(737));
const fs_1 = __importDefault(__webpack_require__(747));
const os_1 = __importDefault(__webpack_require__(87)); const os_1 = __importDefault(__webpack_require__(87));
function getGo(versionSpec, checkLatest, auth) { function getGo(versionSpec, checkLatest, auth) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -6119,6 +6137,15 @@ function makeSemver(version) {
return fullVersion; return fullVersion;
} }
exports.makeSemver = makeSemver; 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 //# sourceMappingURL=installer.js.map
/***/ }), /***/ }),

View file

@ -4,6 +4,7 @@ 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';
import fs from 'fs';
import os from 'os'; import os from 'os';
type InstallationType = 'dist' | 'manifest'; type InstallationType = 'dist' | 'manifest';
@ -298,3 +299,14 @@ export function makeSemver(version: string): string {
} }
return fullVersion; 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();
}

View file

@ -13,7 +13,7 @@ export async function run() {
// versionSpec is optional. If supplied, install / use from the tool cache // 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. // 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}`); core.info(`Setup go version spec ${versionSpec}`);
@ -104,3 +104,29 @@ export function parseGoVersion(versionString: string): string {
// expecting go<version> for runtime.Version() // expecting go<version> for runtime.Version()
return versionString.split(' ')[2].slice('go'.length); 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;
}