Skip to content

Commit

Permalink
Extend go-version to accept go.mod files
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Jul 7, 2024
1 parent 0a12ed9 commit 8e6a7f3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
35 changes: 23 additions & 12 deletions __tests__/setup-go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'},
Expand Down
30 changes: 12 additions & 18 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 0 additions & 14 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 14 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
version = versionFilePath;
}

if (versionFilePath) {
if (!fs.existsSync(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;
Expand Down

0 comments on commit 8e6a7f3

Please sign in to comment.