mirror of
https://github.com/actions/setup-java.git
synced 2026-07-08 16:20:44 +03:00
Migrate to ESM and upgrade dependencies (#1078)
* Migrate to ESM and upgrade dependencies * fix: update import statement for JSON module in kona-installer test --------- Co-authored-by: George Adams <georgeadams1995@gmail.com>
This commit is contained in:
@@ -1,23 +1,115 @@
|
||||
import {
|
||||
MicrosoftDistributions,
|
||||
MICROSOFT_PUBLIC_KEY
|
||||
} from '../../src/distributions/microsoft/installer';
|
||||
import os from 'os';
|
||||
import data from '../data/microsoft.json';
|
||||
import * as httpm from '@actions/http-client';
|
||||
import * as core from '@actions/core';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as gpg from '../../src/gpg';
|
||||
import * as util from '../../src/util';
|
||||
import fs from 'fs';
|
||||
jest,
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
beforeAll,
|
||||
afterAll
|
||||
} from '@jest/globals';
|
||||
import {HttpClient} from '@actions/http-client';
|
||||
import data from '../data/microsoft.json' with {type: 'json'};
|
||||
|
||||
const mockOsArch = jest.fn(() => 'x64');
|
||||
const mockOsPlatform = jest.fn(() => 'linux');
|
||||
|
||||
const real_os_module = await import('os');
|
||||
jest.unstable_mockModule('os', () => ({
|
||||
...real_os_module,
|
||||
default: {
|
||||
...real_os_module.default,
|
||||
arch: mockOsArch,
|
||||
platform: mockOsPlatform,
|
||||
homedir: real_os_module.default.homedir
|
||||
},
|
||||
arch: mockOsArch,
|
||||
platform: mockOsPlatform
|
||||
}));
|
||||
|
||||
const real_fs_module = await import('fs');
|
||||
const mockReaddirSync = jest.fn();
|
||||
jest.unstable_mockModule('fs', () => ({
|
||||
...real_fs_module,
|
||||
default: {
|
||||
...real_fs_module.default,
|
||||
readdirSync: mockReaddirSync
|
||||
},
|
||||
readdirSync: mockReaddirSync
|
||||
}));
|
||||
|
||||
// Mock @actions/core before importing source modules that depend on it
|
||||
jest.unstable_mockModule('@actions/core', () => ({
|
||||
info: jest.fn(),
|
||||
warning: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
error: jest.fn(),
|
||||
notice: jest.fn(),
|
||||
setFailed: jest.fn(),
|
||||
setOutput: jest.fn(),
|
||||
getInput: jest.fn(),
|
||||
getBooleanInput: jest.fn(),
|
||||
getMultilineInput: jest.fn(),
|
||||
addPath: jest.fn(),
|
||||
exportVariable: jest.fn(),
|
||||
saveState: jest.fn(),
|
||||
getState: jest.fn(),
|
||||
setSecret: jest.fn(),
|
||||
isDebug: jest.fn(() => false),
|
||||
startGroup: jest.fn(),
|
||||
endGroup: jest.fn(),
|
||||
group: jest.fn((_name: string, fn: () => Promise<unknown>) => fn()),
|
||||
toPlatformPath: jest.fn((p: string) => p),
|
||||
toWin32Path: jest.fn((p: string) => p),
|
||||
toPosixPath: jest.fn((p: string) => p)
|
||||
}));
|
||||
|
||||
const real_tc_module = await import('@actions/tool-cache');
|
||||
jest.unstable_mockModule('@actions/tool-cache', () => ({
|
||||
...real_tc_module,
|
||||
downloadTool: jest.fn(),
|
||||
cacheDir: jest.fn(),
|
||||
cacheFile: jest.fn()
|
||||
}));
|
||||
|
||||
const real_util_module = await import('../../src/util.js');
|
||||
jest.unstable_mockModule('../../src/util.js', () => ({
|
||||
...real_util_module,
|
||||
extractJdkFile: jest.fn(),
|
||||
getDownloadArchiveExtension: jest.fn(),
|
||||
getToolcachePath: jest.fn(),
|
||||
isJobStatusSuccess: jest.fn(),
|
||||
renameWinArchive: jest.fn(),
|
||||
isVersionSatisfies: real_util_module.isVersionSatisfies,
|
||||
getTempDir: real_util_module.getTempDir
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('../../src/gpg.js', () => ({
|
||||
importKey: jest.fn(),
|
||||
deleteKey: jest.fn(),
|
||||
verifyPackageSignature: jest.fn()
|
||||
}));
|
||||
|
||||
// Dynamic imports after mocking
|
||||
const core = await import('@actions/core');
|
||||
const gpg = await import('../../src/gpg.js');
|
||||
const tc = await import('@actions/tool-cache');
|
||||
const os = (await import('os')).default;
|
||||
const fs = (await import('fs')).default;
|
||||
const {MicrosoftDistributions, MICROSOFT_PUBLIC_KEY} =
|
||||
await import('../../src/distributions/microsoft/installer.js');
|
||||
const util = await import('../../src/util.js');
|
||||
|
||||
describe('findPackageForDownload', () => {
|
||||
let distribution: MicrosoftDistributions;
|
||||
let spyGetManifestFromRepo: jest.SpyInstance;
|
||||
let spyDebug: jest.SpyInstance;
|
||||
let spyCoreError: jest.SpyInstance;
|
||||
let distribution: InstanceType<typeof MicrosoftDistributions>;
|
||||
let spyGetManifestFromRepo: any;
|
||||
let spyDebug: any;
|
||||
let spyCoreError: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockOsArch.mockReturnValue('x64');
|
||||
mockOsPlatform.mockReturnValue(process.platform);
|
||||
|
||||
distribution = new MicrosoftDistributions({
|
||||
version: '',
|
||||
architecture: 'x64',
|
||||
@@ -25,18 +117,18 @@ describe('findPackageForDownload', () => {
|
||||
checkLatest: false
|
||||
});
|
||||
|
||||
spyGetManifestFromRepo = jest.spyOn(httpm.HttpClient.prototype, 'getJson');
|
||||
spyGetManifestFromRepo = jest.spyOn(HttpClient.prototype, 'getJson');
|
||||
spyGetManifestFromRepo.mockReturnValue({
|
||||
result: data,
|
||||
statusCode: 200,
|
||||
headers: {}
|
||||
});
|
||||
|
||||
spyDebug = jest.spyOn(core, 'debug');
|
||||
spyDebug = core.debug as jest.Mock;
|
||||
spyDebug.mockImplementation(() => {});
|
||||
|
||||
// Mock core.error to suppress error logs
|
||||
spyCoreError = jest.spyOn(core, 'error');
|
||||
spyCoreError = core.error as jest.Mock;
|
||||
spyCoreError.mockImplementation(() => {});
|
||||
});
|
||||
|
||||
@@ -118,10 +210,8 @@ describe('findPackageForDownload', () => {
|
||||
])(
|
||||
'defaults to os.arch(): %s mapped to distro arch: %s',
|
||||
async (osArch: string, distroArch: string) => {
|
||||
jest
|
||||
.spyOn(os, 'arch')
|
||||
.mockReturnValue(osArch as ReturnType<typeof os.arch>);
|
||||
jest.spyOn(os, 'platform').mockReturnValue('darwin');
|
||||
mockOsArch.mockReturnValue(osArch);
|
||||
mockOsPlatform.mockReturnValue('darwin');
|
||||
|
||||
const version = '17';
|
||||
const distro = new MicrosoftDistributions({
|
||||
@@ -144,10 +234,8 @@ describe('findPackageForDownload', () => {
|
||||
])(
|
||||
'defaults to os.arch(): %s mapped to distro arch: %s',
|
||||
async (osArch: string, distroArch: string) => {
|
||||
jest
|
||||
.spyOn(os, 'arch')
|
||||
.mockReturnValue(osArch as ReturnType<typeof os.arch>);
|
||||
jest.spyOn(os, 'platform').mockReturnValue('linux');
|
||||
mockOsArch.mockReturnValue(osArch);
|
||||
mockOsPlatform.mockReturnValue('linux');
|
||||
|
||||
const version = '17';
|
||||
const distro = new MicrosoftDistributions({
|
||||
@@ -170,10 +258,8 @@ describe('findPackageForDownload', () => {
|
||||
])(
|
||||
'defaults to os.arch(): %s mapped to distro arch: %s',
|
||||
async (osArch: string, distroArch: string) => {
|
||||
jest
|
||||
.spyOn(os, 'arch')
|
||||
.mockReturnValue(osArch as ReturnType<typeof os.arch>);
|
||||
jest.spyOn(os, 'platform').mockReturnValue('win32');
|
||||
mockOsArch.mockReturnValue(osArch);
|
||||
mockOsPlatform.mockReturnValue('win32');
|
||||
|
||||
const version = '17';
|
||||
const distro = new MicrosoftDistributions({
|
||||
@@ -217,7 +303,7 @@ describe('findPackageForDownload', () => {
|
||||
statusCode: 200,
|
||||
headers: {}
|
||||
});
|
||||
jest.spyOn(os, 'platform').mockReturnValue('linux');
|
||||
mockOsPlatform.mockReturnValue('linux');
|
||||
|
||||
const result = await distribution['findPackageForDownload']('17.0.10');
|
||||
|
||||
@@ -228,16 +314,14 @@ describe('findPackageForDownload', () => {
|
||||
});
|
||||
|
||||
describe('downloadTool', () => {
|
||||
let spyDownloadTool: jest.SpyInstance;
|
||||
let spyExtractJdkFile: jest.SpyInstance;
|
||||
let spyCacheDir: jest.SpyInstance;
|
||||
let spyVerifySignature: jest.SpyInstance;
|
||||
let distribution: MicrosoftDistributions;
|
||||
let spyDownloadTool: any;
|
||||
let spyExtractJdkFile: any;
|
||||
let spyCacheDir: any;
|
||||
let spyVerifySignature: any;
|
||||
let distribution: InstanceType<typeof MicrosoftDistributions>;
|
||||
|
||||
beforeEach(() => {
|
||||
jest
|
||||
.spyOn(os, 'platform')
|
||||
.mockReturnValue(process.platform as ReturnType<typeof os.platform>);
|
||||
mockOsPlatform.mockReturnValue(process.platform);
|
||||
|
||||
distribution = new MicrosoftDistributions({
|
||||
version: '17',
|
||||
@@ -246,27 +330,27 @@ describe('downloadTool', () => {
|
||||
checkLatest: false
|
||||
});
|
||||
|
||||
spyDownloadTool = jest.spyOn(tc, 'downloadTool');
|
||||
spyDownloadTool = tc.downloadTool as jest.Mock;
|
||||
spyDownloadTool.mockImplementation(async () => {
|
||||
return '/tmp/jdk.tar.gz';
|
||||
});
|
||||
|
||||
spyExtractJdkFile = jest.spyOn(util, 'extractJdkFile');
|
||||
spyExtractJdkFile = util.extractJdkFile as jest.Mock;
|
||||
spyExtractJdkFile.mockImplementation(async () => {
|
||||
return '/tmp/unpacked';
|
||||
});
|
||||
|
||||
jest.spyOn(fs, 'readdirSync').mockReturnValue(['jdk'] as any);
|
||||
spyCacheDir = jest.spyOn(tc, 'cacheDir');
|
||||
mockReaddirSync.mockReturnValue(['jdk'] as any);
|
||||
spyCacheDir = tc.cacheDir as jest.Mock;
|
||||
spyCacheDir.mockImplementation(async () => {
|
||||
return '/tmp/cached';
|
||||
});
|
||||
|
||||
jest
|
||||
.spyOn(util, 'renameWinArchive')
|
||||
.mockImplementation((archivePath: string) => `${archivePath}.zip`);
|
||||
(util.renameWinArchive as jest.Mock<any>).mockImplementation(
|
||||
(archivePath: string) => `${archivePath}.zip`
|
||||
);
|
||||
|
||||
spyVerifySignature = jest.spyOn(gpg, 'verifyPackageSignature');
|
||||
spyVerifySignature = gpg.verifyPackageSignature as jest.Mock;
|
||||
spyVerifySignature.mockImplementation(async () => {});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user