diff --git a/CHANGES b/CHANGES index beaf3e5..bd0a141 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,9 @@ +0.1.2 2022-01-19 Tobias Bossert (tobib at cpan.org) + +- Fixed replace regex that (end-of-script marker) +- Added debug capabilities +- Improved example + 0.1.1 2021-06-24 Tobias Bossert (tobib at cpan.org) - Fixed code in Synopsis diff --git a/Readme.md b/Readme.md index 5a8f624..bc9a6b1 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,7 @@ # Devel::Deanonymize -A small tool to make anonymous subs visible to Devel::Coverage (and possibly similar Modules) +A small tool to make anonymous subs visible to Devel::Coverage (and possibly similar Modules). +Code is based on https://github.com/pjcj/Devel--Cover/issues/51#issuecomment-17222928 ## Synopsys @@ -18,6 +19,11 @@ HARNESS_PERL_SWITCHES="-MDevel::Cover=-ignore,^t/,Deanonymize -MDevel::Deanonymi cover -report html ``` +## Debugging + +If your tests suddenly fail for some weird reason, you can set `DEANONYMIZE_DEBUG`. If this environment variable is set, +we print out the filename for every modified file write its contents to `_mod.pl` + ## Coverage Reports Per default, `Devel::Cover` creates a folder named `cover_db` inside the project root. To visualize the result, we have to diff --git a/VERSION b/VERSION index 6da28dd..8294c18 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.1 \ No newline at end of file +0.1.2 \ No newline at end of file diff --git a/examples/lib/Other/Module.pm b/examples/lib/Other/Module.pm new file mode 100644 index 0000000..a674550 --- /dev/null +++ b/examples/lib/Other/Module.pm @@ -0,0 +1,42 @@ +package Other::Module; +use strict; +use warnings FATAL => 'all'; + +use experimental 'signatures'; + +use base 'Exporter'; +our @EXPORT = qw(is_it_the_number2 is_the_sum_the_number2); + +my $anon = sub($number) { + if ($number != 42) { + return "No, it's not"; + } +}; + +sub is_it_the_number2($number) { + if ($number == 42) { + return "It is the number"; + } + else { + &{$anon}($number); + } +} + +sub is_the_sum_the_number2($number1, $number2) { + # extra complicated check + if ($number1 > 42 or $number2 > 42) { + return "No, its not"; + } + elsif (($number2 == 42 or $number1 == 42) and ($number1 + $number2 == 0)) { + return "It is the number"; + } + elsif ($number1 + $number2 == 42) { + return "It is the number" + } + else { + &{$anon}($number1 + $number2); + } + +} + +1; \ No newline at end of file diff --git a/examples/my_program.pl b/examples/my_program.pl index 787bbf1..f3880ca 100644 --- a/examples/my_program.pl +++ b/examples/my_program.pl @@ -2,6 +2,7 @@ use strict; use warnings FATAL => 'all'; use Fancy::Module; +use Other::Module; is_it_the_number(21); is_it_the_number(42); @@ -15,5 +16,17 @@ is_the_sum_the_number(43,21); is_the_sum_the_number(21,43); is_the_sum_the_number(21,21); +is_it_the_number2(21); +is_it_the_number2(42); +is_the_sum_the_number2(21,21); +is_the_sum_the_number2(42,0); +is_the_sum_the_number2(0,42); +is_the_sum_the_number2(42,42); +is_the_sum_the_number2(23,42); +is_the_sum_the_number2(42,23); +is_the_sum_the_number2(43,43); +is_the_sum_the_number2(43,21); +is_the_sum_the_number2(21,43); +is_the_sum_the_number2(21,21); print("done\n"); \ No newline at end of file diff --git a/examples/runit.sh b/examples/runit.sh index 55b74a4..c42f9f5 100644 --- a/examples/runit.sh +++ b/examples/runit.sh @@ -1,18 +1,18 @@ #!/usr/bin/env bash -#PERL_DIR=/home/tbossert/.plenv/versions/5.32.1/bin -PERL_DIR=/usr/bin/ +PERL_DIR=/home/tbossert/.plenv/versions/5.32.1/bin +#PERL_DIR=/usr/bin/ # Delete old coverage Data #$PERL_DIR/cover -delete # Run tests -#HARNESS_PERL_SWITCHES="-MDevel::Cover=-ignore,^t/,Deanonymize -MDevel::Deanonymize=Fancy" $PERL_DIR/prove t/ -I lib/ -I ../lib +#HARNESS_PERL_SWITCHES='-MDevel::Cover=-ignore,^t/,Deanonymize -MDevel::Deanonymize=Fancy|Other' $PERL_DIR/prove t/ -I lib/ -I ../lib #HARNESS_PERL_SWITCHES="-MDevel::Cover=-ignore,^t/,Deanonymize" $PERL_DIR/prove t/ -I lib/ -I ../lib # Run script -$PERL_DIR/perl -MDevel::Cover=-ignore,^t/,Deanonymize -MDevel::Deanonymize=Fancy -I lib/ -I ../lib my_program.pl +$PERL_DIR/perl -MDevel::Cover=-ignore,^t/,Deanonymize -MDevel::Deanonymize='Fancy|Other' -I lib/ -I ../lib my_program.pl $PERL_DIR/cover -report html \ No newline at end of file diff --git a/examples/t/number_test.t b/examples/t/number_test.t index 62d342d..ae4b342 100644 --- a/examples/t/number_test.t +++ b/examples/t/number_test.t @@ -3,9 +3,13 @@ use strict; use warnings; use Test::More; use Fancy::Module; +use Other::Module; ok is_it_the_number(41) eq "No, it's not", 'was not the number'; ok is_it_the_number(42) eq "It is the number", 'was the number'; +ok is_it_the_number2(41) eq "No, it's not", 'was not the number'; +ok is_it_the_number2(42) eq "It is the number", 'was the number'; + done_testing(); diff --git a/lib/Devel/Deanonymize.pm b/lib/Devel/Deanonymize.pm index deb1039..5662cc3 100644 --- a/lib/Devel/Deanonymize.pm +++ b/lib/Devel/Deanonymize.pm @@ -12,6 +12,7 @@ by the statistic: } This script aims to solve this problem by wrapping each file in a sub and thus making these subs I. +Code is based on https://github.com/pjcj/Devel--Cover/issues/51#issuecomment-17222928 =head1 SYNOPSIS @@ -22,6 +23,11 @@ This script aims to solve this problem by wrapping each file in a sub and thus m HARNESS_PERL_SWITCHES="-MDevel::Cover=-ignore,^t/,Deanonymize -MDevel::Deanonymize=. If this environment variable is set, +we print out the filename for every modified file write its contents to C + =head1 EXAMPLES @@ -65,7 +71,7 @@ package Devel::Deanonymize; use strict; use warnings FATAL => 'all'; -our $VERSION = "0.1.1"; +our $VERSION = "0.1.2"; my $include_pattern; @@ -84,6 +90,7 @@ sub modify_files { my (undef, $filename) = @_; return () if ($filename !~ /$include_pattern/); if (my $found = (grep {-e $_} map {"$_/$filename"} grep {!ref} @INC)[0]) { + print "Devel::Deanonymize: $found" . "\n" if $ENV{DEANONYMIZE_DEBUG}; local $/ = undef; open my $fh, '<', $found or die("Can't read module file $found\n"); my $module_text = <$fh>; @@ -97,7 +104,7 @@ sub modify_files { # define everything in a sub, so Devel::Cover will DTRT # NB this introduces no extra linefeeds so D::C's line numbers # in reports match the file on disk - $module_text =~ s/(.*?package\s+\S+)(.*)(__END__|1;|__DATA__)/$1sub classWrapper {$2} classWrapper();/s; + $module_text =~ s/(.*?package\s+\S+)(.*)(__END__|1;|__DATA__)/$1sub classWrapper {$2} classWrapper();\n$3/s; # unhide private methods to avoid "Variable will not stay shared" # warnings that appear due to change of applicable scoping rules @@ -108,6 +115,12 @@ sub modify_files { # filehandle on the scalar open $fh, '<', \$module_text; + if ($ENV{DEANONYMIZE_DEBUG}) { + open my $mod_fh, '>', $found . "_mod.pl"; + print $mod_fh $module_text; + close $mod_fh; + } + # and put it into %INC too so that it looks like we loaded the code # from the file directly $INC{$filename} = $found;