mirror of
https://gitea.com/actions/setup-java.git
synced 2026-07-10 19:31:55 +08:00
Support pinning java-version as "latest"
Add a `latest` alias for the `java-version` input that floats to the newest available stable (GA) release. It is normalized to the SemVer wildcard at the base-installer layer and always resolves from remote (like `check-latest: true`). List-based distributions resolve it automatically via the existing newest-first matching. Corretto selects its newest available major; Oracle and GraalVM look up the newest GA major via the Adoptium API and request it, failing with an actionable error if that major isn't published yet. The jdkfile distribution rejects `latest`. Closes #832 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -420,6 +420,29 @@ describe('setupJava', () => {
|
||||
expect(spyCoreInfo).not.toHaveBeenCalledWith('Trying to download...');
|
||||
});
|
||||
|
||||
it('should resolve the latest version from remote when java-version is "latest", even if a version is cached', async () => {
|
||||
mockJavaBase = new EmptyJavaBase({
|
||||
version: 'latest',
|
||||
architecture: 'x86',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
});
|
||||
|
||||
await expect(mockJavaBase.setupJava()).resolves.toEqual({
|
||||
version: actualJavaVersion,
|
||||
path: javaPathInstalled
|
||||
});
|
||||
|
||||
// `latest` must bypass the tool-cache short-circuit and always resolve remotely
|
||||
expect(spyCoreInfo).toHaveBeenCalledWith(
|
||||
'Trying to resolve the latest version from remote'
|
||||
);
|
||||
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
|
||||
expect(spyCoreInfo).not.toHaveBeenCalledWith(
|
||||
`Resolved Java ${installedJavaVersion} from tool-cache`
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
{
|
||||
@@ -744,11 +767,14 @@ describe('normalizeVersion', () => {
|
||||
const DummyJavaBase = JavaBase as any;
|
||||
|
||||
it.each([
|
||||
['11', {version: '11', stable: true}],
|
||||
['11.0', {version: '11.0', stable: true}],
|
||||
['11.0.10', {version: '11.0.10', stable: true}],
|
||||
['11-ea', {version: '11', stable: false}],
|
||||
['11.0.2-ea', {version: '11.0.2', stable: false}]
|
||||
['11', {version: '11', stable: true, latest: false}],
|
||||
['11.0', {version: '11.0', stable: true, latest: false}],
|
||||
['11.0.10', {version: '11.0.10', stable: true, latest: false}],
|
||||
['11-ea', {version: '11', stable: false, latest: false}],
|
||||
['11.0.2-ea', {version: '11.0.2', stable: false, latest: false}],
|
||||
['latest', {version: 'x', stable: true, latest: true}],
|
||||
['LATEST', {version: 'x', stable: true, latest: true}],
|
||||
[' Latest ', {version: 'x', stable: true, latest: true}]
|
||||
])('normalizeVersion from %s to %s', (input, expected) => {
|
||||
expect(DummyJavaBase.prototype.normalizeVersion.call(null, input)).toEqual(
|
||||
expected
|
||||
|
||||
@@ -204,6 +204,24 @@ describe('getAvailableVersions', () => {
|
||||
expect(availableVersion.url).toBe(expectedLink);
|
||||
});
|
||||
|
||||
it('with latest resolves to the newest available major version', async () => {
|
||||
const distribution = new CorrettoDistribution({
|
||||
version: 'latest',
|
||||
architecture: 'x64',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
});
|
||||
mockPlatform(distribution, 'linux');
|
||||
|
||||
const availableVersion =
|
||||
await distribution['findPackageForDownload']('x');
|
||||
expect(availableVersion).not.toBeNull();
|
||||
// 18 is the newest major present in the mocked Corretto index
|
||||
expect(availableVersion.url).toBe(
|
||||
'https://corretto.aws/downloads/resources/18.0.0.37.1/amazon-corretto-18.0.0.37.1-linux-x64.tar.gz'
|
||||
);
|
||||
});
|
||||
|
||||
it('with unstable version expect to throw not supported error', async () => {
|
||||
const version = '18.0.1-ea';
|
||||
const distribution = new CorrettoDistribution({
|
||||
|
||||
@@ -417,6 +417,61 @@ describe('GraalVMDistribution', () => {
|
||||
);
|
||||
});
|
||||
|
||||
describe('latest alias', () => {
|
||||
it('resolves the newest major version from the Adoptium API', async () => {
|
||||
const latestDistribution = new GraalVMDistribution({
|
||||
...defaultOptions,
|
||||
version: 'latest'
|
||||
});
|
||||
(latestDistribution as any).http = mockHttpClient;
|
||||
jest
|
||||
.spyOn(latestDistribution, 'getPlatform')
|
||||
.mockReturnValue('linux');
|
||||
mockHttpClient.getJson.mockResolvedValue({
|
||||
statusCode: 200,
|
||||
result: {most_recent_feature_release: 25},
|
||||
headers: {}
|
||||
});
|
||||
mockHttpClient.head.mockResolvedValue({
|
||||
message: {statusCode: 200}
|
||||
});
|
||||
|
||||
const result = await (
|
||||
latestDistribution as any
|
||||
).findPackageForDownload('x');
|
||||
|
||||
expect(result).toEqual({
|
||||
url: 'https://download.oracle.com/graalvm/25/latest/graalvm-jdk-25_linux-x64_bin.tar.gz',
|
||||
version: '25'
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an actionable error when the latest major is not yet available', async () => {
|
||||
const latestDistribution = new GraalVMDistribution({
|
||||
...defaultOptions,
|
||||
version: 'latest'
|
||||
});
|
||||
(latestDistribution as any).http = mockHttpClient;
|
||||
jest
|
||||
.spyOn(latestDistribution, 'getPlatform')
|
||||
.mockReturnValue('linux');
|
||||
mockHttpClient.getJson.mockResolvedValue({
|
||||
statusCode: 200,
|
||||
result: {most_recent_feature_release: 25},
|
||||
headers: {}
|
||||
});
|
||||
mockHttpClient.head.mockResolvedValue({
|
||||
message: {statusCode: 404}
|
||||
});
|
||||
|
||||
await expect(
|
||||
(latestDistribution as any).findPackageForDownload('x')
|
||||
).rejects.toThrow(
|
||||
/is not yet available for the GraalVM distribution/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw error for JDK versions less than 17', async () => {
|
||||
await expect(
|
||||
(distribution as any).findPackageForDownload('11')
|
||||
|
||||
@@ -170,6 +170,20 @@ describe('setupJava', () => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('throws for the latest alias since jdkfile has no version list', async () => {
|
||||
const inputs = {
|
||||
version: 'latest',
|
||||
architecture: 'x86',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
};
|
||||
|
||||
mockJavaBase = new LocalDistribution(inputs, expectedJdkFile);
|
||||
await expect(mockJavaBase.setupJava()).rejects.toThrow(
|
||||
"The 'latest' version alias is not supported for the 'jdkfile' distribution. Please specify a concrete version."
|
||||
);
|
||||
});
|
||||
|
||||
it('java is resolved from toolcache, jdkfile is untouched', async () => {
|
||||
const inputs = {
|
||||
version: actualJavaVersion,
|
||||
|
||||
@@ -174,3 +174,59 @@ describe('findPackageForDownload', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('findPackageForDownload with latest', () => {
|
||||
let spyHttpClientHead: any;
|
||||
let spyHttpClientGetJson: any;
|
||||
|
||||
beforeEach(() => {
|
||||
(core.debug as jest.Mock).mockImplementation(() => {});
|
||||
(core.error as jest.Mock).mockImplementation(() => {});
|
||||
spyHttpClientGetJson = jest.spyOn(HttpClient.prototype, 'getJson');
|
||||
spyHttpClientGetJson.mockResolvedValue({
|
||||
statusCode: 200,
|
||||
result: {most_recent_feature_release: 25},
|
||||
headers: {}
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('resolves the newest major version from the Adoptium API', async () => {
|
||||
spyHttpClientHead = jest.spyOn(HttpClient.prototype, 'head');
|
||||
spyHttpClientHead.mockResolvedValue({message: {statusCode: 200}});
|
||||
|
||||
const distribution = new OracleDistribution({
|
||||
version: 'latest',
|
||||
architecture: 'x64',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
});
|
||||
|
||||
const result = await distribution['findPackageForDownload']('x');
|
||||
const osType = distribution.getPlatform();
|
||||
const archiveType = getDownloadArchiveExtension();
|
||||
|
||||
expect(result.version).toBe('25');
|
||||
expect(result.url).toBe(
|
||||
`https://download.oracle.com/java/25/latest/jdk-25_${osType}-x64_bin.${archiveType}`
|
||||
);
|
||||
});
|
||||
|
||||
it('throws an actionable error when the latest major is not yet available', async () => {
|
||||
spyHttpClientHead = jest.spyOn(HttpClient.prototype, 'head');
|
||||
spyHttpClientHead.mockResolvedValue({message: {statusCode: 404}});
|
||||
|
||||
const distribution = new OracleDistribution({
|
||||
version: 'latest',
|
||||
architecture: 'x64',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
});
|
||||
|
||||
await expect(distribution['findPackageForDownload']('x')).rejects.toThrow(
|
||||
/is not yet available for the Oracle JDK distribution/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -312,6 +312,24 @@ describe('findPackageForDownload', () => {
|
||||
expect(resolvedVersion.signatureUrl).toBeDefined();
|
||||
});
|
||||
|
||||
it('version "latest" is normalized to the newest available version', async () => {
|
||||
const distribution = new TemurinDistribution(
|
||||
{
|
||||
version: 'latest',
|
||||
architecture: 'x64',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
},
|
||||
TemurinImplementation.Hotspot
|
||||
);
|
||||
distribution['getAvailableVersions'] = async () => manifestData as any;
|
||||
// normalizeVersion turns `latest` into the wildcard carried on `this.version`
|
||||
const resolvedVersion = await distribution['findPackageForDownload'](
|
||||
distribution['version']
|
||||
);
|
||||
expect(resolvedVersion.version).toBe('16.0.2+7');
|
||||
});
|
||||
|
||||
it('version is found but binaries list is empty', async () => {
|
||||
const distribution = new TemurinDistribution(
|
||||
{
|
||||
|
||||
@@ -56,7 +56,8 @@ const {
|
||||
isVersionSatisfies,
|
||||
isCacheFeatureAvailable,
|
||||
isGhes,
|
||||
validatePaginationUrl
|
||||
validatePaginationUrl,
|
||||
getLatestMajorVersion
|
||||
} = await import('../src/util.js');
|
||||
|
||||
describe('isVersionSatisfies', () => {
|
||||
@@ -400,3 +401,33 @@ describe('isGhes', () => {
|
||||
expect(isGhes()).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getLatestMajorVersion', () => {
|
||||
const makeHttp = (getJson: jest.Mock) =>
|
||||
({getJson}) as unknown as import('@actions/http-client').HttpClient;
|
||||
|
||||
it('returns most_recent_feature_release from the Adoptium API', async () => {
|
||||
const getJson = jest.fn(async () => ({
|
||||
statusCode: 200,
|
||||
result: {most_recent_feature_release: 25},
|
||||
headers: {}
|
||||
}));
|
||||
|
||||
await expect(getLatestMajorVersion(makeHttp(getJson))).resolves.toBe(25);
|
||||
expect(getJson).toHaveBeenCalledWith(
|
||||
'https://api.adoptium.net/v3/info/available_releases'
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when the response does not contain a usable value', async () => {
|
||||
const getJson = jest.fn(async () => ({
|
||||
statusCode: 200,
|
||||
result: {},
|
||||
headers: {}
|
||||
}));
|
||||
|
||||
await expect(getLatestMajorVersion(makeHttp(getJson))).rejects.toThrow(
|
||||
'Could not determine the latest available Java major version'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user