Add unit tests and e2e tests

This commit is contained in:
Maxim Lobanov 2021-03-08 18:34:14 +03:00
parent 1d25bcb6a7
commit 330fe63577
17 changed files with 2252 additions and 298 deletions

View file

@ -0,0 +1,146 @@
import { HttpClient } from '@actions/http-client';
import * as semver from 'semver';
import { AdoptiumDistribution } from '../../src/distributions/adoptium/installer';
import { JavaInstallerOptions } from '../../src/distributions/base-models';
let manifestData = require('../data/adoptium.json') as [];
describe('getAvailableVersions', () => {
let spyHttpClient: jest.SpyInstance;
beforeEach(() => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient.mockReturnValue({
statusCode: 200,
headers: {},
result: []
});
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
jest.restoreAllMocks();
});
it.each([
[
{ version: '11', arch: 'x64', packageType: 'jdk' },
'os=mac&architecture=x64&image_type=jdk&release_type=ga&page_size=20&page=0'
],
[
{ version: '11', arch: 'x86', packageType: 'jdk' },
'os=mac&architecture=x86&image_type=jdk&release_type=ga&page_size=20&page=0'
],
[
{ version: '11', arch: 'x64', packageType: 'jre' },
'os=mac&architecture=x64&image_type=jre&release_type=ga&page_size=20&page=0'
],
[
{ version: '11-ea', arch: 'x64', packageType: 'jdk' },
'os=mac&architecture=x64&image_type=jdk&release_type=ea&page_size=20&page=0'
]
])(
'build correct url for %s',
async (installerOptions: JavaInstallerOptions, expectedParameters) => {
const distribution = new AdoptiumDistribution(installerOptions);
const baseUrl = 'https://api.adoptopenjdk.net/v3/assets/version/%5B1.0,100.0%5D';
const expectedUrl = `${baseUrl}?project=jdk&vendor=adoptopenjdk&heap_size=normal&jvm_impl=hotspot&sort_method=DEFAULT&sort_order=DESC&${expectedParameters}`;
distribution['getPlatformOption'] = () => 'mac';
await distribution['getAvailableVersions']();
expect(spyHttpClient.mock.calls).toHaveLength(1);
expect(spyHttpClient.mock.calls[0][0]).toBe(expectedUrl);
}
);
it('load available versions', async () => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: []
});
const distribution = new AdoptiumDistribution({
version: '11',
arch: 'x64',
packageType: 'jdk'
});
const availableVersions = await distribution['getAvailableVersions']();
expect(availableVersions).not.toBeNull();
expect(availableVersions.length).toBe(manifestData.length * 2);
});
});
describe('findPackageForDownload', () => {
it.each([
['9', '9.0.7+10'],
['15', '15.0.2+7'],
['15.0', '15.0.2+7'],
['15.0.2', '15.0.2+7'],
['15.0.1', '15.0.1+9.1'],
['11.x', '11.0.10+9'],
['x', '15.0.2+7'],
['12', '12.0.2+10.3'] // make sure that '12.0.2+10.1', '12.0.2+10.3', '12.0.2+10.2' are sorted correctly
])('version is resolved correctly %s -> %s', async (input, expected) => {
const distribution = new AdoptiumDistribution({
version: '11',
arch: 'x64',
packageType: 'jdk'
});
distribution['getAvailableVersions'] = async () => manifestData;
const resolvedVersion = await distribution['findPackageForDownload'](new semver.Range(input));
expect(resolvedVersion.version).toBe(expected);
});
it('version is found but binaries list is empty', async () => {
const distribution = new AdoptiumDistribution({
version: '11',
arch: 'x64',
packageType: 'jdk'
});
distribution['getAvailableVersions'] = async () => manifestData;
await expect(
distribution['findPackageForDownload'](new semver.Range('9.0.8'))
).rejects.toThrowError(/Could not find satisfied version for SemVer */);
});
it('version is not found', async () => {
const distribution = new AdoptiumDistribution({
version: '11',
arch: 'x64',
packageType: 'jdk'
});
distribution['getAvailableVersions'] = async () => manifestData;
await expect(
distribution['findPackageForDownload'](new semver.Range('7.x'))
).rejects.toThrowError(/Could not find satisfied version for SemVer */);
});
it('version list is empty', async () => {
const distribution = new AdoptiumDistribution({
version: '11',
arch: 'x64',
packageType: 'jdk'
});
distribution['getAvailableVersions'] = async () => [];
await expect(
distribution['findPackageForDownload'](new semver.Range('11'))
).rejects.toThrowError(/Could not find satisfied version for SemVer */);
});
});

