From 1588b03f82bde849218d8a17f20c4b779d277c40 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Tue, 13 Feb 2024 15:44:08 +0100 Subject: [PATCH] Use `semver` in `toSemVer()`. --- __tests__/utils.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/utils.ts | 27 ++++++++++++++++++--------- 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 __tests__/utils.test.ts diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts new file mode 100644 index 0000000..b35791f --- /dev/null +++ b/__tests__/utils.test.ts @@ -0,0 +1,34 @@ +import * as path from 'path' +import {expect, test} from '@jest/globals' +import {toSemVer} from '../src/utils' + +test('convert version', async () => { + for (var inputAndExpectedOutput of [ + ['22', '22.0.0'], + ['22.0', '22.0.0'], + ['22.0.0', '22.0.0'], + ['22.0.0.2', '22.0.0-2'], + ['22-ea', '22.0.0-ea'], + ['22.0-ea', '22.0.0-ea'], + ['22.0.0-ea', '22.0.0-ea'] + ]) { + expect(toSemVer(inputAndExpectedOutput[0])).toBe(inputAndExpectedOutput[1]) + } +}) + +test('convert invalid version', async () => { + for (var input of ['dev', 'abc', 'a.b.c']) { + let error = new Error('unexpected') + try { + toSemVer(input) + } catch (err) { + if (!(err instanceof Error)) { + fail(`Unexpected non-Error: ${err}`) + } + error = err + } + + expect(error).not.toBeUndefined() + expect(error.message).toContain('Unable to convert') + } +}) diff --git a/src/utils.ts b/src/utils.ts index 1934e56..cf92b7f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,7 @@ import * as c from './constants' import * as core from '@actions/core' import * as github from '@actions/github' import * as httpClient from '@actions/http-client' +import * as semver from 'semver' import * as tc from '@actions/tool-cache' import {ExecOptions, exec as e} from '@actions/exec' import {readFileSync, readdirSync} from 'fs' @@ -154,17 +155,25 @@ function findJavaHomeInSubfolder(searchPath: string): string { } } -/** - * This helper turns GraalVM version numbers (e.g., `22.0.0.2`) into valid - * semver.org versions (e.g., `22.0.0-2`), which is needed because - * @actions/tool-cache uses `semver` to validate versions. - */ export function toSemVer(version: string): string { const parts = version.split('.') - const major = parts[0] - const minor = parts.length > 1 ? parts[1] : '0' - const patch = parts.length > 2 ? parts.slice(2).join('-') : '0' - return `${major}.${minor}.${patch}` + if (parts.length === 4) { + /** + * Turn legacy GraalVM version numbers (e.g., `22.0.0.2`) into valid + * semver.org versions (e.g., `22.0.0-2`). + */ + return `${parts[0]}.${parts[1]}.${parts.slice(2).join('-')}` + } + + const versionParts = version.split('-', 2) + const suffix = versionParts.length === 2 ? '-' + versionParts[1] : '' + const validVersion = semver.valid(semver.coerce(versionParts[0]) + suffix) + if (!validVersion) { + throw new Error( + `Unable to convert '${version}' to semantic version. ${c.ERROR_HINT}` + ) + } + return validVersion } export function isPREvent(): boolean {