Skip to content

Commit

Permalink
Improve error log for wrong mandrel version and javaVersion comb.
Browse files Browse the repository at this point in the history
Add test to test similar to the GraalVM one for the expected error
output.
  • Loading branch information
zakkak committed Oct 30, 2023
1 parent 2b3d0bd commit 7d1c078
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
56 changes: 56 additions & 0 deletions __tests__/mandrel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as path from 'path'
import * as mandrel from '../src/mandrel'
import {expect, test} from '@jest/globals'
import {getLatestRelease} from '../src/utils'

process.env['RUNNER_TOOL_CACHE'] = path.join(__dirname, 'TOOL_CACHE')
process.env['RUNNER_TEMP'] = path.join(__dirname, 'TEMP')

test('request invalid version/javaVersion combination', async () => {
for (var combination of [
['mandrel-23.1.1.0-Final', '17'],
['mandrel-23.0.2.1-Final', '21'],
]) {
let error = new Error('unexpected')
try {
await mandrel.setUpMandrel(combination[0], combination[1])
} catch (err) {
if (!(err instanceof Error)) {
fail(`Unexpected non-Error: ${err}`)
}
error = err
}

expect(error).not.toBeUndefined()
expect(error.message).toContain('Failed to download')
expect(error.message).toContain('Are you sure version')
}
})
test('request invalid version', async () => {
for (var combination of [
['mandrel-23.1.1.0', '21'],
['mandrel-23.0.2.1', '17'],
]) {
let error = new Error('unexpected')
try {
await mandrel.setUpMandrel(combination[0], combination[1])
} catch (err) {
if (!(err instanceof Error)) {
fail(`Unexpected non-Error: ${err}`)
}
error = err
}

expect(error).not.toBeUndefined()
expect(error.message).toContain('Failed to download')
expect(error.message).toContain('Are you sure version')
}
})

test('find latest', async () => {
// Make sure the action can find the latest Mandrel release
const latestRelease = await getLatestRelease(mandrel.MANDREL_REPO)
const tag_name = latestRelease.tag_name
expect(tag_name).toContain(mandrel.MANDREL_TAG_PREFIX)
})

32 changes: 27 additions & 5 deletions src/mandrel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as c from './constants'
import {downloadExtractAndCacheJDK, getLatestRelease} from './utils'
import {downloadTool} from '@actions/tool-cache'
import {basename} from 'path'

const MANDREL_REPO = 'mandrel'
const MANDREL_TAG_PREFIX = c.MANDREL_NAMESPACE
export const MANDREL_REPO = 'mandrel'
export const MANDREL_TAG_PREFIX = c.MANDREL_NAMESPACE
const MANDREL_DL_BASE = 'https://github.com/graalvm/mandrel/releases/download'

export async function setUpMandrel(
Expand Down Expand Up @@ -45,16 +46,37 @@ async function setUpMandrelRelease(
version: string,
javaVersion: string
): Promise<string> {
const identifier = determineMandrelIdentifier(version, javaVersion)
const downloadUrl = `${MANDREL_DL_BASE}/${MANDREL_TAG_PREFIX}${version}/${identifier}${c.GRAALVM_FILE_EXTENSION}`
const toolName = determineToolName(javaVersion)
return downloadExtractAndCacheJDK(
async () => downloadTool(downloadUrl),
async () => downloadMandrelJDK(version, javaVersion),
toolName,
version
)
}

async function downloadMandrelJDK(
version: string,
javaVersion: string
): Promise<string> {
const identifier = determineMandrelIdentifier(version, javaVersion)
const downloadUrl = `${MANDREL_DL_BASE}/${MANDREL_TAG_PREFIX}${version}/${identifier}${c.GRAALVM_FILE_EXTENSION}`
try {
return await downloadTool(downloadUrl)
} catch (error) {
if (error instanceof Error && error.message.includes('404')) {
// Not Found
throw new Error(
`Failed to download ${basename(
downloadUrl
)}. Are you sure version: '${version}' and java-version: '${javaVersion}' are correct?`
)
}
throw new Error(
`Failed to download ${basename(downloadUrl)} (error: ${error}).`
)
}
}

function determineMandrelIdentifier(
version: string,
javaVersion: string
Expand Down

0 comments on commit 7d1c078

Please sign in to comment.