View file

@ -0,0 +1,246 @@
import * as tc from '@actions/tool-cache';
import * as core from '@actions/core';
import path from 'path';
import * as semver from 'semver';
import { JavaBase } from '../../src/distributions/base-installer';
import {
JavaDownloadRelease,
JavaInstallerOptions,
JavaInstallerResults
} from '../../src/distributions/base-models';
class EmptyJavaBase extends JavaBase {
constructor(installerOptions: JavaInstallerOptions) {
super('Empty', installerOptions);
}
protected async downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults> {
return {
version: '11.0.8',
path: `/toolcache/${this.toolcacheFolderName}/11.0.8/${this.architecture}`
};
}
protected async findPackageForDownload(range: semver.Range): Promise<JavaDownloadRelease> {
const availableVersion = '11.0.8';
if (!semver.satisfies(availableVersion, range)) {
throw new Error('Available version not found');
}
return {
version: availableVersion,
url: `some/random_url/java/${availableVersion}`
};
}
}
describe('findInToolcache', () => {
const actualJavaVersion = '11.1.10';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x64');
let mockJavaBase: EmptyJavaBase;
let spyTcFind: jest.SpyInstance;
let spyTcFindAllVersions: jest.SpyInstance;
beforeEach(() => {
spyTcFind = jest.spyOn(tc, 'find');
spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
jest.restoreAllMocks();
});
it.each([
[
{ version: '11', arch: 'x64', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1', arch: 'x64', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1.10', arch: 'x64', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[{ version: '11', arch: 'x64', packageType: 'jre' }, null],
[{ version: '8', arch: 'x64', packageType: 'jdk' }, null],
[{ version: '11', arch: 'x86', packageType: 'jdk' }, null],
[{ version: '11', arch: 'x86', packageType: 'jre' }, null]
])(`should find java for path %s -> %s`, (input, expected) => {
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
spyTcFind.mockImplementation((toolname: string, javaVersion: string, architecture: string) => {
const semverVersion = new semver.Range(javaVersion);
if (path.basename(javaPath) !== architecture || !javaPath.includes(toolname)) {
return '';
}
return semver.satisfies(actualJavaVersion, semverVersion) ? javaPath : '';
});
mockJavaBase = new EmptyJavaBase(input);
expect(mockJavaBase['findInToolcache']()).toEqual(expected);
});
it.each([
['11', '11.0.3'],
['11.0', '11.0.3'],
['11.0.1', '11.0.1'],
['11.0.3', '11.0.3'],
['15', '15.0.2'],
['x', '15.0.2'],
['x-ea', '17.4.4-ea'],
['11-ea', '11.3.2-ea'],
['11.2-ea', '11.2.1-ea'],
['11.2.1-ea', '11.2.1-ea']
])('should choose correct java from tool-cache for input %s', (input, expected) => {
spyTcFindAllVersions.mockReturnValue([
'17.4.4-ea',
'11.0.2',
'15.0.2',
'11.0.3',
'11.2.1-ea',
'11.3.2-ea',
'11.0.1'
]);
spyTcFind.mockImplementation(
(toolname: string, javaVersion: string, architecture: string) =>
`/hostedtoolcache/${toolname}/${javaVersion}/${architecture}`
);
mockJavaBase = new EmptyJavaBase({ version: input, arch: 'x64', packageType: 'jdk' });
const foundVersion = mockJavaBase['findInToolcache']();
expect(foundVersion?.version).toEqual(expected);
});
});
describe('setupJava', () => {
const actualJavaVersion = '11.1.10';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x86');
let mockJavaBase: EmptyJavaBase;
let spyTcFind: jest.SpyInstance;
let spyTcFindAllVersions: jest.SpyInstance;
let spyCoreDebug: jest.SpyInstance;
let spyCoreInfo: jest.SpyInstance;
let spyCoreExportVariable: jest.SpyInstance;
let spyCoreAddPath: jest.SpyInstance;
let spyCoreSetOutput: jest.SpyInstance;
beforeEach(() => {
spyTcFind = jest.spyOn(tc, 'find');
spyTcFind.mockImplementation((toolname: string, javaVersion: string, architecture: string) => {
const semverVersion = new semver.Range(javaVersion);
if (path.basename(javaPath) !== architecture || !javaPath.includes(toolname)) {
return '';
}
return semver.satisfies(actualJavaVersion, semverVersion) ? javaPath : '';
});
spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
// Spy on core methods
spyCoreDebug = jest.spyOn(core, 'debug');
spyCoreDebug.mockImplementation(() => undefined);
spyCoreInfo = jest.spyOn(core, 'info');
spyCoreInfo.mockImplementation(() => undefined);
spyCoreAddPath = jest.spyOn(core, 'addPath');
spyCoreAddPath.mockImplementation(() => undefined);
spyCoreExportVariable = jest.spyOn(core, 'exportVariable');
spyCoreExportVariable.mockImplementation(() => undefined);
spyCoreSetOutput = jest.spyOn(core, 'setOutput');
spyCoreSetOutput.mockImplementation(() => undefined);
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
jest.restoreAllMocks();
});
it.each([
[
{ version: '11', arch: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1', arch: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1.10', arch: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
]
])('should find java locally for %s', (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyTcFind).toHaveBeenCalled();
});
it.each([
[
{ version: '11', arch: 'x86', packageType: 'jre' },
{ path: `/toolcache/Java_Empty_jre/11.0.8/x86`, version: '11.0.8' }
],
[
{ version: '11', arch: 'x64', packageType: 'jdk' },
{ path: `/toolcache/Java_Empty_jdk/11.0.8/x64`, version: '11.0.8' }
],
[
{ version: '11', arch: 'x64', packageType: 'jre' },
{ path: `/toolcache/Java_Empty_jre/11.0.8/x64`, version: '11.0.8' }
]
])('download java with configuration %s', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyTcFind).toHaveBeenCalled();
expect(spyCoreAddPath).toHaveBeenCalled();
expect(spyCoreExportVariable).toHaveBeenCalled();
expect(spyCoreSetOutput).toHaveBeenCalled();
});
it.each([
[{ version: '15', arch: 'x86', packageType: 'jre' }],
[{ version: '11.0.7', arch: 'x64', packageType: 'jre' }]
])('should throw an error for Available version not found for %s', async input => {
mockJavaBase = new EmptyJavaBase(input);
await expect(mockJavaBase.setupJava()).rejects.toThrowError('Available version not found');
expect(spyTcFindAllVersions).toHaveBeenCalled();
expect(spyCoreAddPath).not.toHaveBeenCalled();
expect(spyCoreExportVariable).not.toHaveBeenCalled();
expect(spyCoreSetOutput).not.toHaveBeenCalled();
});
});
describe('normalizeVersion', () => {
const DummyJavaBase = JavaBase as any;
it.each([
['11', { version: new semver.Range('11'), stable: true }],
['11.0', { version: new semver.Range('11.0'), stable: true }],
['11.0.10', { version: new semver.Range('11.0.10'), stable: true }],
['11-ea', { version: new semver.Range('11'), stable: false }],
['11.0.2-ea', { version: new semver.Range('11.0.2'), stable: false }]
])('normalizeVersion from %s to %s', (input, expected) => {
expect(DummyJavaBase.prototype.normalizeVersion.call(null, input)).toEqual(expected);
});
it('normalizeVersion should throw an error for non semver', () => {
const version = '11g';
expect(DummyJavaBase.prototype.normalizeVersion.bind(null, version)).toThrowError(
`The string '${version}' is not valid SemVer notation for Java version. Please check README file for code snippets and more detailed information`
);
});
});

