diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index c241364..6cb16ff 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -559,9 +559,31 @@ describe('setup-go', () => { 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'; + describe('go-version-file', () => { + it('reads version from go.mod', async () => { + inputs['go-version-file'] = 'go.mod'; + const content = `module example.com/mymodule + +go 1.14 + +require ( + example.com/othermodule v1.2.3 + example.com/thismodule v1.2.3 + example.com/thatmodule v1.2.3 +) + +replace example.com/thatmodule => ../thatmodule +exclude example.com/thismodule v1.3.0 +`; + readFileSpy.mockImplementation(() => Buffer.from(content)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go stable version spec 1.14'); + }); + + it('reads version from .go-version', async () => { + inputs['go-version-file'] = '.go-version'; readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); await main.run(); @@ -573,8 +595,7 @@ describe('setup-go', () => { it('is overwritten by go-version', async () => { inputs['go-version'] = '1.13.1'; - - inputs['go-version-from-file'] = '.go-version'; + inputs['go-version-file'] = '.go-version'; readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); await main.run(); @@ -586,7 +607,7 @@ describe('setup-go', () => { it('reports a read failure', async () => { const versionFilePath = '.go-version'; - inputs['go-version-from-file'] = versionFilePath; + inputs['go-version-file'] = versionFilePath; const errMsg = `ENOENT: no such file or directory, open '${versionFilePath}'`; readFileSpy.mockImplementation(() => { throw new Error(errMsg); diff --git a/dist/index.js b/dist/index.js index aacd1ca..cb4de94 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1432,13 +1432,7 @@ 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. // - 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; + const versionSpec = resolveVersionInput(); // 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'; @@ -1506,6 +1500,17 @@ function isGhes() { const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; } +function resolveVersionInput() { + let version = core.getInput('go-version'); + const versionFilePath = core.getInput('go-version-file'); + if (version) { + return version; + } + if (versionFilePath) { + version = installer.parseGoVersionFile(fs_1.default.readFileSync(versionFilePath).toString(), path_1.default.basename(versionFilePath) === 'go.mod'); + } + return version; +} /***/ }), @@ -4941,7 +4946,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.extractGoArchive = exports.getGo = void 0; +exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.extractGoArchive = exports.getGo = void 0; const tc = __importStar(__webpack_require__(533)); const core = __importStar(__webpack_require__(470)); const path = __importStar(__webpack_require__(622)); @@ -5144,6 +5149,14 @@ function makeSemver(version) { return `${verPart}${prereleasePart}`; } exports.makeSemver = makeSemver; +function parseGoVersionFile(contents, isMod) { + if (!isMod) { + return contents.trim(); + } + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; +} +exports.parseGoVersionFile = parseGoVersionFile; /***/ }), diff --git a/src/installer.ts b/src/installer.ts index 08758a4..bb0a084 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -266,3 +266,12 @@ export function makeSemver(version: string): string { return `${verPart}${prereleasePart}`; } + +export function parseGoVersionFile(contents: string, isMod: boolean): string { + if (!isMod) { + return contents.trim(); + } + + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; +} diff --git a/src/main.ts b/src/main.ts index 7593e7a..d91c19a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,14 +12,7 @@ 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. // - const versionFilePath = core.getInput('go-version-from-file'); - const versionSpecFromFile = - versionFilePath && - fs - .readFileSync(versionFilePath) - .toString() - .trim(); - let versionSpec = core.getInput('go-version') || versionSpecFromFile; + const versionSpec = resolveVersionInput(); // stable will be true unless false is the exact input // since getting unstable versions should be explicit @@ -97,3 +90,21 @@ function isGhes(): boolean { ); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; } + +function resolveVersionInput(): string { + let version = core.getInput('go-version'); + const versionFilePath = core.getInput('go-version-file'); + + if (version) { + return version; + } + + if (versionFilePath) { + version = installer.parseGoVersionFile( + fs.readFileSync(versionFilePath).toString(), + path.basename(versionFilePath) === 'go.mod' + ); + } + + return version; +}