Skip to content

Commit

Permalink
Add support for .tool-versions format in php-version-file
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Dec 23, 2024
1 parent 84f76b1 commit 49679d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ describe('Utils tests', () => {
delete process.env['php-version-file'];
delete process.env['php-version'];

existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('php 8.4.2');
expect(await utils.readPHPVersion()).toBe('8.4.2');

existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('setup-php');
expect(await utils.readPHPVersion()).toBe('setup-php');

existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true);
readFileSync.mockReturnValue(
'{ "platform-overrides": { "php": "7.3.25" } }'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,11 @@ export async function readPHPVersion(): Promise<string> {
const versionFile =
(await getInput('php-version-file', false)) || '.php-version';
if (fs.existsSync(versionFile)) {
return fs.readFileSync(versionFile, 'utf8').replace(/[\r\n]/g, '');
const contents: string = fs.readFileSync(versionFile, 'utf8');
const match: RegExpMatchArray | null = contents.match(
/(?:^php\s+)?(\d+(\.\d+)*)/m
);
return match ? match[1] : contents.trim();
} else if (versionFile !== '.php-version') {
throw new Error(`Could not find '${versionFile}' file.`);
}
Expand Down

0 comments on commit 49679d1

Please sign in to comment.