View file

@ -0,0 +1,195 @@
import fs from 'fs';
import * as tc from '@actions/tool-cache';
import * as core from '@actions/core';
import path from 'path';
import * as semver from 'semver';
import * as utils from '../../src/util';
import { LocalDistribution } from '../../src/distributions/local/installer';
describe('setupJava', () => {
const actualJavaVersion = '11.1.10';
const javaPath = path.join('Java_jdkfile_jdk', actualJavaVersion, 'x86');
let mockJavaBase: LocalDistribution;
let spyTcFind: jest.SpyInstance;
let spyTcCacheDir: jest.SpyInstance;
let spyTcFindAllVersions: jest.SpyInstance;
let spyCoreDebug: jest.SpyInstance;
let spyCoreInfo: jest.SpyInstance;
let spyCoreExportVariable: jest.SpyInstance;
let spyCoreAddPath: jest.SpyInstance;
let spyCoreSetOutput: jest.SpyInstance;
let spyFsStat: jest.SpyInstance;
let spyFsReadDir: jest.SpyInstance;
let spyUtilsExtractJdkFile: jest.SpyInstance;
let spyPathResolve: jest.SpyInstance;
let expectedJdkFile = 'JavaLocalJdkFile';
beforeEach(() => {
spyTcFind = jest.spyOn(tc, 'find');
spyTcFind.mockImplementation((toolname: string, javaVersion: string, architecture: string) => {
const semverVersion = new semver.Range(javaVersion);
if (path.basename(javaPath) !== architecture || !javaPath.includes(toolname)) {
return '';
}
return semver.satisfies(actualJavaVersion, semverVersion) ? javaPath : '';
});
spyTcCacheDir = jest.spyOn(tc, 'cacheDir');
spyTcCacheDir.mockImplementation(
(archivePath: string, toolcacheFolderName: string, version: string, architecture: string) =>
path.join(toolcacheFolderName, version, architecture)
);
spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
// Spy on core methods
spyCoreDebug = jest.spyOn(core, 'debug');
spyCoreDebug.mockImplementation(() => undefined);
spyCoreInfo = jest.spyOn(core, 'info');
spyCoreInfo.mockImplementation(() => undefined);
spyCoreAddPath = jest.spyOn(core, 'addPath');
spyCoreAddPath.mockImplementation(() => undefined);
spyCoreExportVariable = jest.spyOn(core, 'exportVariable');
spyCoreExportVariable.mockImplementation(() => undefined);
spyCoreSetOutput = jest.spyOn(core, 'setOutput');
spyCoreSetOutput.mockImplementation(() => undefined);
// Spy on fs methods
spyFsReadDir = jest.spyOn(fs, 'readdirSync');
spyFsReadDir.mockImplementation(() => ['JavaTest']);
spyFsStat = jest.spyOn(fs, 'statSync');
spyFsStat.mockImplementation((file: string) => {
return { isFile: () => file === expectedJdkFile };
});
// Spy on util methods
spyUtilsExtractJdkFile = jest.spyOn(utils, 'extractJdkFile');
spyUtilsExtractJdkFile.mockImplementation(() => 'some/random/path/');
// Spy on path methods
spyPathResolve = jest.spyOn(path, 'resolve');
spyPathResolve.mockImplementation((path: string) => path);
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
jest.restoreAllMocks();
});
it('java is resolved from toolcache, jdkfile is untouched', async () => {
const inputs = { version: actualJavaVersion, arch: 'x86', packageType: 'jdk' };
const jdkFile = 'not_existing_one';
const expected = {
version: actualJavaVersion,
path: path.join('Java_jdkfile_jdk', inputs.version, inputs.arch)
};
mockJavaBase = new LocalDistribution(inputs, jdkFile);
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyTcFind).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${actualJavaVersion} from tool-cache`);
expect(spyCoreInfo).not.toHaveBeenCalledWith(
`Java ${inputs.version} is not found in tool-cache. Trying to unpack JDK file...`
);
});
it("java is resolved from toolcache, jdkfile doesn't exist", async () => {
const inputs = { version: actualJavaVersion, arch: 'x86', packageType: 'jdk' };
const jdkFile = undefined;
const expected = {
version: actualJavaVersion,
path: path.join('Java_jdkfile_jdk', inputs.version, inputs.arch)
};
mockJavaBase = new LocalDistribution(inputs, jdkFile);
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyTcFind).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${actualJavaVersion} from tool-cache`);
expect(spyCoreInfo).not.toHaveBeenCalledWith(
`Java ${inputs.version} is not found in tool-cache. Trying to unpack JDK file...`
);
});
it('java is unpacked from jdkfile', async () => {
const inputs = { version: '11.0.289', arch: 'x86', packageType: 'jdk' };
const jdkFile = expectedJdkFile;
const expected = {
version: '11.0.289',
path: path.join('Java_jdkfile_jdk', inputs.version, inputs.arch)
};
mockJavaBase = new LocalDistribution(inputs, jdkFile);
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyTcFindAllVersions).toHaveBeenCalled();
expect(spyCoreInfo).not.toHaveBeenCalledWith(
`Resolved Java ${actualJavaVersion} from tool-cache`
);
expect(spyCoreInfo).toHaveBeenCalledWith(`Extracting Java from '${jdkFile}'`);
expect(spyCoreInfo).toHaveBeenCalledWith(
`Java ${inputs.version} is not found in tool-cache. Trying to unpack JDK file...`
);
});
it('jdk file is not found', async () => {
const inputs = { version: '11.0.289', arch: 'x86', packageType: 'jdk' };
const jdkFile = 'not_existing_one';
const expected = {
javaVersion: '11.0.289',
javaPath: path.join('Java_jdkfile_jdk', inputs.version, inputs.arch)
};
mockJavaBase = new LocalDistribution(inputs, jdkFile);
expected.javaPath = path.join('Java_jdkfile_jdk', inputs.version, inputs.arch);
await expect(mockJavaBase.setupJava()).rejects.toThrowError(
"JDK file is not found in path 'not_existing_one'"
);
expect(spyTcFindAllVersions).toHaveBeenCalled();
expect(spyCoreInfo).not.toHaveBeenCalledWith(
`Resolved Java ${actualJavaVersion} from tool-cache`
);
expect(spyCoreInfo).not.toHaveBeenCalledWith(`Extracting Java from '${jdkFile}'`);
expect(spyCoreInfo).toHaveBeenCalledWith(
`Java ${inputs.version} is not found in tool-cache. Trying to unpack JDK file...`
);
});
it.each([
[{ version: '8.0.289', arch: 'x64', packageType: 'jdk' }, 'otherJdkFile'],
[{ version: '11.0.289', arch: 'x64', packageType: 'jdk' }, 'otherJdkFile'],
[{ version: '12.0.289', arch: 'x64', packageType: 'jdk' }, 'otherJdkFile'],
[{ version: '11.1.11', arch: 'x64', packageType: 'jdk' }, 'not_existing_one']
])(
`Throw an error if jdkfile has wrong path, inputs %s, jdkfile %s, real name ${expectedJdkFile}`,
async (inputs, jdkFile) => {
mockJavaBase = new LocalDistribution(inputs, jdkFile);
await expect(mockJavaBase.setupJava()).rejects.toThrowError(
/JDK file is not found in path */
);
expect(spyTcFindAllVersions).toHaveBeenCalled();
}
);
it.each([
[{ version: '8.0.289', arch: 'x64', packageType: 'jdk' }, ''],
[{ version: '7.0.289', arch: 'x64', packageType: 'jdk' }, undefined],
[{ version: '11.0.289', arch: 'x64', packageType: 'jdk' }, undefined]
])('Throw an error if jdkfile is not specified, inputs %s', async (inputs, jdkFile) => {
mockJavaBase = new LocalDistribution(inputs, jdkFile);
await expect(mockJavaBase.setupJava()).rejects.toThrowError("'jdkFile' is not specified");
expect(spyTcFindAllVersions).toHaveBeenCalled();
});
});

