Read go version from go.mod

This commit is contained in:
So Jomura 2022-04-29 11:11:36 +00:00 committed by GitHub
parent f5fe54e5a4
commit 55b9afc327
4 changed files with 76 additions and 22 deletions

View file

@ -559,9 +559,31 @@ describe('setup-go', () => {
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', () => { describe('go-version-file', () => {
it('reads version from file', async () => { it('reads version from go.mod', async () => {
inputs['go-version-from-file'] = '.go-version'; 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}`)); readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));
await main.run(); await main.run();
@ -573,8 +595,7 @@ describe('setup-go', () => {
it('is overwritten by go-version', async () => { it('is overwritten by go-version', async () => {
inputs['go-version'] = '1.13.1'; inputs['go-version'] = '1.13.1';
inputs['go-version-file'] = '.go-version';
inputs['go-version-from-file'] = '.go-version';
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));
await main.run(); await main.run();
@ -586,7 +607,7 @@ describe('setup-go', () => {
it('reports a read failure', async () => { it('reports a read failure', async () => {
const versionFilePath = '.go-version'; 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}'`; const errMsg = `ENOENT: no such file or directory, open '${versionFilePath}'`;
readFileSpy.mockImplementation(() => { readFileSpy.mockImplementation(() => {
throw new Error(errMsg); throw new Error(errMsg);

29
dist/index.js vendored
View file

@ -1432,13 +1432,7 @@ 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.
// //
const versionFilePath = core.getInput('go-version-from-file'); const versionSpec = resolveVersionInput();
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 // stable will be true unless false is the exact input
// since getting unstable versions should be explicit // since getting unstable versions should be explicit
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; 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'); const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== '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 }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); 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 tc = __importStar(__webpack_require__(533));
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
@ -5144,6 +5149,14 @@ function makeSemver(version) {
return `${verPart}${prereleasePart}`; return `${verPart}${prereleasePart}`;
} }
exports.makeSemver = makeSemver; 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;
/***/ }), /***/ }),

View file

@ -266,3 +266,12 @@ export function makeSemver(version: string): string {
return `${verPart}${prereleasePart}`; 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] : '';
}

View file

@ -12,14 +12,7 @@ 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.
// //
const versionFilePath = core.getInput('go-version-from-file'); const versionSpec = resolveVersionInput();
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
@ -97,3 +90,21 @@ function isGhes(): boolean {
); );
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; 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;
}