Add go-version-from-file option

This commit is contained in:
jojo43 2020-07-05 10:25:03 +09:00
parent 1616116e1b
commit a707d84385
2 changed files with 49 additions and 1 deletions

View file

@ -31,6 +31,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;
@ -60,6 +61,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
@ -556,4 +558,43 @@ describe('setup-go', () => {
it('does not convert exact versions', async () => { it('does not convert exact versions', async () => {
expect(im.makeSemver('1.13.1')).toBe('1.13.1'); 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\n'));
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\n'));
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}`);
});
});
}); });

View file

@ -12,7 +12,14 @@ 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 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 // stable will be true unless false is the exact input
// since getting unstable versions should be explicit // since getting unstable versions should be explicit