View file

@ -0,0 +1,140 @@
import { HttpClient } from '@actions/http-client';
import * as semver from 'semver';
import { ZuluDistribution } from '../../src/distributions/zulu/installer';
import { IZuluVersions } from '../../src/distributions/zulu/models';
import * as utils from '../../src/util';
const manifestData = require('../data/zulu-releases-default.json') as [];
describe('getAvailableVersions', () => {
let spyHttpClient: jest.SpyInstance;
let spyUtilGetDownloadArchiveExtension: jest.SpyInstance;
beforeEach(() => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient.mockReturnValue({
statusCode: 200,
headers: {},
result: manifestData as IZuluVersions[]
});
spyUtilGetDownloadArchiveExtension = jest.spyOn(utils, 'getDownloadArchiveExtension');
spyUtilGetDownloadArchiveExtension.mockReturnValue('tar.gz');
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
jest.restoreAllMocks();
});
it.each([
[
{ version: '11', arch: 'x86', packageType: 'jdk' },
'?os=macos&ext=tar.gz&bundle_type=jdk&javafx=false&arch=x86&hw_bitness=32&release_status=ga'
],
[
{ version: '11-ea', arch: 'x86', packageType: 'jdk' },
'?os=macos&ext=tar.gz&bundle_type=jdk&javafx=false&arch=x86&hw_bitness=32&release_status=ea'
],
[
{ version: '8', arch: 'x64', packageType: 'jdk' },
'?os=macos&ext=tar.gz&bundle_type=jdk&javafx=false&arch=x86&hw_bitness=64&release_status=ga'
],
[
{ version: '8', arch: 'x64', packageType: 'jre' },
'?os=macos&ext=tar.gz&bundle_type=jre&javafx=false&arch=x86&hw_bitness=64&release_status=ga'
],
[
{ version: '8', arch: 'x64', packageType: 'jdk+fx' },
'?os=macos&ext=tar.gz&bundle_type=jdk&javafx=true&arch=x86&hw_bitness=64&release_status=ga&features=fx'
],
[
{ version: '8', arch: 'x64', packageType: 'jre+fx' },
'?os=macos&ext=tar.gz&bundle_type=jre&javafx=true&arch=x86&hw_bitness=64&release_status=ga&features=fx'
]
])('build correct url for %s -> %s', async (input, parsedUrl) => {
const distribution = new ZuluDistribution(input);
distribution['getPlatformOption'] = () => 'macos';
const buildUrl = `https://api.azul.com/zulu/download/community/v1.0/bundles/${parsedUrl}`;
await distribution['getAvailableVersions']();
expect(spyHttpClient.mock.calls).toHaveLength(1);
expect(spyHttpClient.mock.calls[0][0]).toBe(buildUrl);
});
it('load available versions', async () => {
const distribution = new ZuluDistribution({ version: '11', arch: 'x86', packageType: 'jdk' });
const availableVersions = await distribution['getAvailableVersions']();
expect(availableVersions).toHaveLength(manifestData.length);
});
});
describe('getArchitectureOptions', () => {
it.each([
[{ architecture: 'x64' }, { arch: 'x86', hw_bitness: '64', abi: '' }],
[{ architecture: 'x86' }, { arch: 'x86', hw_bitness: '32', abi: '' }],
[{ architecture: 'x32' }, { arch: 'x32', hw_bitness: '', abi: '' }],
[{ architecture: 'arm' }, { arch: 'arm', hw_bitness: '', abi: '' }]
])('%s -> %s', (input, expected) => {
const distribution = new ZuluDistribution({
version: '11',
arch: input.architecture,
packageType: 'jdk'
});
expect(distribution['getArchitectureOptions']()).toEqual(expected);
});
});
describe('findPackageForDownload', () => {
it.each([
['8', '8.0.282+8'],
['11.x', '11.0.10+9'],
['8.0', '8.0.282+8'],
['11.0.x', '11.0.10+9'],
['15', '15.0.2+7'],
['9.0.0', '9.0.0+0'],
['9.0', '9.0.1+0'],
['8.0.262', '8.0.262+19'] // validate correct choise between [8.0.262.17, 8.0.262.19, 8.0.262.18]
])('version is %s -> %s', async (input, expected) => {
const distribution = new ZuluDistribution({
version: input,
arch: 'x86',
packageType: 'jdk'
});
distribution['getAvailableVersions'] = async () => manifestData;
const result = await distribution['findPackageForDownload'](distribution['version']);
expect(result.version).toBe(expected);
});
it('select correct bundle if there are multiple items with the same jdk version but different zulu versions', async () => {
const distribution = new ZuluDistribution({ version: '', arch: 'x86', packageType: 'jdk' });
distribution['getAvailableVersions'] = async () => manifestData;
const result = await distribution['findPackageForDownload'](new semver.Range('11.0.5'));
expect(result.url).toBe(
'https://cdn.azul.com/zulu/bin/zulu11.35.15-ca-jdk11.0.5-macosx_x64.tar.gz'
);
});
it('should throw an error', async () => {
const distribution = new ZuluDistribution({ version: '18', arch: 'x86', packageType: 'jdk' });
await expect(
distribution['findPackageForDownload'](distribution['version'])
).rejects.toThrowError(/Could not find satisfied version for semver */);
});
});
describe('convertVersionToSemver', () => {
it.each([
[[12], '12'],
[[12, 0], '12.0'],
[[12, 0, 2], '12.0.2'],
[[12, 0, 2, 1], '12.0.2+1'],
[[12, 0, 2, 1, 3], '12.0.2+1']
])('%s -> %s', (input: number[], expected: string) => {
const distribution = new ZuluDistribution({ version: '18', arch: 'x86', packageType: 'jdk' });
const actual = distribution['convertVersionToSemver'](input);
expect(actual).toBe(expected);
});
});