Improve parsing .php-version and tests

This commit is contained in:
Shivam Mathur
2026-09-17 20:53:52 +05:30
parent 0bbee91078
commit 75fa893d48
3 changed files with 55 additions and 19 deletions
+45 -9
View File
@@ -56,12 +56,17 @@ describe('Utils tests', () => {
await expect(utils.parseVersion('foo')).rejects.toThrow(
'Invalid PHP version:'
);
fetchSpy.mockResolvedValue({data: '{ "latest": "8.1.0" }'});
await expect(utils.parseVersion('latest')).rejects.toThrow(
'Invalid PHP version in manifest:'
await expect(utils.parseVersion('8.4\n$(id)')).rejects.toThrow(
'Invalid PHP version:'
);
for (const latest of ['8.1.0', 'pre', 8.4, ['8.4']]) {
fetchSpy.mockResolvedValue({data: JSON.stringify({latest})});
await expect(utils.parseVersion('latest')).rejects.toThrow(
'Invalid PHP version in manifest:'
);
}
fetchSpy.mockReset();
fetchSpy.mockResolvedValueOnce({}).mockResolvedValueOnce({});
await expect(utils.parseVersion('latest')).rejects.toThrow(
@@ -340,8 +345,24 @@ describe('Utils tests', () => {
readFileSync.mockReturnValue('ruby 1.2.3\nphp latest\nnode 20.1.2');
expect(await utils.readPHPVersion()).toBe('latest');
readFileSync.mockReturnValue(' \t8.4 \t\n');
expect(await utils.readPHPVersion()).toBe('8.4');
readFileSync.mockReturnValue(
'#PHP\r\n\r\nruby 1.2.3\r\n \tphp \t latest \t# version\r\nnode 20.1.2'
);
expect(await utils.readPHPVersion()).toBe('latest');
readFileSync.mockReturnValue('php\n8.4');
await expect(utils.readPHPVersion()).rejects.toThrow('Invalid PHP version');
process.env['php-version-file'] = '.tool-versions';
readFileSync.mockReturnValue("ruby 1.2.3\nphp 8.4';id;#\nnode 20.1.2");
await expect(utils.readPHPVersion()).rejects.toThrow('.tool-versions');
delete process.env['php-version-file'];
existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('setup-php');
readFileSync.mockReturnValue('php 8.4 8.5');
await expect(utils.readPHPVersion()).rejects.toThrow('Invalid PHP version');
existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true);
@@ -367,29 +388,44 @@ describe('Utils tests', () => {
const existsSync = jest.spyOn(fs, 'existsSync').mockImplementation();
const readFileSync = jest.spyOn(fs, 'readFileSync').mockImplementation();
process.env['php-version'] = 'bogus';
process.env['php-version'] = '$0';
await expect(utils.readPHPVersion()).rejects.toThrow('php-version input');
delete process.env['php-version'];
existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('bogus');
readFileSync.mockReturnValue(';id');
await expect(utils.readPHPVersion()).rejects.toThrow('.php-version');
existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true);
readFileSync.mockReturnValue('{"platform-overrides":{"php":"bogus"}}');
readFileSync.mockReturnValue('{"platform-overrides":{"php":"`w`"}}');
await expect(utils.readPHPVersion()).rejects.toThrow(
'composer.lock platform-overrides.php'
);
existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true);
readFileSync.mockReturnValue('{"platform-overrides":{"php":8.4}}');
await expect(utils.readPHPVersion()).rejects.toThrow(
'composer.lock platform-overrides.php: number'
);
existsSync
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(true);
readFileSync.mockReturnValue('{"config":{"platform":{"php":"bogus"}}}');
readFileSync.mockReturnValue('{"config":{"platform":{"php":"8.4$(id)"}}}');
await expect(utils.readPHPVersion()).rejects.toThrow(
'composer.json config.platform.php'
);
existsSync
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(true);
readFileSync.mockReturnValue('{"config":{"platform":{"php":["8.4"]}}}');
await expect(utils.readPHPVersion()).rejects.toThrow(
'composer.json config.platform.php: object'
);
existsSync.mockClear();
readFileSync.mockClear();
});
+1 -1
View File
File diff suppressed because one or more lines are too long
+9 -9
View File
@@ -71,15 +71,13 @@ export async function parseVersion(version: string): Promise<string> {
for (const manifestURL of await getManifestURLS()) {
const fetchResult = await fetch.fetch(manifestURL);
if (fetchResult['data'] ?? false) {
const resolved: string | undefined = JSON.parse(fetchResult['data'])[
version
];
const resolved: unknown = JSON.parse(fetchResult['data'])[version];
if (resolved === undefined) {
throw new Error(`Invalid PHP version: ${version.slice(0, 20)}`);
}
if (!/^\d+\.\d+$/.test(resolved)) {
if (typeof resolved !== 'string' || !/^\d+\.\d+$/.test(resolved)) {
throw new Error(
`Invalid PHP version in manifest: ${resolved.slice(0, 10)}`
`Invalid PHP version in manifest: ${typeof resolved === 'string' ? resolved.slice(0, 10) : typeof resolved}`
);
}
return resolved;
@@ -514,10 +512,10 @@ export async function parseExtensionSource(
const VERSION_INPUT_REGEX =
/^(latest|lowest|highest|nightly|master|pre|pre-installed|\d+\.x|\d+(\.\d+){0,2})$/;
function validatePHPVersionInput(version: string, source: string): string {
if (!VERSION_INPUT_REGEX.test(version)) {
function validatePHPVersionInput(version: unknown, source: string): string {
if (typeof version !== 'string' || !VERSION_INPUT_REGEX.test(version)) {
throw new Error(
`Invalid PHP version in ${source}: ${version.slice(0, 20)}`
`Invalid PHP version in ${source}: ${typeof version === 'string' ? version.slice(0, 20) : typeof version}`
);
}
return version;
@@ -535,7 +533,9 @@ export async function readPHPVersion(): Promise<string> {
(await getInput('php-version-file', false)) || '.php-version';
if (fs.existsSync(versionFile)) {
const contents: string = fs.readFileSync(versionFile, 'utf8');
const match = contents.match(/^(?:php\s)?(\S+)$/m);
const match = contents.match(
/^[ \t]*(?:php[ \t]+)?([^\s#]+)[ \t]*(?:#.*)?$/m
);
return validatePHPVersionInput(
match ? match[1] : contents.trim(),
versionFile