mirror of
https://github.com/actions/setup-java.git
synced 2024-11-24 02:29:03 +00:00
7c88894700
* Implement support for custom vendors in setup-java * minor improvements * minor refactoring * Add unit tests and e2e tests * Update documentation for setup-java@v2 release * minor improvements * regenerate dist * fix comments * resolve comments * resolve comments * fix tests * Update README.md Co-authored-by: George Adams <george.adams@microsoft.com> * Apply suggestions from code review Co-authored-by: Konrad Pabjan <konradpabjan@github.com> * fix minor nitpicks * handle 4th digit * pull latest main * Update README.md * rename adoptium to adopt * rename adoptium to adopt * rename adoptium to adopt * Update README.md * make java-version and distribution required for action * update readme * fix tests * fix e2e tests Co-authored-by: George Adams <george.adams@microsoft.com> Co-authored-by: Konrad Pabjan <konradpabjan@github.com>
198 lines
7.7 KiB
TypeScript
198 lines
7.7 KiB
TypeScript
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 util 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 spyGetToolcachePath: 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(() => {
|
|
spyGetToolcachePath = jest.spyOn(util, 'getToolcachePath');
|
|
spyGetToolcachePath.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(util, '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, architecture: 'x86', packageType: 'jdk' };
|
|
const jdkFile = 'not_existing_one';
|
|
const expected = {
|
|
version: actualJavaVersion,
|
|
path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture)
|
|
};
|
|
|
|
mockJavaBase = new LocalDistribution(inputs, jdkFile);
|
|
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
|
|
expect(spyGetToolcachePath).toHaveBeenCalled();
|
|
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${actualJavaVersion} from tool-cache`);
|
|
expect(spyCoreInfo).not.toHaveBeenCalledWith(
|
|
`Java ${inputs.version} was 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, architecture: 'x86', packageType: 'jdk' };
|
|
const jdkFile = undefined;
|
|
const expected = {
|
|
version: actualJavaVersion,
|
|
path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture)
|
|
};
|
|
|
|
mockJavaBase = new LocalDistribution(inputs, jdkFile);
|
|
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
|
|
expect(spyGetToolcachePath).toHaveBeenCalled();
|
|
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${actualJavaVersion} from tool-cache`);
|
|
expect(spyCoreInfo).not.toHaveBeenCalledWith(
|
|
`Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...`
|
|
);
|
|
});
|
|
|
|
it('java is unpacked from jdkfile', async () => {
|
|
const inputs = { version: '11.0.289', architecture: 'x86', packageType: 'jdk' };
|
|
const jdkFile = expectedJdkFile;
|
|
const expected = {
|
|
version: '11.0.289',
|
|
path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture)
|
|
};
|
|
|
|
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} was not found in tool-cache. Trying to unpack JDK file...`
|
|
);
|
|
});
|
|
|
|
it('jdk file is not found', async () => {
|
|
const inputs = { version: '11.0.289', architecture: 'x86', packageType: 'jdk' };
|
|
const jdkFile = 'not_existing_one';
|
|
const expected = {
|
|
javaVersion: '11.0.289',
|
|
javaPath: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture)
|
|
};
|
|
|
|
mockJavaBase = new LocalDistribution(inputs, jdkFile);
|
|
expected.javaPath = path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture);
|
|
await expect(mockJavaBase.setupJava()).rejects.toThrowError(
|
|
"JDK file was 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} was not found in tool-cache. Trying to unpack JDK file...`
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
[{ version: '8.0.289', architecture: 'x64', packageType: 'jdk' }, 'otherJdkFile'],
|
|
[{ version: '11.0.289', architecture: 'x64', packageType: 'jdk' }, 'otherJdkFile'],
|
|
[{ version: '12.0.289', architecture: 'x64', packageType: 'jdk' }, 'otherJdkFile'],
|
|
[{ version: '11.1.11', architecture: '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 was not found in path */
|
|
);
|
|
expect(spyTcFindAllVersions).toHaveBeenCalled();
|
|
}
|
|
);
|
|
|
|
it.each([
|
|
[{ version: '8.0.289', architecture: 'x64', packageType: 'jdk' }, ''],
|
|
[{ version: '7.0.289', architecture: 'x64', packageType: 'jdk' }, undefined],
|
|
[{ version: '11.0.289', architecture: '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();
|
|
});
|
|
});
|