Migrate to ESM and upgrade dependencies (#1574)

* Migrate to ESM

* updated package.json version

* Update README for v6 and clean up dev dependencies

* Remove unused ts-node

* fix audit issue

---------

Co-authored-by: gowridurgad <gowridurgad@gmail.com>
This commit is contained in:
gowridurgad
2026-07-14 08:08:27 +05:30
committed by GitHub
parent 04be95cf35
commit 8207627860
86 changed files with 108799 additions and 102919 deletions
+225 -103
View File
@@ -1,51 +1,163 @@
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as httpm from '@actions/http-client';
import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import {
jest,
describe,
it,
expect,
beforeEach,
afterEach,
afterAll
} from '@jest/globals';
import {fileURLToPath} from 'url';
import fs from 'fs';
import cp from 'child_process';
import osm from 'os';
import path from 'path';
import * as main from '../src/main';
import * as auth from '../src/authutil';
import OfficialBuilds from '../src/distributions/official_builds/official_builds';
import {INodeVersion} from '../src/distributions/base-models';
import nodeTestManifest from './data/versions-manifest.json';
import nodeTestDist from './data/node-dist-index.json';
import nodeTestDistNightly from './data/node-nightly-index.json';
import nodeTestDistRc from './data/node-rc-index.json';
import nodeV8CanaryTestDist from './data/v8-canary-dist-index.json';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Mock @actions modules before importing anything that depends on them
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)
}));
jest.unstable_mockModule('@actions/io', () => ({
which: jest.fn(),
mkdirP: jest.fn(),
rmRF: jest.fn(),
mv: jest.fn(),
cp: jest.fn()
}));
jest.unstable_mockModule('@actions/tool-cache', () => ({
find: jest.fn(),
findAllVersions: jest.fn(),
downloadTool: jest.fn(),
extractTar: jest.fn(),
extractZip: jest.fn(),
extractXar: jest.fn(),
extract7z: jest.fn(),
cacheDir: jest.fn(),
cacheFile: jest.fn(),
getManifestFromRepo: jest.fn(),
findFromManifest: jest.fn(),
HTTPError: class HTTPError extends Error {
readonly httpStatusCode: number;
constructor(httpStatusCode?: number) {
super(`Unexpected HTTP response: ${httpStatusCode}`);
this.httpStatusCode = httpStatusCode ?? 0;
}
}
}));
const _mockGetJson = jest.fn();
jest.unstable_mockModule('@actions/http-client', () => ({
HttpClient: jest.fn().mockImplementation(() => ({
getJson: _mockGetJson
}))
}));
jest.unstable_mockModule('@actions/exec', () => ({
exec: jest.fn(),
getExecOutput: jest.fn()
}));
jest.unstable_mockModule('@actions/cache', () => ({
saveCache: jest.fn(),
restoreCache: jest.fn(),
isFeatureAvailable: jest.fn()
}));
// Pre-import real authutil before mocking
const realAuth = await import('../src/authutil.js');
jest.unstable_mockModule('../src/authutil.js', () => ({
...realAuth,
configAuthentication: jest.fn()
}));
// Dynamic imports after mocking
const core = await import('@actions/core');
const io = await import('@actions/io');
const tc = await import('@actions/tool-cache');
const httpm = await import('@actions/http-client');
const exec = await import('@actions/exec');
const cache = await import('@actions/cache');
const main = await import('../src/main.js');
const auth = await import('../src/authutil.js');
const {default: OfficialBuilds} =
await import('../src/distributions/official_builds/official_builds.js');
const {default: nodeTestManifest} = await import(
'./data/versions-manifest.json',
{with: {type: 'json'}}
);
const {default: nodeTestDist} = await import('./data/node-dist-index.json', {
with: {type: 'json'}
});
const {default: nodeTestDistNightly} = await import(
'./data/node-nightly-index.json',
{with: {type: 'json'}}
);
const {default: nodeTestDistRc} = await import('./data/node-rc-index.json', {
with: {type: 'json'}
});
const {default: nodeV8CanaryTestDist} = await import(
'./data/v8-canary-dist-index.json',
{with: {type: 'json'}}
);
import type {INodeVersion} from '../src/distributions/base-models.js';
import type {IToolRelease} from '@actions/tool-cache';
describe('setup-node', () => {
let build: OfficialBuilds;
let build: InstanceType<typeof OfficialBuilds>;
let inputs = {} as any;
let os = {} as any;
let inSpy: jest.SpyInstance;
let findSpy: jest.SpyInstance;
let findAllVersionsSpy: jest.SpyInstance;
let cnSpy: jest.SpyInstance;
let logSpy: jest.SpyInstance;
let warningSpy: jest.SpyInstance;
let getManifestSpy: jest.SpyInstance;
let platSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance;
let dlSpy: jest.SpyInstance;
let exSpy: jest.SpyInstance;
let cacheSpy: jest.SpyInstance;
let dbgSpy: jest.SpyInstance;
let whichSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance;
let readFileSyncSpy: jest.SpyInstance;
let mkdirpSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
let authSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;
let getExecOutputSpy: jest.SpyInstance;
let getJsonSpy: jest.SpyInstance;
let inSpy: jest.Mock;
let findSpy: jest.Mock;
let findAllVersionsSpy: jest.Mock;
let cnSpy: jest.SpiedFunction<typeof process.stdout.write>;
let logSpy: jest.Mock;
let warningSpy: jest.Mock;
let addPathSpy: jest.Mock;
let setFailedSpy: jest.Mock;
let getManifestSpy: jest.Mock;
let platSpy: jest.SpiedFunction<typeof osm.platform>;
let archSpy: jest.SpiedFunction<typeof osm.arch>;
let dlSpy: jest.Mock;
let exSpy: jest.Mock;
let cacheSpy: jest.Mock;
let dbgSpy: jest.Mock;
let whichSpy: jest.Mock;
let existsSpy: jest.SpiedFunction<typeof fs.existsSync>;
let readFileSyncSpy: jest.Mock;
let mkdirpSpy: jest.Mock;
let execSpy: jest.SpiedFunction<typeof cp.execSync>;
let authSpy: jest.Mock;
let isCacheActionAvailable: jest.Mock;
let getExecOutputSpy: jest.Mock;
let getJsonSpy: jest.Mock;
beforeEach(() => {
// @actions/core
@@ -53,8 +165,8 @@ describe('setup-node', () => {
process.env['GITHUB_PATH'] = ''; // Stub out ENV file functionality so we can verify it writes to standard out
process.env['GITHUB_OUTPUT'] = ''; // Stub out ENV file functionality so we can verify it writes to standard out
inputs = {};
inSpy = jest.spyOn(core, 'getInput');
inSpy.mockImplementation(name => inputs[name]);
inSpy = core.getInput as jest.Mock;
inSpy.mockImplementation((name: any) => inputs[name]);
// node
os = {};
@@ -65,34 +177,61 @@ describe('setup-node', () => {
execSpy = jest.spyOn(cp, 'execSync');
// @actions/tool-cache
findSpy = jest.spyOn(tc, 'find');
findAllVersionsSpy = jest.spyOn(tc, 'findAllVersions');
dlSpy = jest.spyOn(tc, 'downloadTool');
exSpy = jest.spyOn(tc, 'extractTar');
cacheSpy = jest.spyOn(tc, 'cacheDir');
getManifestSpy = jest.spyOn(tc, 'getManifestFromRepo');
findSpy = tc.find as jest.Mock;
findAllVersionsSpy = tc.findAllVersions as jest.Mock;
dlSpy = tc.downloadTool as jest.Mock;
exSpy = tc.extractTar as jest.Mock;
cacheSpy = tc.cacheDir as jest.Mock;
getManifestSpy = tc.getManifestFromRepo as jest.Mock;
// Restore findFromManifest after clearMocks.
// In ESM, the real tc.findFromManifest uses os.platform() from its own
// module namespace, which jest.spyOn on our os import cannot override.
// So we provide a minimal reimplementation that uses the test's os var.
(tc.findFromManifest as jest.Mock).mockImplementation(
async (versionSpec: any, stable: any, manifest: any, archFilter: any) => {
const arch = archFilter || os['arch'] || 'x64';
const plat = os['platform'] || process.platform;
const semverModule = await import('semver');
for (const rel of manifest || []) {
if (
semverModule.default.satisfies(rel.version, versionSpec, {
includePrerelease: true
}) &&
rel.stable === stable
) {
const file = rel.files.find(
(f: any) => f.arch === arch && f.platform === plat
);
if (file) return {...rel, files: [file]};
}
}
return undefined;
}
);
// http-client
getJsonSpy = jest.spyOn(httpm.HttpClient.prototype, 'getJson');
getJsonSpy = _mockGetJson;
(httpm.HttpClient as jest.Mock).mockImplementation(() => ({
getJson: _mockGetJson
}));
// io
whichSpy = jest.spyOn(io, 'which');
whichSpy = io.which as jest.Mock;
existsSpy = jest.spyOn(fs, 'existsSync');
mkdirpSpy = jest.spyOn(io, 'mkdirP');
mkdirpSpy = io.mkdirP as jest.Mock;
// @actions/tool-cache
isCacheActionAvailable = jest.spyOn(cache, 'isFeatureAvailable');
isCacheActionAvailable = cache.isFeatureAvailable as jest.Mock;
// disable authentication portion for installer tests
authSpy = jest.spyOn(auth, 'configAuthentication');
authSpy = auth.configAuthentication as jest.Mock;
authSpy.mockImplementation(() => {});
// gets
getManifestSpy.mockImplementation(
() => <tc.IToolRelease[]>nodeTestManifest
);
getManifestSpy.mockImplementation(() => <IToolRelease[]>nodeTestManifest);
getJsonSpy.mockImplementation(url => {
getJsonSpy.mockImplementation((url: any) => {
let res: any;
if (url.includes('/rc')) {
res = <INodeVersion[]>nodeTestDistRc;
@@ -107,28 +246,18 @@ describe('setup-node', () => {
// writes
cnSpy = jest.spyOn(process.stdout, 'write');
logSpy = jest.spyOn(core, 'info');
dbgSpy = jest.spyOn(core, 'debug');
warningSpy = jest.spyOn(core, 'warning');
cnSpy.mockImplementation(line => {
// uncomment to debug
process.stderr.write('write:' + line + '\n');
});
logSpy.mockImplementation(line => {
// uncomment to debug
process.stderr.write('log:' + line + '\n');
});
dbgSpy.mockImplementation(msg => {
// uncomment to see debug output
// process.stderr.write(msg + '\n');
});
warningSpy.mockImplementation(msg => {
// uncomment to debug
// process.stderr.write('log:' + msg + '\n');
});
logSpy = core.info as jest.Mock;
dbgSpy = core.debug as jest.Mock;
warningSpy = core.warning as jest.Mock;
addPathSpy = core.addPath as jest.Mock;
setFailedSpy = core.setFailed as jest.Mock;
cnSpy.mockImplementation(() => true);
logSpy.mockImplementation(() => {});
dbgSpy.mockImplementation(() => {});
warningSpy.mockImplementation(() => {});
// @actions/exec
getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
getExecOutputSpy = exec.getExecOutput as jest.Mock;
getExecOutputSpy.mockImplementation(() => 'v16.15.0');
});
@@ -156,7 +285,7 @@ describe('setup-node', () => {
async (versionSpec, platform, expectedVersion, expectedLts) => {
os.platform = platform;
os.arch = 'x64';
const versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
const versions: IToolRelease[] | null = await tc.getManifestFromRepo(
'actions',
'node-versions',
'mocktoken'
@@ -187,7 +316,7 @@ describe('setup-node', () => {
it('finds version in cache with stable not supplied', async () => {
inputs['node-version'] = '12';
inSpy.mockImplementation(name => inputs[name]);
inSpy.mockImplementation((name: any) => inputs[name]);
const toolPath = path.normalize('/cache/node/12.16.1/x64');
findSpy.mockImplementation(() => toolPath);
@@ -199,14 +328,14 @@ describe('setup-node', () => {
it('finds version in cache and adds it to the path', async () => {
inputs['node-version'] = '12';
inSpy.mockImplementation(name => inputs[name]);
inSpy.mockImplementation((name: any) => inputs[name]);
const toolPath = path.normalize('/cache/node/12.16.1/x64');
findSpy.mockImplementation(() => toolPath);
await main.run();
const expPath = path.join(toolPath, 'bin');
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
expect(addPathSpy).toHaveBeenCalledWith(expPath);
});
it('handles unhandled find error and reports error', async () => {
@@ -219,7 +348,7 @@ describe('setup-node', () => {
await main.run();
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + osm.EOL);
expect(setFailedSpy).toHaveBeenCalledWith(errMsg);
});
//--------------------------------------------------
@@ -247,7 +376,7 @@ describe('setup-node', () => {
const toolPath = path.normalize('/cache/node/12.16.2/x64');
exSpy.mockImplementation(async () => '/some/other/temp/path');
cacheSpy.mockImplementation(async () => toolPath);
whichSpy.mockImplementation(cmd => {
whichSpy.mockImplementation((cmd: any) => {
return `some/${cmd}/path`;
});
@@ -278,7 +407,7 @@ describe('setup-node', () => {
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
expect(addPathSpy).toHaveBeenCalledWith(expPath);
});
it('falls back to a version from node dist from mirror', async () => {
@@ -314,7 +443,7 @@ describe('setup-node', () => {
);
expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
expect(addPathSpy).toHaveBeenCalledWith(expPath);
});
it('falls back to a version from node dist', async () => {
@@ -348,7 +477,7 @@ describe('setup-node', () => {
);
expect(dlSpy).toHaveBeenCalled();
expect(exSpy).toHaveBeenCalled();
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
expect(addPathSpy).toHaveBeenCalledWith(expPath);
});
it('does not find a version that does not exist', async () => {
@@ -367,8 +496,8 @@ describe('setup-node', () => {
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(cnSpy).toHaveBeenCalledWith(
`::error::Unable to find Node version '${versionSpec}' for platform ${os.platform} and architecture ${os.arch}.${osm.EOL}`
expect(setFailedSpy).toHaveBeenCalledWith(
`Unable to find Node version '${versionSpec}' for platform ${os.platform} and architecture ${os.arch}.`
);
});
@@ -390,7 +519,7 @@ describe('setup-node', () => {
});
await main.run();
expect(cnSpy).toHaveBeenCalledWith(`::error::${errMsg}${osm.EOL}`);
expect(setFailedSpy).toHaveBeenCalledWith(errMsg);
});
it('reports when download failed but version exists', async () => {
@@ -583,7 +712,7 @@ describe('setup-node', () => {
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
expect(addPathSpy).toHaveBeenCalledWith(expPath);
});
it('fallback to dist if manifest is not available', async () => {
@@ -626,7 +755,7 @@ describe('setup-node', () => {
expect(logSpy).toHaveBeenCalledWith(
`Attempting to download ${versionSpec}...`
);
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
expect(addPathSpy).toHaveBeenCalledWith(expPath);
});
});
@@ -668,9 +797,7 @@ describe('setup-node', () => {
`Found LTS release '${expectedVersion}' for Node version 'lts/${lts}'`
);
expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
expect(cnSpy).toHaveBeenCalledWith(
`::add-path::${path.join(toolPath, 'bin')}${osm.EOL}`
);
expect(addPathSpy).toHaveBeenCalledWith(path.join(toolPath, 'bin'));
}
);
@@ -728,9 +855,7 @@ describe('setup-node', () => {
);
expect(logSpy).toHaveBeenCalledWith('Extracting ...');
expect(logSpy).toHaveBeenCalledWith('Adding to the cache ...');
expect(cnSpy).toHaveBeenCalledWith(
`::add-path::${path.join(toolPath, 'bin')}${osm.EOL}`
);
expect(addPathSpy).toHaveBeenCalledWith(path.join(toolPath, 'bin'));
}
);
@@ -750,8 +875,8 @@ describe('setup-node', () => {
expect(dbgSpy).toHaveBeenCalledWith(
'Getting manifest from actions/node-versions@main'
);
expect(cnSpy).toHaveBeenCalledWith(
`::error::Unable to parse LTS alias for Node version 'lts/'${osm.EOL}`
expect(setFailedSpy).toHaveBeenCalledWith(
`Unable to parse LTS alias for Node version 'lts/'`
);
});
@@ -774,8 +899,8 @@ describe('setup-node', () => {
expect(dbgSpy).toHaveBeenCalledWith(
`LTS alias 'unknown' for Node version 'lts/unknown'`
);
expect(cnSpy).toHaveBeenCalledWith(
`::error::Unable to find LTS release 'unknown' for Node version 'lts/unknown'.${osm.EOL}`
expect(setFailedSpy).toHaveBeenCalledWith(
`Unable to find LTS release 'unknown' for Node version 'lts/unknown'.`
);
});
@@ -799,9 +924,7 @@ describe('setup-node', () => {
expect(dbgSpy).toHaveBeenCalledWith(
'Getting manifest from actions/node-versions@main'
);
expect(cnSpy).toHaveBeenCalledWith(
`::error::Unable to download manifest${osm.EOL}`
);
expect(setFailedSpy).toHaveBeenCalledWith('Unable to download manifest');
});
});
@@ -871,7 +994,6 @@ describe('setup-node', () => {
darwin: 'darwin',
win32: 'win'
}[os.platform];
inputs['node-version'] = version;
inputs['architecture'] = arch;
inputs['token'] = 'faketoken';