setup-php/__tests__/install.test.ts

162 lines
5.1 KiB
TypeScript
Raw Normal View History

import * as install from '../src/install';
2020-10-04 20:08:22 +07:00
import * as utils from '../src/utils';
/**
* Mock install.ts
*/
jest.mock('../src/install', () => ({
getScript: jest
.fn()
.mockImplementation(
async (
filename: string,
version: string,
os_version: string
): Promise<string> => {
const extension_csv: string = process.env['extensions'] || '';
const ini_values_csv: string = process.env['ini-values'] || '';
const coverage_driver: string = process.env['coverage'] || '';
let tools_csv: string = process.env['tools'] || '';
let script = 'initial script ' + filename + version + os_version;
script += tools_csv ? 'add_tool' : '';
script += extension_csv ? 'install extensions' : '';
script += coverage_driver ? 'set coverage driver' : '';
script += ini_values_csv ? 'edit php.ini' : '';
return script;
}
),
run: jest.fn().mockImplementation(async (): Promise<string> => {
const os_version: string = process.env['RUNNER_OS'] || '';
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
);
const tool = await utils.scriptTool(os_version);
const filename = os_version + (await utils.scriptExtension(os_version));
return [
await install.getScript(filename, version, os_version),
tool,
filename,
version,
__dirname
].join(' ');
})
}));
/**
* Function to set the process.env
*
* @param version
* @param os
* @param extension_csv
* @param ini_values_csv
* @param coverage_driver
2020-05-22 03:24:13 +07:00
* @param tools
*/
function setEnv(
version: string | number,
os: string,
extension_csv: string,
ini_values_csv: string,
2019-11-07 12:41:35 +07:00
coverage_driver: string,
2020-05-22 03:24:13 +07:00
tools: string
): void {
process.env['php-version'] = version.toString();
process.env['RUNNER_OS'] = os;
process.env['extensions'] = extension_csv;
process.env['ini-values'] = ini_values_csv;
process.env['coverage'] = coverage_driver;
2019-12-27 08:26:49 +07:00
process.env['tools'] = tools;
}
describe('Install', () => {
it('Test install on windows', async () => {
2020-05-22 03:24:13 +07:00
setEnv('7.0', 'win32', '', '', '', '');
let script: string = '' + (await install.run());
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('pwsh win32.ps1 7.0 ' + __dirname);
2019-12-14 11:43:01 +07:00
2020-05-22 03:24:13 +07:00
setEnv('7.3', 'win32', '', '', '', '');
script = '' + (await install.run());
2019-12-14 11:43:01 +07:00
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('pwsh win32.ps1 7.3 ' + __dirname);
2020-05-22 03:24:13 +07:00
setEnv('7.3', 'win32', 'a, b', 'a=b', 'x', '');
script = '' + (await install.run());
expect(script).toContain('initial script');
expect(script).toContain('install extensions');
expect(script).toContain('edit php.ini');
expect(script).toContain('set coverage driver');
2020-10-04 20:08:22 +07:00
expect(script).toContain('pwsh win32.ps1 7.3 ' + __dirname);
});
it('Test install on linux', async () => {
2020-05-22 03:24:13 +07:00
setEnv('7.3', 'linux', '', '', '', '');
let script: string = '' + (await install.run());
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash linux.sh 7.3 ');
setEnv('latest', 'linux', '', '', '', '');
script = '' + (await install.run());
expect(script).toContain('initial script');
2020-11-28 06:46:22 +07:00
expect(script).toContain('bash linux.sh 8.0 ');
2020-05-22 03:24:13 +07:00
setEnv('7.3', 'linux', 'a, b', 'a=b', 'x', 'phpunit');
2019-11-07 12:41:35 +07:00
2020-05-22 03:24:13 +07:00
script = '' + (await install.run());
2019-11-07 12:41:35 +07:00
expect(script).toContain('initial script');
expect(script).toContain('install extensions');
expect(script).toContain('edit php.ini');
expect(script).toContain('set coverage driver');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash linux.sh 7.3');
2019-12-27 08:26:49 +07:00
expect(script).toContain('add_tool');
});
it('Test install on darwin', async () => {
2020-05-22 03:24:13 +07:00
setEnv('7.3', 'darwin', '', '', '', '');
let script: string = '' + (await install.run());
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash darwin.sh 7.3 ' + __dirname);
2020-05-22 03:24:13 +07:00
setEnv('7.3', 'darwin', 'a, b', 'a=b', 'x', '');
script = '' + (await install.run());
expect(script).toContain('initial script');
expect(script).toContain('install extensions');
expect(script).toContain('edit php.ini');
expect(script).toContain('set coverage driver');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash darwin.sh 7.3 ' + __dirname);
});
it('Test malformed version inputs', async () => {
2020-05-22 03:24:13 +07:00
setEnv('7.4.1', 'darwin', '', '', '', '');
let script: string = '' + '' + (await install.run());
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash darwin.sh 7.4 ' + __dirname);
2020-05-22 03:24:13 +07:00
setEnv(8.0, 'darwin', '', '', '', '');
script = '' + (await install.run());
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash darwin.sh 8.0 ' + __dirname);
2020-05-22 03:24:13 +07:00
setEnv(8, 'darwin', '', '', '', '');
script = '' + (await install.run());
expect(script).toContain('initial script');
2020-10-04 20:08:22 +07:00
expect(script).toContain('bash darwin.sh 8.0 ' + __dirname);
setEnv(8.1, 'darwin', '', '', '', '');
script = '' + (await install.run());
expect(script).toContain('initial script');
expect(script).toContain('bash darwin.sh 8.1 ' + __dirname);
});
});