Support .php-version file; closes #629

This commit is contained in:
Steve Lacey
2023-01-29 13:26:49 +08:00
parent baf9535ba5
commit fd03521cb4
6 changed files with 98 additions and 6 deletions

View File

@ -10,7 +10,7 @@ jest.mock('../src/install', () => ({
.mockImplementation(async (os: string): Promise<string> => {
const filename = os + (await utils.scriptExtension(os));
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
await utils.resolveVersion()
);
const ini_file: string = await utils.parseIniFile(
await utils.getInput('ini-file', false)

View File

@ -1,3 +1,4 @@
import fs from 'fs';
import * as path from 'path';
import * as utils from '../src/utils';
@ -7,7 +8,8 @@ import * as utils from '../src/utils';
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation(key => {
return ['setup-php'].indexOf(key) !== -1 ? key : '';
})
}),
info: jest.fn()
}));
/**
@ -261,6 +263,31 @@ describe('Utils tests', () => {
);
});
it('checking resolveVersion', async () => {
await expect(utils.resolveVersion()).rejects.toThrow(
"Neither 'php-version' nor 'php-version-file' inputs were supplied, and could not find '.php-version' file."
);
process.env['php-version-file'] = '.phpenv-version';
await expect(utils.resolveVersion()).rejects.toThrow(
"Could not find '.phpenv-version' file."
);
const existsSync = jest.spyOn(fs, 'existsSync').mockImplementation();
const readFileSync = jest.spyOn(fs, 'readFileSync').mockImplementation();
existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('8.1');
expect(await utils.resolveVersion()).toBe('8.1');
process.env['php-version'] = '8.2';
expect(await utils.resolveVersion()).toBe('8.2');
existsSync.mockClear();
readFileSync.mockClear();
});
it('checking setVariable', async () => {
let script: string = await utils.setVariable('var', 'command', 'linux');
expect(script).toEqual('\nvar="$(command)"\n');