Skip to content

Commit 8e6a7f3

Browse files
committed
Extend go-version to accept go.mod files
1 parent 0a12ed9 commit 8e6a7f3

File tree

4 files changed

+49
-52
lines changed

4 files changed

+49
-52
lines changed

__tests__/setup-go.test.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -868,18 +868,6 @@ use .
868868
expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
869869
});
870870

871-
it('reads version from .go-version', async () => {
872-
inputs['go-version-file'] = '.go-version';
873-
existsSpy.mockImplementation(() => true);
874-
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));
875-
876-
await main.run();
877-
878-
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.0');
879-
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.0...');
880-
expect(logSpy).toHaveBeenCalledWith('matching 1.13.0...');
881-
});
882-
883871
it('is overwritten by go-version', async () => {
884872
inputs['go-version'] = '1.13.1';
885873
inputs['go-version-file'] = 'go.mod';
@@ -904,6 +892,29 @@ use .
904892
);
905893
});
906894

895+
it('go-version accepts a go.mod file', async () => {
896+
inputs['go-version'] = 'go.mod';
897+
existsSpy.mockImplementation(() => true);
898+
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
899+
900+
await main.run();
901+
902+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.14');
903+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.14...');
904+
expect(logSpy).toHaveBeenCalledWith('matching 1.14...');
905+
});
906+
907+
it('go-version reports a read failure', async () => {
908+
inputs['go-version'] = 'path/to/go.mod';
909+
existsSpy.mockImplementation(() => false);
910+
911+
await main.run();
912+
913+
expect(cnSpy).toHaveBeenCalledWith(
914+
`::error::The specified go version file at: path/to/go.mod does not exist${osm.EOL}`
915+
);
916+
});
917+
907918
it('acquires specified architecture of go', async () => {
908919
for (const {arch, version, osSpec} of [
909920
{arch: 'amd64', version: '1.13.7', osSpec: 'linux'},

dist/setup/index.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -88244,7 +88244,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
8824488244
return (mod && mod.__esModule) ? mod : { "default": mod };
8824588245
};
8824688246
Object.defineProperty(exports, "__esModule", ({ value: true }));
88247-
exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0;
88247+
exports.resolveStableVersionInput = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0;
8824888248
const tc = __importStar(__nccwpck_require__(7784));
8824988249
const core = __importStar(__nccwpck_require__(2186));
8825088250
const path = __importStar(__nccwpck_require__(1017));
@@ -88540,16 +88540,6 @@ function makeSemver(version) {
8854088540
return fullVersion;
8854188541
}
8854288542
exports.makeSemver = makeSemver;
88543-
function parseGoVersionFile(versionFilePath) {
88544-
const contents = fs_1.default.readFileSync(versionFilePath).toString();
88545-
if (path.basename(versionFilePath) === 'go.mod' ||
88546-
path.basename(versionFilePath) === 'go.work') {
88547-
const match = contents.match(/^go (\d+(\.\d+)*)/m);
88548-
return match ? match[1] : '';
88549-
}
88550-
return contents.trim();
88551-
}
88552-
exports.parseGoVersionFile = parseGoVersionFile;
8855388543
function resolveStableVersionDist(versionSpec, arch) {
8855488544
return __awaiter(this, void 0, void 0, function* () {
8855588545
const archFilter = sys.getArch(arch);
@@ -88749,18 +88739,22 @@ function parseGoVersion(versionString) {
8874988739
exports.parseGoVersion = parseGoVersion;
8875088740
function resolveVersionInput() {
8875188741
let version = core.getInput('go-version');
88752-
const versionFilePath = core.getInput('go-version-file');
88742+
let versionFilePath = core.getInput('go-version-file');
8875388743
if (version && versionFilePath) {
8875488744
core.warning('Both go-version and go-version-file inputs are specified, only go-version will be used');
88755-
}
88756-
if (version) {
88757-
return version;
88745+
versionFilePath = '';
8875888746
}
8875988747
if (versionFilePath) {
88760-
if (!fs_1.default.existsSync(versionFilePath)) {
88761-
throw new Error(`The specified go version file at: ${versionFilePath} does not exist`);
88748+
version = versionFilePath;
88749+
}
88750+
if (path_1.default.basename(version) === 'go.mod' ||
88751+
path_1.default.basename(version) === 'go.work') {
88752+
if (!fs_1.default.existsSync(version)) {
88753+
throw new Error(`The specified go version file at: ${version} does not exist`);
8876288754
}
88763-
version = installer.parseGoVersionFile(versionFilePath);
88755+
const contents = fs_1.default.readFileSync(version).toString();
88756+
const match = contents.match(/^go (\d+(\.\d+)*)/m);
88757+
return match ? match[1] : '';
8876488758
}
8876588759
return version;
8876688760
}

src/installer.ts

-14
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,6 @@ export function makeSemver(version: string): string {
417417
return fullVersion;
418418
}
419419

420-
export function parseGoVersionFile(versionFilePath: string): string {
421-
const contents = fs.readFileSync(versionFilePath).toString();
422-
423-
if (
424-
path.basename(versionFilePath) === 'go.mod' ||
425-
path.basename(versionFilePath) === 'go.work'
426-
) {
427-
const match = contents.match(/^go (\d+(\.\d+)*)/m);
428-
return match ? match[1] : '';
429-
}
430-
431-
return contents.trim();
432-
}
433-
434420
async function resolveStableVersionDist(versionSpec: string, arch: string) {
435421
const archFilter = sys.getArch(arch);
436422
const platFilter = sys.getPlatform();

src/main.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,31 @@ export function parseGoVersion(versionString: string): string {
137137

138138
function resolveVersionInput(): string {
139139
let version = core.getInput('go-version');
140-
const versionFilePath = core.getInput('go-version-file');
140+
let versionFilePath = core.getInput('go-version-file');
141141

142142
if (version && versionFilePath) {
143143
core.warning(
144144
'Both go-version and go-version-file inputs are specified, only go-version will be used'
145145
);
146+
versionFilePath = '';
146147
}
147-
148-
if (version) {
149-
return version;
148+
if (versionFilePath) {
149+
version = versionFilePath;
150150
}
151151

152-
if (versionFilePath) {
153-
if (!fs.existsSync(versionFilePath)) {
152+
if (
153+
path.basename(version) === 'go.mod' ||
154+
path.basename(version) === 'go.work'
155+
) {
156+
if (!fs.existsSync(version)) {
154157
throw new Error(
155-
`The specified go version file at: ${versionFilePath} does not exist`
158+
`The specified go version file at: ${version} does not exist`
156159
);
157160
}
158-
version = installer.parseGoVersionFile(versionFilePath);
161+
162+
const contents = fs.readFileSync(version).toString();
163+
const match = contents.match(/^go (\d+(\.\d+)*)/m);
164+
return match ? match[1] : '';
159165
}
160166

161167
return version;

0 commit comments

Comments
 (0)