CC: | module-build [...] perl.org |
Subject: | patch to allow embedded ok() calls in block in no_leaks_ok |
I have a test script like:
use Test::More;
...
my $thing = ...
do_stuff_to($thing);
is( get_stuff_from($thing), ... );
ok( $thing->do_otherstuff, ... );
...
I would like to adapt this test to check for memory leaks at the same
time, with something like:
use Test::More;
use Test::LeakTrace;
...
no_leaks_ok {
my $thing = ...
do_stuff_to($thing);
is( get_stuff_from($thing), ... );
ok( $thing->do_otherstuff, ... );
...
};
... but that won't currently work because the ok() call creates scalars
that survive the block (causing false positive leak alerts) and
no_leaks_ok runs the block more than once, messing up the test counts.
The attached patch is the Test::LeakTrace side of a fix for this. It
also requires a change to Module::Build to add a disable() method to
temporarily make ok() and friends into no-ops. Module::Build
maintainers cc-ed.
Subject: | patch.txt |
--- /usr/local/lib/perl/5.10.0/Test/LeakTrace.pm 2009-07-01 08:30:26.000000000 +0200
+++ /tmp/LeakTrace.pm 2009-07-30 17:10:01.000000000 +0200
@@ -73,15 +73,19 @@
my $Test = __PACKAGE__->builder;
+ # call to prepare cache in $block, and to run the tests if block includes
+ # calls to ok(), is(), fail(), etc.
+ $block->();
+
if(!_runops_installed()){
my $mod = exists $INC{'Devel/Cover.pm'} ? 'Devel::Cover' : 'strange runops routines';
return $Test->ok(1, "skipped (under $mod)");
}
- # calls to prepare cache in $block
- $block->();
-
+ # suppress the side effects of ok() etc calls within the block.
+ $Test->disable(1);
my $got = _do_leaktrace($block, 'leaked_count', 0);
+ $Test->disable(0);
my $desc = sprintf 'leaks %s %-2s %s', $got, $cmp_op, $expected;
if(defined $description){