etussum12/覆盖率检查器

允许检查单个pull请求的代码覆盖率

1.1.1 2024-06-25 15:05 UTC

README

允许旧代码使用新标准

Build Status Coverage Status Scrutinizer Code Quality

覆盖率检查器允许通过仅对新/编辑的代码强制执行新标准来增量实现新标准。

像phpcs和phpmd这样的工具是全有或全无的方法,覆盖率检查器允许它与diff一起工作,即强制执行所有pull请求/变更请求。

这有时被称为“基线”

还与PHPunit合作,例如允许90%的新/编辑代码被覆盖,这将随着时间的推移提高整体覆盖率。

安装

Composer

使用Composer只需

composer require --dev exussum12/coverage-checker

然后调用所需的脚本

使用Phar

Phar是一种打包格式,应该是一个单独的下载。最新的Phar可以在这里找到。

下载后运行chmod +x diffFilter.phar,然后作为./diffFilter.phar调用,后跟正常选项

手动

将此存储库克隆到您可以访问构建计划的地方,首选composer install,但如果没有安装composer,将使用非composer类加载器。如果没有使用composer,某些PHP特定功能可能无法按预期工作。然后调用所需的脚本

用法

首先需要一个diff

git diff origin/master... > diff.txt

有关应生成哪些diff的更深入示例,请参阅这里

然后为要检查的工具输出(例如phpcs,PHPUnit,phpmd等)

 phpcs --standard=psr2 --report=json > phpcs.json || true 

这里|| true确保如果phpcs失败,整个构建不会失败。

然后调用diffFilter

 ./vendor/bin/diffFilter --phpcs diff.txt phpcs.json 100

最后一个参数(本例中为100)是可选的,默认值为100。可以将此降低到90等,以确保至少90%的更改代码符合标准。如果更改的代码通过最低覆盖率,diffFilter将退出状态为0。否则为2

扩展指南

更深入的指南可以在wiki上找到,还有一些关于加快构建速度的提示。

作为git钩子安装

GitHooks目录中有两个示例钩子,如果您将其链接到这些钩子,则diffFilter将在本地运行。

pre-commit在提交之前发生,pre-receive将阻止您推送

可用diff过滤器完整列表

以下是所有工具及其简要描述的列表

--buddy		Parses buddy (magic number detection) output
--checkstyle	Parses a report in checkstyle format
--clover	Parses text output in clover (xml) format
--codeclimate	Parse codeclimate output
--humbug	Parses the json report format of humbug (mutation testing)
--infecton	Parses the infection text log format
--jacoco	Parses xml coverage report produced by Jacoco
--phan		Parse the default phan(static analysis) output
--phanJson	Parses phan (static analysis) in json format
--phpcpd	Parses the text output from phpcpd (Copy Paste Detect)
--phpcs		Parses the json report format of phpcs, this mode only reports errors as violations
--phpcsStrict	Parses the json report format of phpcs, this mode reports errors and warnings as violations
--phpmd		Parses the xml report format of phpmd, this mode reports multi line violations once per diff, instead of on each line
		the violation occurs
--phpmdStrict	Parses the xml report format of phpmd, this mode reports multi line violations once per line they occur 
--phpmnd	Parses the text output of phpmnd (Magic Number Detection)
--phpstan	Parses the text output of phpstan
--phpunit	Parses text output in clover (xml) format generated with coverage-clover=file.xml
--pylint	Parses PyLint output
--psalm		Parses Psalm output

以信息模式运行

只需将第三个参数传递为0,这将显示失败的行,但不会使构建失败

为什么不运行自动修复器

确实存在某些工具的自动修复器,但在大型代码库中,有许多情况无法自动修复。覆盖率检查器允许通过强制所有更改符合新标准,将新标准应用于代码中最常用的部分。

什么是diff过滤器测试

diff过滤器测试是一种使用已知点的执行和差异(diff)的测试。这些信息可用于仅运行已更改的测试,从而在许多情况下节省运行测试的分钟。

良好的工作流程是分支,使用--coverage-php=php-coverage.php运行测试,然后运行测试时运行git diff origin/master... > diff.txt && ./composer/bin/phpunit

这将在第一步中保存覆盖率信息,然后diff会过滤出可运行的测试。

这次一次性的工作可以避免在每次运行时执行不必要的测试,因为这些测试的代码没有变化。

有关安装和使用的更多信息,请查看Wiki