Skip to content

Commit

Permalink
end-of-script regex fix, debug capabilities, version++
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtoobii committed Jan 19, 2022
1 parent c4b2505 commit 6778c04
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 `<filpath/filename>_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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.1.2
42 changes: 42 additions & 0 deletions examples/lib/Other/Module.pm
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions examples/my_program.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
8 changes: 4 additions & 4 deletions examples/runit.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions examples/t/number_test.t
Original file line number Diff line number Diff line change
Expand Up @@ -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();

17 changes: 15 additions & 2 deletions lib/Devel/Deanonymize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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<visible>.
Code is based on https://github.com/pjcj/Devel--Cover/issues/51#issuecomment-17222928
=head1 SYNOPSIS
Expand All @@ -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=<include_pattern" prove t/
=head1 DEBUGGING
If your tests suddenly fail for some weird reason, you can set C<DEANONYMIZE_DEBUG>. If this environment variable is set,
we print out the filename for every modified file write its contents to C<filepath/filename_mod.pl>
=head1 EXAMPLES
Expand Down Expand Up @@ -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;

Expand All @@ -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>;
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 6778c04

Please sign in to comment.