setup-php/src/coverage.ts

150 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-10-08 19:42:54 +07:00
import * as utils from './utils';
import * as extensions from './extensions';
import * as config from './config';
export async function checkXdebugError(
extension: string,
version: string
): Promise<string> {
if (
(/^5\.[3-6]$|^7\.[0-1]$/.test(version) && extension == 'xdebug3') ||
(/^8\.[0-9]$/.test(version) && extension == 'xdebug2')
) {
return extension + ' is not supported on PHP ' + version;
}
return '';
}
2019-10-12 00:39:05 +07:00
/**
* Function to set up Xdebug
2019-10-12 00:39:05 +07:00
*
2020-06-20 07:55:19 +07:00
* @param extension
2019-10-12 00:39:05 +07:00
* @param version
2022-01-29 06:59:58 +07:00
* @param os
2019-12-26 20:01:18 +07:00
* @param pipe
2019-10-12 00:39:05 +07:00
*/
2019-11-24 03:34:12 +07:00
export async function addCoverageXdebug(
2020-06-20 07:55:19 +07:00
extension: string,
2019-10-08 19:42:54 +07:00
version: string,
2022-01-29 06:59:58 +07:00
os: string,
2019-12-26 20:01:18 +07:00
pipe: string
2019-10-08 19:42:54 +07:00
): Promise<string> {
2021-03-23 08:20:05 +07:00
let script = '\n';
const error: string = await checkXdebugError(extension, version);
if (!error) {
script +=
2022-01-29 06:59:58 +07:00
(await extensions.addExtension(':pcov:false', version, os, true)) + pipe;
extension = extension == 'xdebug3' ? 'xdebug' : extension;
script +=
2022-01-29 06:59:58 +07:00
(await extensions.addExtension(extension, version, os, true)) + pipe;
2022-06-30 19:06:40 +07:00
script += await utils.setVariable(
'xdebug_version',
'php -r "echo phpversion(\'xdebug\');"',
os
);
script +=
(await utils.getCommand(os, 'extension_log')) +
'xdebug "Xdebug $xdebug_version enabled as coverage driver"';
} else {
script += await utils.addLog('$cross', extension, error, os);
}
2021-03-23 08:20:05 +07:00
return script;
2019-10-08 19:42:54 +07:00
}
2019-10-12 00:39:05 +07:00
/**
* Function to set up PCOV
2019-10-12 00:39:05 +07:00
*
* @param version
2022-01-29 06:59:58 +07:00
* @param os
2019-12-26 20:01:18 +07:00
* @param pipe
2019-10-12 00:39:05 +07:00
*/
2019-11-24 03:34:12 +07:00
export async function addCoveragePCOV(
version: string,
2022-01-29 06:59:58 +07:00
os: string,
2019-12-26 20:01:18 +07:00
pipe: string
2019-11-24 03:34:12 +07:00
): Promise<string> {
let script = '\n';
switch (true) {
2019-10-08 19:42:54 +07:00
default:
2021-03-23 08:20:05 +07:00
script +=
2022-01-29 06:59:58 +07:00
(await extensions.addExtension(':xdebug:false', version, os, true)) +
2021-03-23 08:20:05 +07:00
pipe;
2019-10-17 03:11:13 +07:00
script +=
2022-01-29 06:59:58 +07:00
(await extensions.addExtension('pcov', version, os, true)) + pipe;
script += (await config.addINIValues('pcov.enabled=1', os, true)) + '\n';
2022-06-30 19:06:40 +07:00
script += await utils.setVariable(
'pcov_version',
'php -r "echo phpversion(\'pcov\');"',
os
);
script +=
(await utils.getCommand(os, 'extension_log')) +
'pcov "PCOV $pcov_version enabled as coverage driver"';
2019-10-08 19:42:54 +07:00
break;
case /5\.[3-6]|7\.0/.test(version):
2019-10-17 03:11:13 +07:00
script += await utils.addLog(
'$cross',
'pcov',
'PHP 7.1 or newer is required',
2022-01-29 06:59:58 +07:00
os
2019-10-08 19:42:54 +07:00
);
break;
}
return script;
}
2019-10-12 00:39:05 +07:00
/**
* Function to disable Xdebug and PCOV
*
* @param version
2022-01-29 06:59:58 +07:00
* @param os
2019-12-26 20:01:18 +07:00
* @param pipe
2019-10-12 00:39:05 +07:00
*/
2019-11-24 03:34:12 +07:00
export async function disableCoverage(
version: string,
2022-01-29 06:59:58 +07:00
os: string,
2019-12-26 20:01:18 +07:00
pipe: string
2019-11-24 03:34:12 +07:00
): Promise<string> {
let script = '\n';
2021-03-23 08:20:05 +07:00
script +=
2022-01-29 06:59:58 +07:00
(await extensions.addExtension(':pcov:false', version, os, true)) + pipe;
script +=
2022-01-29 06:59:58 +07:00
(await extensions.addExtension(':xdebug:false', version, os, true)) + pipe;
script += await utils.addLog('$tick', 'none', 'Disabled Xdebug and PCOV', os);
2019-10-08 19:42:54 +07:00
return script;
}
2019-11-24 03:34:12 +07:00
/**
* Function to set coverage driver
*
* @param coverage_driver
* @param version
2022-01-29 06:59:58 +07:00
* @param os
2019-11-24 03:34:12 +07:00
*/
export async function addCoverage(
coverage_driver: string,
version: string,
2022-01-29 06:59:58 +07:00
os: string
2019-11-24 03:34:12 +07:00
): Promise<string> {
2020-01-26 17:45:39 +07:00
coverage_driver = coverage_driver.toLowerCase();
2022-01-29 06:59:58 +07:00
const script: string = '\n' + (await utils.stepLog('Setup Coverage', os));
const pipe: string = (await utils.suppressOutput(os)) + '\n';
2019-11-24 03:34:12 +07:00
switch (coverage_driver) {
case 'pcov':
2022-01-29 06:59:58 +07:00
return script + (await addCoveragePCOV(version, os, pipe));
2019-11-24 03:34:12 +07:00
case 'xdebug':
2020-12-24 19:50:42 +07:00
case 'xdebug2':
case 'xdebug3':
2020-12-24 19:50:42 +07:00
return (
2022-01-29 06:59:58 +07:00
script + (await addCoverageXdebug(coverage_driver, version, os, pipe))
2020-06-20 07:55:19 +07:00
);
2019-11-24 03:34:12 +07:00
case 'none':
2022-01-29 06:59:58 +07:00
return script + (await disableCoverage(version, os, pipe));
2019-11-24 03:34:12 +07:00
default:
return '';
}
}