From 864dd31b7bbb2073b572ad5525d0ccd225b32638 Mon Sep 17 00:00:00 2001 From: Wataru Kurashima Date: Wed, 31 Aug 2022 13:35:30 +0900 Subject: [PATCH] test: async test -> sync --- __tests__/utils.test.ts | 7 ++++++- dist/index.js | 6 ++---- src/utils.ts | 6 ++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 50732785..2fd4be33 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -52,7 +52,8 @@ describe('Utils tests', () => { expect(await utils.parseVersion('4.x')).toBe(undefined); }); - it('checking resolveVersionInput', async () => { + it('checking resolveVersionInput', () => { + // composer.json expect(utils.parsePhpVersionFile('{"require": {"php": "^8.1"}}')).toBe( '8.1' ); @@ -68,6 +69,10 @@ describe('Utils tests', () => { expect(utils.parsePhpVersionFile('{"require": {"php": "^8.1|^8.0"}}')).toBe( '8.1' ); + // asdf: .tool-versions + expect(utils.parsePhpVersionFile('php 8.1')).toBe('8.1'); + // phpenv: .php-version + expect(utils.parsePhpVersionFile('8.1.1')).toBe('8.1'); }); it('checking parseIniFile', async () => { diff --git a/dist/index.js b/dist/index.js index d0aa2242..db9eaa04 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1070,10 +1070,8 @@ async function resolveVersionInput() { } exports.resolveVersionInput = resolveVersionInput; function parsePhpVersionFile(contents) { - const json = JSON.parse(contents); - const version = json.config?.platform?.php ?? json.require?.php; - const match = version.match(/(\d+(\.\d+))/); - return match ? match[1] : ''; + const match = contents.match(/("php:")?.*?(\d+(\.\d+))/); + return match ? match[2] : ''; } exports.parsePhpVersionFile = parsePhpVersionFile; async function getManifestURL() { diff --git a/src/utils.ts b/src/utils.ts index 206e5674..bf885ff6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -76,10 +76,8 @@ export async function resolveVersionInput(): Promise { } export function parsePhpVersionFile(contents: string): string { - const json = JSON.parse(contents); - const version = json.config?.platform?.php ?? json.require?.php; - const match = version.match(/(\d+(\.\d+))/); - return match ? match[1] : ''; + const match = contents.match(/("php:")?.*?(\d+(\.\d+))/); + return match ? match[2] : ''; } /** Function to get manifest URL