From 8e6a7f3c647e83ee815397ca07f45095364d6865 Mon Sep 17 00:00:00 2001 From: myaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com> Date: Sun, 7 Jul 2024 12:33:39 -0400 Subject: [PATCH] Extend go-version to accept go.mod files --- __tests__/setup-go.test.ts | 35 +++++++++++++++++++++++------------ dist/setup/index.js | 30 ++++++++++++------------------ src/installer.ts | 14 -------------- src/main.ts | 24 +++++++++++++++--------- 4 files changed, 50 insertions(+), 53 deletions(-) diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 70f2166..ddbe193 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -868,18 +868,6 @@ use . expect(logSpy).toHaveBeenCalledWith('matching 1.19...'); }); - it('reads version from .go-version', async () => { - inputs['go-version-file'] = '.go-version'; - existsSpy.mockImplementation(() => true); - readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); - - await main.run(); - - expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.0'); - expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.0...'); - expect(logSpy).toHaveBeenCalledWith('matching 1.13.0...'); - }); - it('is overwritten by go-version', async () => { inputs['go-version'] = '1.13.1'; inputs['go-version-file'] = 'go.mod'; @@ -904,6 +892,29 @@ use . ); }); + it('go-version accepts a go.mod file', async () => { + inputs['go-version'] = 'go.mod'; + existsSpy.mockImplementation(() => true); + readFileSpy.mockImplementation(() => Buffer.from(goModContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.14'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.14...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.14...'); + }); + + it('go-version reports a read failure', async () => { + inputs['go-version'] = 'path/to/go.mod'; + existsSpy.mockImplementation(() => false); + + await main.run(); + + expect(cnSpy).toHaveBeenCalledWith( + `::error::The specified go version file at: path/to/go.mod does not exist${osm.EOL}` + ); + }); + it('acquires specified architecture of go', async () => { for (const {arch, version, osSpec} of [ {arch: 'amd64', version: '1.13.7', osSpec: 'linux'}, diff --git a/dist/setup/index.js b/dist/setup/index.js index 9ad3784..7931411 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -88244,7 +88244,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0; +exports.resolveStableVersionInput = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0; const tc = __importStar(__nccwpck_require__(7784)); const core = __importStar(__nccwpck_require__(2186)); const path = __importStar(__nccwpck_require__(1017)); @@ -88540,16 +88540,6 @@ function makeSemver(version) { return fullVersion; } exports.makeSemver = makeSemver; -function parseGoVersionFile(versionFilePath) { - const contents = fs_1.default.readFileSync(versionFilePath).toString(); - if (path.basename(versionFilePath) === 'go.mod' || - path.basename(versionFilePath) === 'go.work') { - const match = contents.match(/^go (\d+(\.\d+)*)/m); - return match ? match[1] : ''; - } - return contents.trim(); -} -exports.parseGoVersionFile = parseGoVersionFile; function resolveStableVersionDist(versionSpec, arch) { return __awaiter(this, void 0, void 0, function* () { const archFilter = sys.getArch(arch); @@ -88749,18 +88739,22 @@ function parseGoVersion(versionString) { exports.parseGoVersion = parseGoVersion; function resolveVersionInput() { let version = core.getInput('go-version'); - const versionFilePath = core.getInput('go-version-file'); + let versionFilePath = core.getInput('go-version-file'); if (version && versionFilePath) { core.warning('Both go-version and go-version-file inputs are specified, only go-version will be used'); - } - if (version) { - return version; + versionFilePath = ''; } if (versionFilePath) { - if (!fs_1.default.existsSync(versionFilePath)) { - throw new Error(`The specified go version file at: ${versionFilePath} does not exist`); + version = versionFilePath; + } + if (path_1.default.basename(version) === 'go.mod' || + path_1.default.basename(version) === 'go.work') { + if (!fs_1.default.existsSync(version)) { + throw new Error(`The specified go version file at: ${version} does not exist`); } - version = installer.parseGoVersionFile(versionFilePath); + const contents = fs_1.default.readFileSync(version).toString(); + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; } return version; } diff --git a/src/installer.ts b/src/installer.ts index 817c334..28554d3 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -417,20 +417,6 @@ export function makeSemver(version: string): string { return fullVersion; } -export function parseGoVersionFile(versionFilePath: string): string { - const contents = fs.readFileSync(versionFilePath).toString(); - - if ( - path.basename(versionFilePath) === 'go.mod' || - path.basename(versionFilePath) === 'go.work' - ) { - const match = contents.match(/^go (\d+(\.\d+)*)/m); - return match ? match[1] : ''; - } - - return contents.trim(); -} - async function resolveStableVersionDist(versionSpec: string, arch: string) { const archFilter = sys.getArch(arch); const platFilter = sys.getPlatform(); diff --git a/src/main.ts b/src/main.ts index 690d277..91f031c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -137,25 +137,31 @@ export function parseGoVersion(versionString: string): string { function resolveVersionInput(): string { let version = core.getInput('go-version'); - const versionFilePath = core.getInput('go-version-file'); + let versionFilePath = core.getInput('go-version-file'); if (version && versionFilePath) { core.warning( 'Both go-version and go-version-file inputs are specified, only go-version will be used' ); + versionFilePath = ''; } - - if (version) { - return version; - } - if (versionFilePath) { - if (!fs.existsSync(versionFilePath)) { + version = versionFilePath; + } + + if ( + path.basename(version) === 'go.mod' || + path.basename(version) === 'go.work' + ) { + if (!fs.existsSync(version)) { throw new Error( - `The specified go version file at: ${versionFilePath} does not exist` + `The specified go version file at: ${version} does not exist` ); } - version = installer.parseGoVersionFile(versionFilePath); + + const contents = fs.readFileSync(version).toString(); + const match = contents.match(/^go (\d+(\.\d+)*)/m); + return match ? match[1] : ''; } return version;