1
0
Fork 0
mirror of https://github.com/actions/setup-go synced 2024-11-09 15:12:40 +00:00

add support gowork for go-version-file

This commit is contained in:
koba1t 2022-11-01 19:13:57 +09:00
parent c4a742cab1
commit 7678c83214
No known key found for this signature in database
GPG key ID: E407ED062C8D17DE
7 changed files with 48 additions and 7 deletions

View file

@ -66,6 +66,22 @@ jobs:
run: __tests__/verify-go.sh 1.14
shell: bash
go-version-file-with-gowork:
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.work
- name: verify go
run: __tests__/verify-go.sh 1.19
shell: bash
setup-versions-from-manifest:
name: Setup ${{ matrix.go }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}

View file

@ -127,7 +127,7 @@ steps:
```
## 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.
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains 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

3
__tests__/data/go.work Normal file
View file

@ -0,0 +1,3 @@
go 1.19
use .

View file

@ -824,6 +824,12 @@ require (
replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0
`;
const goWorkContents = `go 1.19
use .
`;
it('reads version from go.mod', async () => {
@ -838,6 +844,18 @@ exclude example.com/thismodule v1.3.0
expect(logSpy).toHaveBeenCalledWith('matching 1.14...');
});
it('reads version from go.work', async () => {
inputs['go-version-file'] = 'go.work';
existsSpy.mockImplementation(() => true);
readFileSpy.mockImplementation(() => Buffer.from(goWorkContents));
await main.run();
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.19');
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.19...');
expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
});
it('reads version from .go-version', async () => {
inputs['go-version-file'] = '.go-version';
existsSpy.mockImplementation(() => true);

View file

@ -5,7 +5,7 @@ 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.'
description: 'Path to the go.mod or go.work 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

3
dist/setup/index.js vendored
View file

@ -63444,7 +63444,8 @@ function makeSemver(version) {
exports.makeSemver = makeSemver;
function parseGoVersionFile(versionFilePath) {
const contents = fs_1.default.readFileSync(versionFilePath).toString();
if (path.basename(versionFilePath) === 'go.mod') {
if (path.basename(versionFilePath) === 'go.mod' ||
path.basename(versionFilePath) === 'go.work') {
const match = contents.match(/^go (\d+(\.\d+)*)/m);
return match ? match[1] : '';
}

View file

@ -316,7 +316,10 @@ export function makeSemver(version: string): string {
export function parseGoVersionFile(versionFilePath: string): string {
const contents = fs.readFileSync(versionFilePath).toString();
if (path.basename(versionFilePath) === 'go.mod') {
if (
path.basename(versionFilePath) === 'go.mod' ||
path.basename(versionFilePath) === 'go.work'
) {
const match = contents.match(/^go (\d+(\.\d+)*)/m);
return match ? match[1] : '';
}