|
| 1 | +import { HttpClient } from '@actions/http-client'; |
| 2 | +import { JavaInstallerOptions } from '../../src/distributions/base-models'; |
| 3 | + |
| 4 | +import { CorrettoDistribution } from '../../src/distributions/corretto/installer'; |
| 5 | +import * as util from '../../src/util'; |
| 6 | + |
| 7 | +const manifestData = require('../data/corretto.json') as []; |
| 8 | + |
| 9 | +describe('getAvailableVersions', () => { |
| 10 | + let spyHttpClient: jest.SpyInstance; |
| 11 | + let spyGetDownloadArchiveExtension: jest.SpyInstance; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); |
| 15 | + spyHttpClient.mockReturnValue({ |
| 16 | + statusCode: 200, |
| 17 | + headers: {}, |
| 18 | + result: manifestData |
| 19 | + }); |
| 20 | + spyGetDownloadArchiveExtension = jest.spyOn(util, 'getDownloadArchiveExtension'); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + jest.resetAllMocks(); |
| 25 | + jest.clearAllMocks(); |
| 26 | + jest.restoreAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('getAvailableVersions', () => { |
| 30 | + it('load available versions', async () => { |
| 31 | + const distribution = new CorrettoDistribution({ |
| 32 | + version: '11', |
| 33 | + architecture: 'x64', |
| 34 | + packageType: 'jdk', |
| 35 | + checkLatest: false |
| 36 | + }); |
| 37 | + mockPlatform(distribution, 'linux'); |
| 38 | + |
| 39 | + const availableVersions = await distribution['getAvailableVersions'](); |
| 40 | + expect(availableVersions).not.toBeNull(); |
| 41 | + expect(availableVersions.length).toBe(6); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each([ |
| 45 | + [{ version: '16', architecture: 'x64', packageType: 'jdk', checkLatest: false }, 'macos', 6], |
| 46 | + [{ version: '16', architecture: 'x86', packageType: 'jdk', checkLatest: false }, 'macos', 0], |
| 47 | + [{ version: '16', architecture: 'x64', packageType: 'jre', checkLatest: false }, 'macos', 0], |
| 48 | + [{ version: '16', architecture: 'x64', packageType: 'jdk', checkLatest: false }, 'linux', 6], |
| 49 | + [ |
| 50 | + { version: '18', architecture: 'x64', packageType: 'jdk', checkLatest: false }, |
| 51 | + 'windows', |
| 52 | + 6 |
| 53 | + ], |
| 54 | + [{ version: '18', architecture: 'x64', packageType: 'jre', checkLatest: false }, 'windows', 1] |
| 55 | + ])( |
| 56 | + 'fetch expected amount of available versions for %s', |
| 57 | + async ( |
| 58 | + installerOptions: JavaInstallerOptions, |
| 59 | + platform: string, |
| 60 | + expectedAmountOfAvailableVersions |
| 61 | + ) => { |
| 62 | + const distribution = new CorrettoDistribution(installerOptions); |
| 63 | + mockPlatform(distribution, platform); |
| 64 | + |
| 65 | + const availableVersions = await distribution['getAvailableVersions'](); |
| 66 | + expect(availableVersions).not.toBeNull(); |
| 67 | + expect(availableVersions.length).toBe(expectedAmountOfAvailableVersions); |
| 68 | + } |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('findPackageForDownload', () => { |
| 73 | + it.each([ |
| 74 | + [ |
| 75 | + 'macos', |
| 76 | + 'https://corretto.aws/downloads/resources/18.0.0.37.1/amazon-corretto-18.0.0.37.1-macosx-x64.tar.gz' |
| 77 | + ], |
| 78 | + [ |
| 79 | + 'windows', |
| 80 | + 'https://corretto.aws/downloads/resources/18.0.0.37.1/amazon-corretto-18.0.0.37.1-windows-x64-jdk.zip' |
| 81 | + ], |
| 82 | + [ |
| 83 | + 'linux', |
| 84 | + 'https://corretto.aws/downloads/resources/18.0.0.37.1/amazon-corretto-18.0.0.37.1-linux-x64.tar.gz' |
| 85 | + ] |
| 86 | + ])('for os: %s', async (platform: string, expectedLink: string) => { |
| 87 | + const version = '18'; |
| 88 | + const distribution = new CorrettoDistribution({ |
| 89 | + version, |
| 90 | + architecture: 'x64', |
| 91 | + packageType: 'jdk', |
| 92 | + checkLatest: false |
| 93 | + }); |
| 94 | + mockPlatform(distribution, platform); |
| 95 | + |
| 96 | + const availableVersion = await distribution['findPackageForDownload'](version); |
| 97 | + expect(availableVersion).not.toBeNull(); |
| 98 | + expect(availableVersion.url).toBe(expectedLink); |
| 99 | + }); |
| 100 | + |
| 101 | + it('with unstable version expect to throw not supported error', async () => { |
| 102 | + const version = '18.0.1-ea'; |
| 103 | + const distribution = new CorrettoDistribution({ |
| 104 | + version, |
| 105 | + architecture: 'x64', |
| 106 | + packageType: 'jdk', |
| 107 | + checkLatest: false |
| 108 | + }); |
| 109 | + mockPlatform(distribution, 'linux'); |
| 110 | + |
| 111 | + await expect(distribution['findPackageForDownload'](version)).rejects.toThrowError( |
| 112 | + 'Early access versions are not supported' |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it('with non major version expect to throw not supported error', async () => { |
| 117 | + const version = '18.0.1'; |
| 118 | + const distribution = new CorrettoDistribution({ |
| 119 | + version, |
| 120 | + architecture: 'x64', |
| 121 | + packageType: 'jdk', |
| 122 | + checkLatest: false |
| 123 | + }); |
| 124 | + mockPlatform(distribution, 'linux'); |
| 125 | + |
| 126 | + await expect(distribution['findPackageForDownload'](version)).rejects.toThrowError( |
| 127 | + 'Only major versions are supported' |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('with unfound version throw could not find error', async () => { |
| 132 | + const version = '4'; |
| 133 | + const distribution = new CorrettoDistribution({ |
| 134 | + version, |
| 135 | + architecture: 'x64', |
| 136 | + packageType: 'jdk', |
| 137 | + checkLatest: false |
| 138 | + }); |
| 139 | + mockPlatform(distribution, 'linux'); |
| 140 | + |
| 141 | + await expect(distribution['findPackageForDownload'](version)).rejects.toThrowError( |
| 142 | + "Could not find satisfied version for SemVer '4'" |
| 143 | + ); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + const mockPlatform = (distributon: CorrettoDistribution, platform: string) => { |
| 148 | + distributon['getPlatformOption'] = () => platform; |
| 149 | + const mockedExtension = platform === 'windows' ? 'zip' : 'tar.gz'; |
| 150 | + spyGetDownloadArchiveExtension.mockReturnValue(mockedExtension); |
| 151 | + }; |
| 152 | +}); |
0 commit comments