Migrate the action and tests to ES modules

This commit is contained in:
Shivam Mathur
2026-10-09 00:14:33 +05:30
parent f1477a078f
commit 11b9f9a7f0
21 changed files with 95 additions and 97 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import * as config from '../src/config';
import * as config from '../src/config.js';
describe('Config tests', () => {
it.each`
+2 -1
View File
@@ -1,4 +1,5 @@
import * as core from '../src/core';
import {jest} from '@jest/globals';
import * as core from '../src/core.js';
describe('Core tests', () => {
const originalEnv = process.env;
+1 -1
View File
@@ -1,4 +1,4 @@
import * as coverage from '../src/coverage';
import * as coverage from '../src/coverage.js';
describe('Config tests', () => {
it.each`
+1 -1
View File
@@ -1,5 +1,5 @@
import * as fs from 'fs';
import * as extensions from '../src/extensions';
import * as extensions from '../src/extensions.js';
describe('Extension tests', () => {
it.each`
+1 -1
View File
@@ -1,4 +1,4 @@
import * as fetch from '../src/fetch';
import * as fetch from '../src/fetch.js';
import nock from 'nock';
beforeAll(() => nock.disableNetConnect());
+7 -5
View File
@@ -1,12 +1,11 @@
import * as install from '../src/install';
import * as utils from '../src/utils';
import {jest} from '@jest/globals';
/**
* Mock install.ts
*/
jest.mock('../src/install', () => ({
jest.unstable_mockModule('../src/install.js', () => ({
getScript: jest
.fn()
.fn<typeof import('../src/install.js').getScript>()
.mockImplementation(async (os: string): Promise<string> => {
const filename = os + (await utils.scriptExtension(os));
const version: string = await utils.parseVersion(
@@ -36,7 +35,7 @@ jest.mock('../src/install', () => ({
/**
* Mock fetch.ts
*/
jest.mock('../src/fetch', () => ({
jest.unstable_mockModule('../src/fetch.js', () => ({
fetch: jest.fn().mockImplementation(() => {
return {
data: '{ "latest": "8.3", "lowest": "8.1", "highest": "8.3", "nightly": "8.4", "5.x": "5.6" }'
@@ -44,6 +43,9 @@ jest.mock('../src/fetch', () => ({
})
}));
const utils = await import('../src/utils.js');
const install = await import('../src/install.js');
describe('Install', () => {
it.each`
version | os | extension_csv | ini_file | ini_values_csv | coverage_driver | tools | output
+1 -1
View File
@@ -1,4 +1,4 @@
import * as packagist from '../src/packagist';
import * as packagist from '../src/packagist.js';
import nock from 'nock';
beforeAll(() => nock.disableNetConnect());
+23 -50
View File
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as tools from '../src/tools';
import {ToolData, ToolInput} from '../src/tools';
import {jest} from '@jest/globals';
import fs from 'fs';
import type {ToolData, ToolInput} from '../src/tools.js';
function getData(data: Partial<ToolData>): ToolData {
const tool = data.tool || 'tool';
@@ -40,9 +40,9 @@ function unsetComposerAuthEnv(): void {
/**
* Mock fetch.ts
*/
jest.mock('../src/fetch', () => ({
jest.unstable_mockModule('../src/fetch.js', () => ({
fetch: jest
.fn()
.fn<typeof import('../src/fetch.js').fetch>()
.mockImplementation(
async (url: string, token?: string): Promise<Record<string, string>> => {
if (url.includes('deployer')) {
@@ -86,9 +86,9 @@ jest.mock('../src/fetch', () => ({
)
}));
jest.mock('../src/packagist', () => ({
jest.unstable_mockModule('../src/packagist.js', () => ({
search: jest
.fn()
.fn<typeof import('../src/packagist.js').search>()
.mockImplementation(
async (
package_name: string,
@@ -102,6 +102,10 @@ jest.mock('../src/packagist', () => ({
)
}));
const tools = await import('../src/tools.js');
afterEach(() => jest.restoreAllMocks());
describe('Tools tests', () => {
it.each`
token | version
@@ -527,30 +531,11 @@ describe('Tools tests', () => {
});
it('checking affected composer version with CRLF ranges', async () => {
let affected = false;
let fixed = true;
await jest.isolateModulesAsync(async () => {
jest.doMock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: (
filePath: fs.PathOrFileDescriptor,
options?: unknown
) => {
if (String(filePath).includes('composer-gh-auth-no-op')) {
return '1.0.0-0 1.10.28\r\n2.0.0-0 2.2.28\r\n2.3.0-0 2.9.8';
}
return (jest.requireActual('fs') as typeof fs).readFileSync(
filePath,
options as fs.ObjectEncodingOptions & {flag?: string}
);
}
}));
const isolatedTools = jest.requireActual<typeof tools>('../src/tools');
affected = isolatedTools.skipGitHubAuthForComposerVersion('2.9.7');
fixed = isolatedTools.skipGitHubAuthForComposerVersion('2.9.8');
});
expect(affected).toBe(true);
expect(fixed).toBe(false);
jest
.spyOn(fs, 'readFileSync')
.mockReturnValue('1.0.0-0 1.10.28\r\n2.0.0-0 2.2.28\r\n2.3.0-0 2.9.8');
expect(tools.skipGitHubAuthForComposerVersion('2.9.7')).toBe(true);
expect(tools.skipGitHubAuthForComposerVersion('2.9.8')).toBe(false);
});
it.each`
@@ -948,26 +933,14 @@ describe('Tools tests', () => {
}
});
let result: string = '';
await jest.isolateModulesAsync(async () => {
jest.doMock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: (
filePath: fs.PathOrFileDescriptor,
options?: unknown
) => {
if (String(filePath).includes('tools.json')) {
return brokenToolsJson;
}
return (jest.requireActual('fs') as typeof fs).readFileSync(
filePath,
options as fs.ObjectEncodingOptions & {flag?: string}
);
}
}));
const isolatedTools = jest.requireActual<typeof tools>('../src/tools');
result = await isolatedTools.addTools('broken-tool', '7.4', 'linux');
const readFileSync = fs.readFileSync;
jest.spyOn(fs, 'readFileSync').mockImplementation((filePath, options) => {
if (String(filePath).includes('tools.json')) {
return brokenToolsJson;
}
return readFileSync(filePath, options);
});
const result = await tools.addTools('broken-tool', '7.4', 'linux');
expect(result).toContain(
'add_log "$cross" "broken-tool" "broken-tool has no function defined. Please report this issue."'
+13 -8
View File
@@ -1,8 +1,13 @@
import {fileURLToPath} from 'url';
import {jest} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import * as path from 'path';
import * as utils from '../src/utils';
import * as fetchModule from '../src/fetch';
const fetchMock = jest.fn<typeof import('../src/fetch.js').fetch>();
jest.unstable_mockModule('../src/fetch.js', () => ({fetch: fetchMock}));
const utils = await import('../src/utils.js');
const dirname = path.dirname(fileURLToPath(import.meta.url));
describe('Utils tests', () => {
it('checking readEnv', async () => {
@@ -40,9 +45,9 @@ describe('Utils tests', () => {
});
it('checking parseVersion', async () => {
const fetchSpy = jest.spyOn(fetchModule, 'fetch').mockResolvedValue({
const fetchSpy = fetchMock.mockResolvedValue({
data: fs.readFileSync(
path.join(__dirname, '../src/configs/php-versions.json'),
path.join(dirname, '../src/configs/php-versions.json'),
'utf8'
)
});
@@ -324,8 +329,8 @@ describe('Utils tests', () => {
"Could not find '.phpenv-version' file."
);
const existsSync = jest.spyOn(fs, 'existsSync').mockImplementation();
const readFileSync = jest.spyOn(fs, 'readFileSync').mockImplementation();
const existsSync = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const readFileSync = jest.spyOn(fs, 'readFileSync').mockReturnValue('');
existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('8.1');
@@ -391,8 +396,8 @@ describe('Utils tests', () => {
});
it('readPHPVersion rejects unsupported values from each source', async () => {
const existsSync = jest.spyOn(fs, 'existsSync').mockImplementation();
const readFileSync = jest.spyOn(fs, 'readFileSync').mockImplementation();
const existsSync = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const readFileSync = jest.spyOn(fs, 'readFileSync').mockReturnValue('');
process.env['php-version'] = '$0';
await expect(utils.readPHPVersion()).rejects.toThrow('php-version input');
+1 -1
View File
File diff suppressed because one or more lines are too long
+3
View File
@@ -0,0 +1,3 @@
{
"type": "module"
}
+6 -2
View File
@@ -1,11 +1,15 @@
module.exports = {
export default {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1'
},
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': ['ts-jest', {tsconfig: 'tsconfig.test.json'}]
'^.+\\.ts$': ['ts-jest', {tsconfig: 'tsconfig.test.json', useESM: true}]
},
verbose: true,
collectCoverage: true
+1
View File
@@ -3,6 +3,7 @@
"version": "2.40.0",
"private": false,
"description": "Setup PHP for use with GitHub Actions",
"type": "module",
"main": "lib/install.js",
"types": "lib/install.d.ts",
"directories": {
+1 -1
View File
@@ -1,4 +1,4 @@
import * as utils from './utils';
import * as utils from './utils.js';
/**
* Add script to set custom ini values for unix
+3 -3
View File
@@ -1,6 +1,6 @@
import * as utils from './utils';
import * as extensions from './extensions';
import * as config from './config';
import * as utils from './utils.js';
import * as extensions from './extensions.js';
import * as config from './config.js';
export async function checkXdebugError(
extension: string,
+1 -1
View File
@@ -1,4 +1,4 @@
import * as utils from './utils';
import * as utils from './utils.js';
/**
* Install and enable extensions for darwin
+11 -8
View File
@@ -1,11 +1,15 @@
import {fileURLToPath} from 'url';
import path from 'path';
import {exec} from '@actions/exec';
import fs from 'fs';
import * as config from './config';
import * as core from './core';
import * as coverage from './coverage';
import * as extensions from './extensions';
import * as tools from './tools';
import * as utils from './utils';
import * as config from './config.js';
import * as core from './core.js';
import * as coverage from './coverage.js';
import * as extensions from './extensions.js';
import * as tools from './tools.js';
import * as utils from './utils.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Build the script
@@ -15,7 +19,7 @@ import * as utils from './utils';
export async function getScript(os: string): Promise<string> {
const url = 'https://setup-php.com/sponsor';
const filename = os + (await utils.scriptExtension(os));
const script_path = path.join(__dirname, '../src/scripts', filename);
const script_path = path.join(dirname, '../src/scripts', filename);
const run_path = script_path.replace(os, 'run');
const extension_csv: string = utils.sanitizeShellInput(
await utils.getInput('extensions', false),
@@ -65,7 +69,6 @@ export async function run(): Promise<void> {
const os: string = process.platform;
const tool = await utils.scriptTool(os);
const run_path = await getScript(os);
const {exec} = await import(/* webpackMode: "eager" */ '@actions/exec');
await exec(tool + run_path);
}
+1 -1
View File
@@ -1,5 +1,5 @@
import * as cv from 'compare-versions';
import * as fetch from './fetch';
import * as fetch from './fetch.js';
type RS = Record<string, string>;
type RSRS = Record<string, RS>;
+8 -5
View File
@@ -1,9 +1,12 @@
import {fileURLToPath} from 'url';
import path from 'path';
import fs from 'fs';
import * as cv from 'compare-versions';
import * as fetch from './fetch';
import * as packagist from './packagist';
import * as utils from './utils';
import * as fetch from './fetch.js';
import * as packagist from './packagist.js';
import * as utils from './utils.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Valid function names for custom tool handlers
@@ -155,7 +158,7 @@ export function skipGitHubAuthForComposerVersion(version: string): boolean {
}
return fs
.readFileSync(
path.join(__dirname, '../src/configs/composer-gh-auth-no-op'),
path.join(dirname, '../src/configs/composer-gh-auth-no-op'),
'utf8'
)
.trim()
@@ -672,7 +675,7 @@ export async function getData(
php_version: string,
os: string
): Promise<ToolData> {
const json_file_path = path.join(__dirname, '../src/configs/tools.json');
const json_file_path = path.join(dirname, '../src/configs/tools.json');
const json_file: string = fs.readFileSync(json_file_path, 'utf8');
const json_objects: Record<string, ToolConfig> = JSON.parse(json_file);
release = release.replace(/\s+/g, '');
+6 -3
View File
@@ -1,7 +1,10 @@
import {fileURLToPath} from 'url';
import fs from 'fs';
import * as path from 'path';
import * as core from './core';
import * as fetch from './fetch';
import * as core from './core.js';
import * as fetch from './fetch.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Function to read environment variable and return a string value.
@@ -480,7 +483,7 @@ export async function customPackage(
const pkg_name: string = pkg.replace(/\d+|(pdo|pecl)[_-]|[_-]db2/, '');
const script_extension: string = await scriptExtension(os);
const script: string = path.join(
__dirname,
dirname,
'../src/scripts/' + type + '/' + pkg_name + script_extension
);
const command: string = await getCommand(os, pkg_name);
+3 -3
View File
@@ -5,11 +5,11 @@
"lib": [
"ES2025"
],
"module": "Node20",
"moduleResolution": "Node16",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitAny": true,
"outDir": "./lib",
"removeComments": false,
"removeComments": true,
"rootDir": "./src",
"sourceMap": true,
"strict": true,