setup-php/src/coverage.ts

157 lines
3.4 KiB
TypeScript
Raw Normal View History

2019-10-08 18:12:54 +05:30
import * as utils from './utils';
import * as extensions from './extensions';
import * as config from './config';
2019-10-11 23:09:05 +05:30
/**
2019-11-24 02:04:12 +05:30
* Function to setup Xdebug
2019-10-11 23:09:05 +05:30
*
2020-06-20 06:25:19 +05:30
* @param extension
2019-10-11 23:09:05 +05:30
* @param version
* @param os_version
2019-12-26 18:31:18 +05:30
* @param pipe
2019-10-11 23:09:05 +05:30
*/
2019-11-24 02:04:12 +05:30
export async function addCoverageXdebug(
2020-06-20 06:25:19 +05:30
extension: string,
2019-10-08 18:12:54 +05:30
version: string,
2019-12-26 18:31:18 +05:30
os_version: string,
pipe: string
2019-10-08 18:12:54 +05:30
): Promise<string> {
2020-06-20 06:25:19 +05:30
const xdebug =
(await extensions.addExtension(extension, version, os_version, true)) +
pipe;
const ini = await config.addINIValues(
'xdebug.mode=coverage',
os_version,
true
);
2020-06-20 06:25:19 +05:30
const log = await utils.addLog(
'$tick',
extension,
'Xdebug enabled as coverage driver',
os_version
);
return [xdebug, ini, log].join('\n');
2019-10-08 18:12:54 +05:30
}
2019-10-11 23:09:05 +05:30
/**
* Function to setup PCOV
*
* @param version
* @param os_version
2019-12-26 18:31:18 +05:30
* @param pipe
2019-10-11 23:09:05 +05:30
*/
2019-11-24 02:04:12 +05:30
export async function addCoveragePCOV(
version: string,
2019-12-26 18:31:18 +05:30
os_version: string,
pipe: string
2019-11-24 02:04:12 +05:30
): Promise<string> {
let script = '\n';
switch (true) {
2019-10-08 18:12:54 +05:30
default:
2019-10-17 01:41:13 +05:30
script +=
(await extensions.addExtension('pcov', version, os_version, true)) +
2019-12-26 18:31:18 +05:30
pipe +
2019-10-17 01:41:13 +05:30
'\n';
script +=
(await config.addINIValues('pcov.enabled=1', os_version, true)) + '\n';
2019-10-08 18:12:54 +05:30
// add command to disable xdebug and enable pcov
switch (os_version) {
case 'linux':
case 'darwin':
2019-12-26 18:31:18 +05:30
script += 'remove_extension xdebug' + pipe + '\n';
2019-10-08 18:12:54 +05:30
break;
case 'win32':
2019-12-26 18:31:18 +05:30
script += 'Remove-Extension xdebug' + pipe + '\n';
2019-10-08 18:12:54 +05:30
break;
}
// success
2019-10-17 01:41:13 +05:30
script += await utils.addLog(
'$tick',
'coverage: pcov',
2019-10-08 18:12:54 +05:30
'PCOV enabled as coverage driver',
2019-10-17 01:41:13 +05:30
os_version
2019-10-08 18:12:54 +05:30
);
// version is not supported
break;
case /5\.[3-6]|7\.0/.test(version):
2019-10-17 01:41:13 +05:30
script += await utils.addLog(
'$cross',
'pcov',
'PHP 7.1 or newer is required',
os_version
2019-10-08 18:12:54 +05:30
);
break;
}
return script;
}
2019-10-11 23:09:05 +05:30
/**
* Function to disable Xdebug and PCOV
*
* @param version
* @param os_version
2019-12-26 18:31:18 +05:30
* @param pipe
2019-10-11 23:09:05 +05:30
*/
2019-11-24 02:04:12 +05:30
export async function disableCoverage(
version: string,
2019-12-26 18:31:18 +05:30
os_version: string,
pipe: string
2019-11-24 02:04:12 +05:30
): Promise<string> {
let script = '\n';
2019-10-08 18:12:54 +05:30
switch (os_version) {
case 'linux':
case 'darwin':
2019-12-26 18:31:18 +05:30
script += 'remove_extension xdebug' + pipe + '\n';
script += 'remove_extension pcov' + pipe + '\n';
2019-10-08 18:12:54 +05:30
break;
case 'win32':
2019-12-26 18:31:18 +05:30
script += 'Remove-Extension xdebug' + pipe + '\n';
script += 'Remove-Extension pcov' + pipe + '\n';
2019-10-08 18:12:54 +05:30
break;
}
2019-10-17 01:41:13 +05:30
script += await utils.addLog(
'$tick',
'none',
2019-10-08 18:12:54 +05:30
'Disabled Xdebug and PCOV',
2019-10-17 01:41:13 +05:30
os_version
2019-10-08 18:12:54 +05:30
);
return script;
}
2019-11-24 02:04:12 +05:30
/**
* Function to set coverage driver
*
* @param coverage_driver
* @param version
* @param os_version
*/
export async function addCoverage(
coverage_driver: string,
version: string,
os_version: string
): Promise<string> {
2020-01-26 16:15:39 +05:30
coverage_driver = coverage_driver.toLowerCase();
2019-11-24 02:04:12 +05:30
const script: string =
'\n' + (await utils.stepLog('Setup Coverage', os_version));
2019-12-26 18:31:18 +05:30
const pipe: string = await utils.suppressOutput(os_version);
2019-11-24 02:04:12 +05:30
switch (coverage_driver) {
case 'pcov':
2019-12-26 18:31:18 +05:30
return script + (await addCoveragePCOV(version, os_version, pipe));
2019-11-24 02:04:12 +05:30
case 'xdebug':
2020-06-20 06:25:19 +05:30
case 'xdebug3':
return (
script +
(await addCoverageXdebug(coverage_driver, version, os_version, pipe))
);
2019-11-24 02:04:12 +05:30
case 'none':
2019-12-26 18:31:18 +05:30
return script + (await disableCoverage(version, os_version, pipe));
2019-11-24 02:04:12 +05:30
default:
return '';
}
}