From a707d84385be85607f1d50c46b9cbd3dc3300b82 Mon Sep 17 00:00:00 2001 From: jojo43 Date: Sun, 5 Jul 2020 10:25:03 +0900 Subject: [PATCH] Add go-version-from-file option --- __tests__/setup-go.test.ts | 41 ++++++++++++++++++++++++++++++++++++++ src/main.ts | 9 ++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 9007c81..3a2841d 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -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; @@ -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 @@ -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\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}`); + }); + }); }); diff --git a/src/main.ts b/src/main.ts index 2d90b2f..7593e7a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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