|
| 1 | +import {GraalVMDistribution} from '../../src/distributions/graalvm/installer'; |
| 2 | +import os from 'os'; |
| 3 | +import * as core from '@actions/core'; |
| 4 | +import {getDownloadArchiveExtension} from '../../src/util'; |
| 5 | +import {HttpClient} from '@actions/http-client'; |
| 6 | + |
| 7 | +describe('findPackageForDownload', () => { |
| 8 | + let distribution: GraalVMDistribution; |
| 9 | + let spyDebug: jest.SpyInstance; |
| 10 | + let spyHttpClient: jest.SpyInstance; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + distribution = new GraalVMDistribution({ |
| 14 | + version: '', |
| 15 | + architecture: 'x64', |
| 16 | + packageType: 'jdk', |
| 17 | + checkLatest: false |
| 18 | + }); |
| 19 | + |
| 20 | + spyDebug = jest.spyOn(core, 'debug'); |
| 21 | + spyDebug.mockImplementation(() => {}); |
| 22 | + }); |
| 23 | + |
| 24 | + it.each([ |
| 25 | + [ |
| 26 | + '21', |
| 27 | + '21', |
| 28 | + 'https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' |
| 29 | + ], |
| 30 | + [ |
| 31 | + '21.0.4', |
| 32 | + '21.0.4', |
| 33 | + 'https://download.oracle.com/graalvm/21/archive/graalvm-jdk-21.0.4_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' |
| 34 | + ], |
| 35 | + [ |
| 36 | + '17', |
| 37 | + '17', |
| 38 | + 'https://download.oracle.com/graalvm/17/latest/graalvm-jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' |
| 39 | + ], |
| 40 | + [ |
| 41 | + '17.0.12', |
| 42 | + '17.0.12', |
| 43 | + 'https://download.oracle.com/graalvm/17/archive/graalvm-jdk-17.0.12_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' |
| 44 | + ] |
| 45 | + ])('version is %s -> %s', async (input, expectedVersion, expectedUrl) => { |
| 46 | + /* Needed only for this particular test because /latest/ urls tend to change */ |
| 47 | + spyHttpClient = jest.spyOn(HttpClient.prototype, 'head'); |
| 48 | + spyHttpClient.mockReturnValue( |
| 49 | + Promise.resolve({ |
| 50 | + message: { |
| 51 | + statusCode: 200 |
| 52 | + } |
| 53 | + }) |
| 54 | + ); |
| 55 | + |
| 56 | + const result = await distribution['findPackageForDownload'](input); |
| 57 | + |
| 58 | + jest.restoreAllMocks(); |
| 59 | + |
| 60 | + expect(result.version).toBe(expectedVersion); |
| 61 | + const osType = distribution.getPlatform(); |
| 62 | + const archiveType = getDownloadArchiveExtension(); |
| 63 | + const url = expectedUrl |
| 64 | + .replace('{{OS_TYPE}}', osType) |
| 65 | + .replace('{{ARCHIVE_TYPE}}', archiveType); |
| 66 | + expect(result.url).toBe(url); |
| 67 | + }); |
| 68 | + |
| 69 | + it.each([ |
| 70 | + [ |
| 71 | + '24-ea', |
| 72 | + /^https:\/\/github\.com\/graalvm\/oracle-graalvm-ea-builds\/releases\/download\/jdk-24\.0\.0-ea\./ |
| 73 | + ] |
| 74 | + ])('version is %s -> %s', async (version, expectedUrlPrefix) => { |
| 75 | + /* Needed only for this particular test because /latest/ urls tend to change */ |
| 76 | + spyHttpClient = jest.spyOn(HttpClient.prototype, 'head'); |
| 77 | + spyHttpClient.mockReturnValue( |
| 78 | + Promise.resolve({ |
| 79 | + message: { |
| 80 | + statusCode: 200 |
| 81 | + } |
| 82 | + }) |
| 83 | + ); |
| 84 | + |
| 85 | + const eaDistro = new GraalVMDistribution({ |
| 86 | + version, |
| 87 | + architecture: '', // to get default value |
| 88 | + packageType: 'jdk', |
| 89 | + checkLatest: false |
| 90 | + }); |
| 91 | + |
| 92 | + const versionWithoutEA = version.split('-')[0]; |
| 93 | + const result = await eaDistro['findPackageForDownload'](versionWithoutEA); |
| 94 | + |
| 95 | + jest.restoreAllMocks(); |
| 96 | + |
| 97 | + expect(result.url).toEqual(expect.stringMatching(expectedUrlPrefix)); |
| 98 | + }); |
| 99 | + |
| 100 | + it.each([ |
| 101 | + ['amd64', 'x64'], |
| 102 | + ['arm64', 'aarch64'] |
| 103 | + ])( |
| 104 | + 'defaults to os.arch(): %s mapped to distro arch: %s', |
| 105 | + async (osArch: string, distroArch: string) => { |
| 106 | + jest.spyOn(os, 'arch').mockReturnValue(osArch); |
| 107 | + jest.spyOn(os, 'platform').mockReturnValue('linux'); |
| 108 | + |
| 109 | + const version = '21'; |
| 110 | + const distro = new GraalVMDistribution({ |
| 111 | + version, |
| 112 | + architecture: '', // to get default value |
| 113 | + packageType: 'jdk', |
| 114 | + checkLatest: false |
| 115 | + }); |
| 116 | + |
| 117 | + const osType = distribution.getPlatform(); |
| 118 | + if (osType === 'windows' && distroArch == 'aarch64') { |
| 119 | + return; // skip, aarch64 is not available for Windows |
| 120 | + } |
| 121 | + const archiveType = getDownloadArchiveExtension(); |
| 122 | + const result = await distro['findPackageForDownload'](version); |
| 123 | + const expectedUrl = `https://download.oracle.com/graalvm/21/latest/graalvm-jdk-21_${osType}-${distroArch}_bin.${archiveType}`; |
| 124 | + |
| 125 | + expect(result.url).toBe(expectedUrl); |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + it('should throw an error', async () => { |
| 130 | + await expect(distribution['findPackageForDownload']('8')).rejects.toThrow( |
| 131 | + /GraalVM is only supported for JDK 17 and later/ |
| 132 | + ); |
| 133 | + await expect(distribution['findPackageForDownload']('11')).rejects.toThrow( |
| 134 | + /GraalVM is only supported for JDK 17 and later/ |
| 135 | + ); |
| 136 | + await expect(distribution['findPackageForDownload']('18')).rejects.toThrow( |
| 137 | + /Could not find GraalVM for SemVer */ |
| 138 | + ); |
| 139 | + |
| 140 | + const unavailableEADistro = new GraalVMDistribution({ |
| 141 | + version: '17-ea', |
| 142 | + architecture: '', // to get default value |
| 143 | + packageType: 'jdk', |
| 144 | + checkLatest: false |
| 145 | + }); |
| 146 | + await expect( |
| 147 | + unavailableEADistro['findPackageForDownload']('17') |
| 148 | + ).rejects.toThrow( |
| 149 | + /No GraalVM EA build found\. Are you sure java-version: '17-ea' is correct\?/ |
| 150 | + ); |
| 151 | + }); |
| 152 | +}); |
0 commit comments