Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add go-version-from-file option #62

Merged
merged 15 commits into from
May 12, 2022
41 changes: 41 additions & 0 deletions __tests__/setup-go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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;
Expand Down Expand Up @@ -60,6 +61,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
Expand Down Expand Up @@ -556,4 +558,43 @@ describe('setup-go', () => {
it('does not convert exact versions', async () => {
expect(im.makeSemver('1.13.1')).toBe('1.13.1');
});

describe('go-version-from-file', () => {
it('reads version from file', async () => {
inputs['go-version-from-file'] = '.go-version';
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));

await main.run();

expect(logSpy).toHaveBeenCalledWith(
'Setup go stable version spec 1.13.0'
);
});

it('is overwritten by go-version', async () => {
inputs['go-version'] = '1.13.1';

inputs['go-version-from-file'] = '.go-version';
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));

await main.run();

expect(logSpy).toHaveBeenCalledWith(
'Setup go stable version spec 1.13.1'
);
});

it('reports a read failure', async () => {
const versionFilePath = '.go-version';
inputs['go-version-from-file'] = versionFilePath;
const errMsg = `ENOENT: no such file or directory, open '${versionFilePath}'`;
readFileSpy.mockImplementation(() => {
throw new Error(errMsg);
});

await main.run();

expect(cnSpy).toHaveBeenCalledWith(`::error::${errMsg}${osm.EOL}`);
});
});
});
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-from-file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go-version-from-file:
go-version-file:

description: Path to the file with the Go version. go-version overwrites this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: Path to the file with the Go version. go-version overwrites this.
description: 'Path to the go.mod file.'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IvanZosimov FWIW as an end user I would find the original description more accurate. While I guess none of us can predict what % of users will end up specifying go.mod and what other % .go-version, but I assume (hope) that the intention is to support both and so the description should reflect that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @radeksimko 👋 ! Thank you, that's definitely a good point! We are going to update setup-go documentation with the new chapter related to this functionality with a more broad description of the input.

stable:
description: 'Whether to download only stable versions'
default: 'true'
Expand Down
8 changes: 7 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,13 @@ 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 versionFilePath = core.getInput('go-version-from-file');
const versionSpecFromFile = versionFilePath &&
fs_1.default
.readFileSync(versionFilePath)
.toString()
.trim();
let versionSpec = core.getInput('go-version') || versionSpecFromFile;
// stable will be true unless false is the exact input
// since getting unstable versions should be explicit
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ 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 versionFilePath = core.getInput('go-version-from-file');
const versionSpecFromFile =
versionFilePath &&
fs
.readFileSync(versionFilePath)
.toString()
.trim();
let versionSpec = core.getInput('go-version') || versionSpecFromFile;

// stable will be true unless false is the exact input
// since getting unstable versions should be explicit
Expand Down