mirror of
https://github.com/actions/setup-java.git
synced 2026-07-01 17:41:38 +00:00
Compare commits
No commits in common. "bac80db521946ffd9f3ee647e171765e93887b76" and "f5b041c5eb3c2ef14c5e3c9916a0e822a9a12b04" have entirely different histories.
bac80db521
...
f5b041c5eb
@ -1,732 +1,154 @@
|
|||||||
import * as core from '@actions/core';
|
|
||||||
import * as tc from '@actions/tool-cache';
|
|
||||||
import * as http from '@actions/http-client';
|
|
||||||
import fs from 'fs';
|
|
||||||
import path from 'path';
|
|
||||||
import {GraalVMDistribution} from '../../src/distributions/graalvm/installer';
|
import {GraalVMDistribution} from '../../src/distributions/graalvm/installer';
|
||||||
import {JavaInstallerOptions} from '../../src/distributions/base-models';
|
import os from 'os';
|
||||||
import * as util from '../../src/util';
|
import * as core from '@actions/core';
|
||||||
|
import {getDownloadArchiveExtension} from '../../src/util';
|
||||||
jest.mock('@actions/core');
|
import {HttpClient, HttpClientResponse} from '@actions/http-client';
|
||||||
jest.mock('@actions/tool-cache');
|
|
||||||
jest.mock('@actions/http-client');
|
|
||||||
|
|
||||||
jest.mock('../../src/util', () => ({
|
|
||||||
...jest.requireActual('../../src/util'),
|
|
||||||
extractJdkFile: jest.fn(),
|
|
||||||
getDownloadArchiveExtension: jest.fn(),
|
|
||||||
renameWinArchive: jest.fn(),
|
|
||||||
getGitHubHttpHeaders: jest.fn().mockReturnValue({Accept: 'application/json'})
|
|
||||||
}));
|
|
||||||
|
|
||||||
jest.mock('fs', () => ({
|
|
||||||
...jest.requireActual('fs'),
|
|
||||||
readdirSync: jest.fn()
|
|
||||||
}));
|
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
process.env.NODE_ENV = 'test';
|
|
||||||
|
|
||||||
if (!jest.isMockFunction(http.HttpClient)) {
|
|
||||||
throw new Error('HTTP client must be mocked in tests!');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!jest.isMockFunction(tc.downloadTool)) {
|
|
||||||
throw new Error('Tool cache downloadTool must be mocked in tests!');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('✅ All external dependencies are properly mocked');
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GraalVMDistribution', () => {
|
describe('GraalVMDistribution', () => {
|
||||||
let distribution: GraalVMDistribution;
|
let distribution: GraalVMDistribution;
|
||||||
let mockHttpClient: jest.Mocked<http.HttpClient>;
|
let spyDebug: jest.SpyInstance;
|
||||||
|
let spyHttpClient: jest.SpyInstance;
|
||||||
const defaultOptions: JavaInstallerOptions = {
|
|
||||||
version: '17',
|
|
||||||
architecture: 'x64',
|
|
||||||
packageType: 'jdk',
|
|
||||||
checkLatest: false
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
distribution = new GraalVMDistribution({
|
||||||
|
version: '',
|
||||||
|
architecture: 'x64',
|
||||||
|
packageType: 'jdk',
|
||||||
|
checkLatest: false
|
||||||
|
});
|
||||||
|
|
||||||
distribution = new GraalVMDistribution(defaultOptions);
|
spyDebug = jest.spyOn(core, 'debug').mockImplementation(() => {});
|
||||||
|
|
||||||
mockHttpClient = new http.HttpClient() as jest.Mocked<http.HttpClient>;
|
|
||||||
(distribution as any).http = mockHttpClient;
|
|
||||||
|
|
||||||
(util.getDownloadArchiveExtension as jest.Mock).mockReturnValue('tar.gz');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterEach(() => {
|
||||||
expect(jest.isMockFunction(http.HttpClient)).toBe(true);
|
|
||||||
|
|
||||||
expect(jest.isMockFunction(tc.downloadTool)).toBe(true);
|
|
||||||
expect(jest.isMockFunction(tc.cacheDir)).toBe(true);
|
|
||||||
|
|
||||||
jest.restoreAllMocks();
|
jest.restoreAllMocks();
|
||||||
jest.clearAllMocks();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getPlatform', () => {
|
const setupHttpClientSpy = () => {
|
||||||
it('should map darwin to macos', () => {
|
spyHttpClient = jest.spyOn(HttpClient.prototype, 'head').mockResolvedValue({
|
||||||
const result = distribution.getPlatform('darwin');
|
message: {statusCode: 200} as any, // Minimal mock for IncomingMessage
|
||||||
expect(result).toBe('macos');
|
readBody: jest.fn().mockResolvedValue('')
|
||||||
});
|
} as HttpClientResponse);
|
||||||
|
};
|
||||||
|
|
||||||
it('should map win32 to windows', () => {
|
const testCases = [
|
||||||
const result = distribution.getPlatform('win32');
|
[
|
||||||
expect(result).toBe('windows');
|
'21',
|
||||||
});
|
'21',
|
||||||
|
'https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'21.0.4',
|
||||||
|
'21.0.4',
|
||||||
|
'https://download.oracle.com/graalvm/21/archive/graalvm-jdk-21.0.4_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'17',
|
||||||
|
'17',
|
||||||
|
'https://download.oracle.com/graalvm/17/latest/graalvm-jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'17.0.12',
|
||||||
|
'17.0.12',
|
||||||
|
'https://download.oracle.com/graalvm/17/archive/graalvm-jdk-17.0.12_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
it('should map linux to linux', () => {
|
it.each(testCases)(
|
||||||
const result = distribution.getPlatform('linux');
|
'should find package for version %s',
|
||||||
expect(result).toBe('linux');
|
async (input, expectedVersion, expectedUrl) => {
|
||||||
});
|
setupHttpClientSpy();
|
||||||
|
|
||||||
it('should throw error for unsupported platform', () => {
|
const result = await distribution['findPackageForDownload'](input);
|
||||||
expect(() => distribution.getPlatform('aix' as NodeJS.Platform)).toThrow(
|
const osType = distribution.getPlatform();
|
||||||
"Platform 'aix' is not supported. Supported platforms: 'linux', 'macos', 'windows'"
|
const archiveType = getDownloadArchiveExtension();
|
||||||
);
|
const expectedFormattedUrl = expectedUrl
|
||||||
});
|
.replace('{{OS_TYPE}}', osType)
|
||||||
});
|
.replace('{{ARCHIVE_TYPE}}', archiveType);
|
||||||
|
|
||||||
describe('downloadTool', () => {
|
expect(result.version).toBe(expectedVersion);
|
||||||
const javaRelease = {
|
expect(result.url).toBe(expectedFormattedUrl);
|
||||||
version: '17.0.5',
|
}
|
||||||
url: 'https://example.com/graalvm.tar.gz'
|
);
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
it.each([
|
||||||
(tc.downloadTool as jest.Mock).mockResolvedValue('/tmp/archive.tar.gz');
|
[
|
||||||
(tc.cacheDir as jest.Mock).mockResolvedValue('/cached/java/path');
|
'24-ea',
|
||||||
|
/^https:\/\/github\.com\/graalvm\/oracle-graalvm-ea-builds\/releases\/download\/jdk-24\.0\.0-ea\./
|
||||||
|
]
|
||||||
|
])(
|
||||||
|
'should find EA package for version %s',
|
||||||
|
async (version, expectedUrlPrefix) => {
|
||||||
|
setupHttpClientSpy();
|
||||||
|
|
||||||
(util.extractJdkFile as jest.Mock).mockResolvedValue('/tmp/extracted');
|
const eaDistro = new GraalVMDistribution({
|
||||||
|
version,
|
||||||
|
architecture: '',
|
||||||
|
packageType: 'jdk',
|
||||||
|
checkLatest: false
|
||||||
|
});
|
||||||
|
|
||||||
// Mock renameWinArchive - it returns the same path (no renaming)
|
const versionWithoutEA = version.split('-')[0];
|
||||||
// But it appears the implementation might not even call this
|
const result = await eaDistro['findPackageForDownload'](versionWithoutEA);
|
||||||
(util.renameWinArchive as jest.Mock).mockImplementation((p: string) => p);
|
|
||||||
|
|
||||||
(util.getDownloadArchiveExtension as jest.Mock).mockReturnValue('tar.gz');
|
expect(result.url).toEqual(expect.stringMatching(expectedUrlPrefix));
|
||||||
|
}
|
||||||
(fs.readdirSync as jest.Mock).mockReturnValue(['graalvm-jdk-17.0.5']);
|
);
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
['amd64', ['x64', 'amd64']],
|
||||||
|
['arm64', ['aarch64', 'arm64']]
|
||||||
|
])(
|
||||||
|
'defaults to os.arch(): %s mapped to distro arch: %s',
|
||||||
|
async (osArch: string, distroArchs: string[]) => {
|
||||||
jest
|
jest
|
||||||
.spyOn(distribution as any, 'getToolcacheVersionName')
|
.spyOn(os, 'arch')
|
||||||
.mockImplementation(version => version);
|
.mockReturnValue(osArch as ReturnType<typeof os.arch>);
|
||||||
});
|
|
||||||
|
|
||||||
it('should download, extract and cache the tool successfully', async () => {
|
const distribution = new GraalVMDistribution({
|
||||||
const result = await (distribution as any).downloadTool(javaRelease);
|
version: '21',
|
||||||
|
architecture: '', // to get default value
|
||||||
// Verify the download was initiated
|
packageType: 'jdk',
|
||||||
expect(tc.downloadTool).toHaveBeenCalledWith(javaRelease.url);
|
checkLatest: false
|
||||||
|
|
||||||
// The implementation uses the original path for extraction
|
|
||||||
expect(util.extractJdkFile).toHaveBeenCalledWith(
|
|
||||||
'/tmp/archive.tar.gz', // Original path
|
|
||||||
'tar.gz'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Verify directory reading
|
|
||||||
expect(fs.readdirSync).toHaveBeenCalledWith('/tmp/extracted');
|
|
||||||
|
|
||||||
// Verify caching with correct parameters
|
|
||||||
expect(tc.cacheDir).toHaveBeenCalledWith(
|
|
||||||
path.join('/tmp/extracted', 'graalvm-jdk-17.0.5'),
|
|
||||||
'Java_GraalVM_jdk',
|
|
||||||
'17.0.5',
|
|
||||||
'x64'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Verify the result
|
|
||||||
expect(result).toEqual({
|
|
||||||
version: '17.0.5',
|
|
||||||
path: '/cached/java/path'
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verify logging
|
const osType = distribution.getPlatform();
|
||||||
expect(core.info).toHaveBeenCalledWith(
|
if (osType === 'windows' && distroArchs.includes('aarch64')) {
|
||||||
'Downloading Java 17.0.5 (GraalVM) from https://example.com/graalvm.tar.gz ...'
|
return; // skip, aarch64 is not available for Windows
|
||||||
);
|
|
||||||
expect(core.info).toHaveBeenCalledWith('Extracting Java archive...');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should verify that renameWinArchive is available but may not be called', () => {
|
|
||||||
// Just verify the mock is set up correctly
|
|
||||||
const originalPath = '/tmp/archive.tar.gz';
|
|
||||||
|
|
||||||
// Call the mock directly to verify it works
|
|
||||||
const result = util.renameWinArchive(originalPath);
|
|
||||||
|
|
||||||
// Verify it returns the same path (no renaming)
|
|
||||||
expect(result).toBe(originalPath);
|
|
||||||
expect(util.renameWinArchive).toHaveBeenCalledWith(originalPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle different archive extensions', async () => {
|
|
||||||
// Test with a .zip file
|
|
||||||
(util.getDownloadArchiveExtension as jest.Mock).mockReturnValue('zip');
|
|
||||||
(tc.downloadTool as jest.Mock).mockResolvedValue('/tmp/archive.zip');
|
|
||||||
|
|
||||||
const zipRelease = {
|
|
||||||
version: '17.0.5',
|
|
||||||
url: 'https://example.com/graalvm.zip'
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await (distribution as any).downloadTool(zipRelease);
|
|
||||||
|
|
||||||
expect(util.extractJdkFile).toHaveBeenCalledWith(
|
|
||||||
'/tmp/archive.zip',
|
|
||||||
'zip'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
version: '17.0.5',
|
|
||||||
path: '/cached/java/path'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('findPackageForDownload', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
jest.spyOn(distribution, 'getPlatform').mockReturnValue('linux');
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('stable builds', () => {
|
|
||||||
it('should construct correct URL for specific version', async () => {
|
|
||||||
const mockResponse = {
|
|
||||||
message: {statusCode: 200}
|
|
||||||
} as http.HttpClientResponse;
|
|
||||||
mockHttpClient.head.mockResolvedValue(mockResponse);
|
|
||||||
|
|
||||||
const result = await (distribution as any).findPackageForDownload(
|
|
||||||
'17.0.5'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
url: 'https://download.oracle.com/graalvm/17/archive/graalvm-jdk-17.0.5_linux-x64_bin.tar.gz',
|
|
||||||
version: '17.0.5'
|
|
||||||
});
|
|
||||||
expect(mockHttpClient.head).toHaveBeenCalledWith(result.url);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should construct correct URL for major version (latest)', async () => {
|
|
||||||
const mockResponse = {
|
|
||||||
message: {statusCode: 200}
|
|
||||||
} as http.HttpClientResponse;
|
|
||||||
mockHttpClient.head.mockResolvedValue(mockResponse);
|
|
||||||
|
|
||||||
const result = await (distribution as any).findPackageForDownload('21');
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
url: 'https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_linux-x64_bin.tar.gz',
|
|
||||||
version: '21'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error for unsupported architecture', async () => {
|
|
||||||
distribution = new GraalVMDistribution({
|
|
||||||
...defaultOptions,
|
|
||||||
architecture: 'x86'
|
|
||||||
});
|
|
||||||
(distribution as any).http = mockHttpClient;
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('17')
|
|
||||||
).rejects.toThrow('Unsupported architecture: x86');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error for JDK versions less than 17', async () => {
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('11')
|
|
||||||
).rejects.toThrow('GraalVM is only supported for JDK 17 and later');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error for non-jdk package types', async () => {
|
|
||||||
distribution = new GraalVMDistribution({
|
|
||||||
...defaultOptions,
|
|
||||||
packageType: 'jre'
|
|
||||||
});
|
|
||||||
(distribution as any).http = mockHttpClient;
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('17')
|
|
||||||
).rejects.toThrow('GraalVM provides only the `jdk` package type');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when file not found (404)', async () => {
|
|
||||||
const mockResponse = {
|
|
||||||
message: {statusCode: 404}
|
|
||||||
} as http.HttpClientResponse;
|
|
||||||
mockHttpClient.head.mockResolvedValue(mockResponse);
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('17.0.99')
|
|
||||||
).rejects.toThrow('Could not find GraalVM for SemVer 17.0.99');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error for other HTTP errors', async () => {
|
|
||||||
const mockResponse = {
|
|
||||||
message: {statusCode: 500}
|
|
||||||
} as http.HttpClientResponse;
|
|
||||||
mockHttpClient.head.mockResolvedValue(mockResponse);
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('17')
|
|
||||||
).rejects.toThrow(
|
|
||||||
'Http request for GraalVM failed with status code: 500'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('EA builds', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
distribution = new GraalVMDistribution(defaultOptions);
|
|
||||||
(distribution as any).http = mockHttpClient;
|
|
||||||
(distribution as any).stable = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should delegate to findEABuildDownloadUrl for unstable versions', async () => {
|
|
||||||
const currentPlatform =
|
|
||||||
process.platform === 'win32' ? 'windows' : process.platform;
|
|
||||||
|
|
||||||
const mockEAVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-x64_bin.tar.gz'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
arch: 'aarch64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-aarch64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: mockEAVersions,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('x64');
|
|
||||||
|
|
||||||
const result = await (distribution as any).findPackageForDownload('23');
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
url: 'https://example.com/download/graalvm-jdk-23_linux-x64_bin.tar.gz',
|
|
||||||
version: '23-ea-20240716'
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(mockHttpClient.getJson).toHaveBeenCalledWith(
|
|
||||||
'https://api.github.com/repos/graalvm/oracle-graalvm-ea-builds/contents/versions/23-ea.json?ref=main',
|
|
||||||
{Accept: 'application/json'}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when no latest EA version found', async () => {
|
|
||||||
const currentPlatform =
|
|
||||||
process.platform === 'win32' ? 'windows' : process.platform;
|
|
||||||
|
|
||||||
const mockEAVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: false,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-x64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: mockEAVersions,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('x64');
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('23')
|
|
||||||
).rejects.toThrow("Unable to find latest version for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when no matching file for architecture in EA build', async () => {
|
|
||||||
const currentPlatform =
|
|
||||||
process.platform === 'win32' ? 'windows' : process.platform;
|
|
||||||
|
|
||||||
const mockEAVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'arm64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-arm64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: mockEAVersions,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('x64');
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('23')
|
|
||||||
).rejects.toThrow("Unable to find file metadata for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when no matching platform in EA build', async () => {
|
|
||||||
const mockEAVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: 'different-platform',
|
|
||||||
filename: 'graalvm-jdk-23_different-x64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: mockEAVersions,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('x64');
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('23')
|
|
||||||
).rejects.toThrow("Unable to find file metadata for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when filename does not start with graalvm-jdk-', async () => {
|
|
||||||
const currentPlatform =
|
|
||||||
process.platform === 'win32' ? 'windows' : process.platform;
|
|
||||||
|
|
||||||
const mockEAVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'wrong-prefix-23_linux-x64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: mockEAVersions,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('x64');
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('23')
|
|
||||||
).rejects.toThrow("Unable to find file metadata for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when EA version JSON is not found', async () => {
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: null,
|
|
||||||
statusCode: 404,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findPackageForDownload('23')
|
|
||||||
).rejects.toThrow("No GraalVM EA build found for version '23-ea'");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('findEABuildDownloadUrl', () => {
|
|
||||||
const currentPlatform =
|
|
||||||
process.platform === 'win32' ? 'windows' : process.platform;
|
|
||||||
|
|
||||||
const mockEAVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-x64_bin.tar.gz'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
arch: 'aarch64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-aarch64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
version: '23-ea-20240709',
|
|
||||||
latest: false,
|
|
||||||
download_base_url: 'https://example.com/old/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-x64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
let fetchEASpy: jest.SpyInstance;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
fetchEASpy = jest.spyOn(distribution as any, 'fetchEAJson');
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('x64');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should find latest version and return correct URL', async () => {
|
|
||||||
fetchEASpy.mockResolvedValue(mockEAVersions);
|
|
||||||
|
|
||||||
const result = await (distribution as any).findEABuildDownloadUrl(
|
|
||||||
'23-ea'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchEASpy).toHaveBeenCalledWith('23-ea');
|
|
||||||
expect(result).toEqual({
|
|
||||||
url: 'https://example.com/download/graalvm-jdk-23_linux-x64_bin.tar.gz',
|
|
||||||
version: '23-ea-20240716'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when no latest version found', async () => {
|
|
||||||
const noLatestVersions = mockEAVersions.map(v => ({...v, latest: false}));
|
|
||||||
fetchEASpy.mockResolvedValue(noLatestVersions);
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findEABuildDownloadUrl('23-ea')
|
|
||||||
).rejects.toThrow("Unable to find latest version for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when no matching file for architecture', async () => {
|
|
||||||
const wrongArchVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'arm',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'graalvm-jdk-23_linux-arm_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
fetchEASpy.mockResolvedValue(wrongArchVersions);
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findEABuildDownloadUrl('23-ea')
|
|
||||||
).rejects.toThrow("Unable to find file metadata for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when filename does not start with graalvm-jdk-', async () => {
|
|
||||||
const badFilenameVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: currentPlatform,
|
|
||||||
filename: 'wrong-name.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
fetchEASpy.mockResolvedValue(badFilenameVersions);
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findEABuildDownloadUrl('23-ea')
|
|
||||||
).rejects.toThrow("Unable to find file metadata for '23-ea'");
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should work with aarch64 architecture', async () => {
|
|
||||||
jest
|
|
||||||
.spyOn(distribution as any, 'distributionArchitecture')
|
|
||||||
.mockReturnValue('aarch64');
|
|
||||||
|
|
||||||
fetchEASpy.mockResolvedValue(mockEAVersions);
|
|
||||||
|
|
||||||
const result = await (distribution as any).findEABuildDownloadUrl(
|
|
||||||
'23-ea'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
url: 'https://example.com/download/graalvm-jdk-23_linux-aarch64_bin.tar.gz',
|
|
||||||
version: '23-ea-20240716'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when platform does not match', async () => {
|
|
||||||
const wrongPlatformVersions = [
|
|
||||||
{
|
|
||||||
version: '23-ea-20240716',
|
|
||||||
latest: true,
|
|
||||||
download_base_url: 'https://example.com/download/',
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
arch: 'x64',
|
|
||||||
platform: 'different-platform',
|
|
||||||
filename: 'graalvm-jdk-23_different-x64_bin.tar.gz'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
fetchEASpy.mockResolvedValue(wrongPlatformVersions);
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
(distribution as any).findEABuildDownloadUrl('23-ea')
|
|
||||||
).rejects.toThrow("Unable to find file metadata for '23-ea'");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('fetchEAJson', () => {
|
|
||||||
it('should fetch and return EA version data', async () => {
|
|
||||||
const mockData = [{version: '23-ea', files: []}];
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: mockData,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await (distribution as any).fetchEAJson('23-ea');
|
|
||||||
|
|
||||||
expect(mockHttpClient.getJson).toHaveBeenCalledWith(
|
|
||||||
'https://api.github.com/repos/graalvm/oracle-graalvm-ea-builds/contents/versions/23-ea.json?ref=main',
|
|
||||||
{Accept: 'application/json'}
|
|
||||||
);
|
|
||||||
expect(result).toEqual(mockData);
|
|
||||||
expect(core.debug).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when no data returned', async () => {
|
|
||||||
mockHttpClient.getJson.mockResolvedValue({
|
|
||||||
result: null,
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
await expect((distribution as any).fetchEAJson('23-ea')).rejects.toThrow(
|
|
||||||
"No GraalVM EA build found for version '23-ea'. Please check if the version is correct."
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle HTTP errors properly', async () => {
|
|
||||||
mockHttpClient.getJson.mockRejectedValue(new Error('Network error'));
|
|
||||||
|
|
||||||
await expect((distribution as any).fetchEAJson('23-ea')).rejects.toThrow(
|
|
||||||
'Network error'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Integration tests', () => {
|
|
||||||
it('should handle different architectures correctly', async () => {
|
|
||||||
const architectures = ['x64', 'aarch64'];
|
|
||||||
|
|
||||||
for (const arch of architectures) {
|
|
||||||
distribution = new GraalVMDistribution({
|
|
||||||
...defaultOptions,
|
|
||||||
architecture: arch
|
|
||||||
});
|
|
||||||
(distribution as any).http = mockHttpClient;
|
|
||||||
|
|
||||||
const mockResponse = {
|
|
||||||
message: {statusCode: 200}
|
|
||||||
} as http.HttpClientResponse;
|
|
||||||
mockHttpClient.head.mockResolvedValue(mockResponse);
|
|
||||||
|
|
||||||
const result = await (distribution as any).findPackageForDownload('17');
|
|
||||||
expect(result.url).toContain(arch);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle different platforms correctly', async () => {
|
|
||||||
const platforms = [
|
|
||||||
{process: 'darwin', expected: 'macos'},
|
|
||||||
{process: 'win32', expected: 'windows'},
|
|
||||||
{process: 'linux', expected: 'linux'}
|
|
||||||
];
|
|
||||||
|
|
||||||
const originalPlatform = process.platform;
|
|
||||||
|
|
||||||
for (const {process: proc, expected} of platforms) {
|
|
||||||
Object.defineProperty(process, 'platform', {
|
|
||||||
value: proc,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
distribution = new GraalVMDistribution(defaultOptions);
|
|
||||||
(distribution as any).http = mockHttpClient;
|
|
||||||
|
|
||||||
const mockResponse = {
|
|
||||||
message: {statusCode: 200}
|
|
||||||
} as http.HttpClientResponse;
|
|
||||||
mockHttpClient.head.mockResolvedValue(mockResponse);
|
|
||||||
|
|
||||||
const result = await (distribution as any).findPackageForDownload('17');
|
|
||||||
expect(result.url).toContain(expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(process, 'platform', {
|
const archiveType = getDownloadArchiveExtension();
|
||||||
value: originalPlatform,
|
const result = await distribution['findPackageForDownload']('21');
|
||||||
configurable: true
|
|
||||||
});
|
const expectedUrls = distroArchs.map(
|
||||||
|
distroArch =>
|
||||||
|
`https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_${osType}-${distroArch}_bin.${archiveType}`
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(expectedUrls).toContain(result.url);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
it('should throw an error for unsupported versions', async () => {
|
||||||
|
setupHttpClientSpy();
|
||||||
|
|
||||||
|
const unsupportedVersions = ['8', '11'];
|
||||||
|
for (const version of unsupportedVersions) {
|
||||||
|
await expect(
|
||||||
|
distribution['findPackageForDownload'](version)
|
||||||
|
).rejects.toThrow(/GraalVM is only supported for JDK 17 and later/);
|
||||||
|
}
|
||||||
|
|
||||||
|
const unavailableEADistro = new GraalVMDistribution({
|
||||||
|
version: '17-ea',
|
||||||
|
architecture: '',
|
||||||
|
packageType: 'jdk',
|
||||||
|
checkLatest: false
|
||||||
});
|
});
|
||||||
|
await expect(
|
||||||
|
unavailableEADistro['findPackageForDownload']('17')
|
||||||
|
).rejects.toThrow(
|
||||||
|
`No GraalVM EA build found for version '17-ea'. Please check if the version is correct.`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
2
dist/cleanup/index.js
vendored
2
dist/cleanup/index.js
vendored
@ -94747,7 +94747,7 @@ function convertVersionToSemver(version) {
|
|||||||
}
|
}
|
||||||
exports.convertVersionToSemver = convertVersionToSemver;
|
exports.convertVersionToSemver = convertVersionToSemver;
|
||||||
function getGitHubHttpHeaders() {
|
function getGitHubHttpHeaders() {
|
||||||
const resolvedToken = core.getInput('token') || process.env.GITHUB_TOKEN;
|
const resolvedToken = process.env.GITHUB_TOKEN || core.getInput('token');
|
||||||
const auth = !resolvedToken ? undefined : `token ${resolvedToken}`;
|
const auth = !resolvedToken ? undefined : `token ${resolvedToken}`;
|
||||||
const headers = {
|
const headers = {
|
||||||
accept: 'application/vnd.github.VERSION.raw'
|
accept: 'application/vnd.github.VERSION.raw'
|
||||||
|
|||||||
2
dist/setup/index.js
vendored
2
dist/setup/index.js
vendored
@ -132772,7 +132772,7 @@ function convertVersionToSemver(version) {
|
|||||||
}
|
}
|
||||||
exports.convertVersionToSemver = convertVersionToSemver;
|
exports.convertVersionToSemver = convertVersionToSemver;
|
||||||
function getGitHubHttpHeaders() {
|
function getGitHubHttpHeaders() {
|
||||||
const resolvedToken = core.getInput('token') || process.env.GITHUB_TOKEN;
|
const resolvedToken = process.env.GITHUB_TOKEN || core.getInput('token');
|
||||||
const auth = !resolvedToken ? undefined : `token ${resolvedToken}`;
|
const auth = !resolvedToken ? undefined : `token ${resolvedToken}`;
|
||||||
const headers = {
|
const headers = {
|
||||||
accept: 'application/vnd.github.VERSION.raw'
|
accept: 'application/vnd.github.VERSION.raw'
|
||||||
|
|||||||
@ -184,7 +184,7 @@ export function convertVersionToSemver(version: number[] | string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getGitHubHttpHeaders(): OutgoingHttpHeaders {
|
export function getGitHubHttpHeaders(): OutgoingHttpHeaders {
|
||||||
const resolvedToken = core.getInput('token') || process.env.GITHUB_TOKEN;
|
const resolvedToken = process.env.GITHUB_TOKEN || core.getInput('token');
|
||||||
const auth = !resolvedToken ? undefined : `token ${resolvedToken}`;
|
const auth = !resolvedToken ? undefined : `token ${resolvedToken}`;
|
||||||
|
|
||||||
const headers: OutgoingHttpHeaders = {
|
const headers: OutgoingHttpHeaders = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user