(我想我已经看到了这个问题的答案,但我找不到了。我遇到的最接近的是这个问题 https://stackoverflow.com/q/24420346/2877364).
我正在黑客攻击prt https://metacpan.org/pod/distribution/App-PRT/script/prt并正在尝试获取我的更改的完整覆盖数据。我的一些新测试调用system($^X, 'prt', ...)
运行子进程。然后我测试这些过程的输出。但是,Devel::Cover 不会收集子进程中的覆盖率数据。如何收集该数据,而不导致 Devel::Cover 在我执行常规操作时运行make test
?目前我有一个丑陋的解决方法(见下文),但我认为必须有一个更简单的方法。
完整的 MCVE 是here https://github.com/cxw42/multi-process-devel-cover-test。为了方便起见,复制命令行:
git clone https://github.com/cxw42/multi-process-devel-cover-test.git ; cd multi-process-devel-cover-test ; perl Makefile.PL ; make ; cover -test
注释掉第11行t/01-help.t
对于下面描述的解决方法。
Edit我使用的原因system
是要达到 100% 的覆盖率-h
/--help
/--version
。这些开关由Getopt::长 https://metacpan.org/pod/Getopt::Long,这称为exit()
自动地。如果需要的话我可以使用-exitval=>NOEXIT
并用我自己的代码处理退出。
不起作用的事情
PERL5OPT
$ PERL5OPT=-MDevel::Cover=-silent,1 cover -test
/home/cxw/perl5/perlbrew/perls/perlcygbrew-5.26.2-1/bin/cover shouldn't be run with coverage turned on.
HARNESS_PERL_SWITCHES
+cover
$ HARNESS_PERL_SWITCHES=-MDevel::Cover=-silent,1 cover -test
...
t/01-help.t .. # Devel::Cover not covering
...
----- ------ ------ ------ ------ ------ ------ ------
File stmt bran cond sub pod time total
----- ------ ------ ------ ------ ------ ------ ------
Total n/a n/a n/a n/a n/a n/a n/a
----- ------ ------ ------ ------ ------ ------ ------
HARNESS_PERL_SWITCHES
+EUMM make test
$ HARNESS_PERL_SWITCHES=-MDevel::Cover=-silent,1 make test
...
t/01-help.t .. # Devel::Cover not covering
...
$ (export HARNESS_PERL_SWITCHES=-MDevel::Cover=-silent,1 ; make test)
...
t/01-help.t .. # Devel::Cover not covering
...
目前的解决方法
目前我找到的唯一办法就是手动添加-MDevel::Cover
当我调用时到命令行system
。灵感来自这个答案 https://stackoverflow.com/a/24429239/2877364 and 相应的测试 https://github.com/pjcj/Devel--Cover/blob/7f365d2e412675bc741adef43d6cc9353cf1885b/t/internal/criteria.t#L10-L18,我有以下解决方法:
# Detect whether Devel::Cover is running
my $is_covering = !!(eval 'Devel::Cover::get_coverage()');
my @perl = ($^X, $is_covering ? ('-MDevel::Cover=-silent,1') : ());
diag $is_covering ? 'Devel::Cover running' : 'Devel::Cover not covering';
# Pass the Devel::Cover option, if any, to the child process
system(@perl, 'prt', ...);
在 MCVE 中,解决方法是here https://github.com/cxw42/multi-process-devel-cover-test/blob/5395b4f0894790db01773a4f90c4713110f39e63/t/01-help.t#L9-L12它被使用here https://github.com/cxw42/multi-process-devel-cover-test/blob/5395b4f0894790db01773a4f90c4713110f39e63/t/01-help.t#L25.