Merge pull request #283 from koba1t/add_support_gowork_for_go-version-file

add support go.work file for go-version-file
This commit is contained in:
Marko Zivic 2022-11-08 11:45:09 +01:00 committed by GitHub
commit e983b65a44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 7 deletions

View file

@ -66,6 +66,22 @@ jobs:
run: __tests__/verify-go.sh 1.14 run: __tests__/verify-go.sh 1.14
shell: bash 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: setup-versions-from-manifest:
name: Setup ${{ matrix.go }} ${{ matrix.os }} name: Setup ${{ matrix.go }} ${{ matrix.os }}
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
@ -123,4 +139,4 @@ jobs:
go-version: ${{ matrix.go-version }} go-version: ${{ matrix.go-version }}
architecture: x64 architecture: x64
- name: Verify Go - name: Verify Go
run: go version run: go version

View file

@ -127,7 +127,7 @@ steps:
``` ```
## Getting go version from the go.mod file ## 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. 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 > 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 replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0 exclude example.com/thismodule v1.3.0
`;
const goWorkContents = `go 1.19
use .
`; `;
it('reads version from go.mod', async () => { it('reads version from go.mod', async () => {
@ -838,6 +844,18 @@ exclude example.com/thismodule v1.3.0
expect(logSpy).toHaveBeenCalledWith('matching 1.14...'); 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 () => { it('reads version from .go-version', async () => {
inputs['go-version-file'] = '.go-version'; inputs['go-version-file'] = '.go-version';
existsSpy.mockImplementation(() => true); existsSpy.mockImplementation(() => true);

View file

@ -5,7 +5,7 @@ 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: go-version-file:
description: 'Path to the go.mod file.' description: 'Path to the go.mod or go.work 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
@ -21,8 +21,8 @@ inputs:
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.' description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
outputs: outputs:
go-version: go-version:
description: 'The installed Go version. Useful when given a version range as input.' description: 'The installed Go version. Useful when given a version range as input.'
cache-hit: cache-hit:
description: 'A boolean value to indicate if a cache was hit' description: 'A boolean value to indicate if a cache was hit'
runs: runs:
using: 'node16' using: 'node16'

3
dist/setup/index.js vendored
View file

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

View file

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