Use semver in toSemVer().

This commit is contained in:
Fabio Niephaus 2024-02-13 15:44:08 +01:00
parent 6df9f698c6
commit 1588b03f82
No known key found for this signature in database
GPG Key ID: F21CF5275F31DFD6
2 changed files with 52 additions and 9 deletions

34
__tests__/utils.test.ts Normal file
View File

@ -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')
}
})

View File

@ -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 {