mirror of
https://github.com/graalvm/setup-graalvm.git
synced 2025-01-18 19:26:39 +08:00
Use semver
in toSemVer()
.
This commit is contained in:
parent
6df9f698c6
commit
1588b03f82
34
__tests__/utils.test.ts
Normal file
34
__tests__/utils.test.ts
Normal 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')
|
||||||
|
}
|
||||||
|
})
|
27
src/utils.ts
27
src/utils.ts
@ -2,6 +2,7 @@ import * as c from './constants'
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as github from '@actions/github'
|
import * as github from '@actions/github'
|
||||||
import * as httpClient from '@actions/http-client'
|
import * as httpClient from '@actions/http-client'
|
||||||
|
import * as semver from 'semver'
|
||||||
import * as tc from '@actions/tool-cache'
|
import * as tc from '@actions/tool-cache'
|
||||||
import {ExecOptions, exec as e} from '@actions/exec'
|
import {ExecOptions, exec as e} from '@actions/exec'
|
||||||
import {readFileSync, readdirSync} from 'fs'
|
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 {
|
export function toSemVer(version: string): string {
|
||||||
const parts = version.split('.')
|
const parts = version.split('.')
|
||||||
const major = parts[0]
|
if (parts.length === 4) {
|
||||||
const minor = parts.length > 1 ? parts[1] : '0'
|
/**
|
||||||
const patch = parts.length > 2 ? parts.slice(2).join('-') : '0'
|
* Turn legacy GraalVM version numbers (e.g., `22.0.0.2`) into valid
|
||||||
return `${major}.${minor}.${patch}`
|
* 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 {
|
export function isPREvent(): boolean {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user