File Coverage

blib/lib/Dist/Zilla/Plugin/PurePerlTests.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::PurePerlTests;
2             # git description: 774b4f7
3              
4             $Dist::Zilla::Plugin::PurePerlTests::VERSION = '0.04';
5 1     1   420 use strict;
  1         2  
  1         29  
6 1     1   4 use warnings;
  1         1  
  1         20  
7              
8 1     1   223 use Dist::Zilla::File::InMemory;
  0            
  0            
9              
10             use Moose;
11             use Moose::Autobox;
12              
13             with 'Dist::Zilla::Role::FileGatherer';
14              
15             has env_var => (
16             is => 'ro',
17             isa => 'Str',
18             required => 1,
19             );
20              
21             sub gather_files {
22             my $self = shift;
23              
24             for my $file ( grep { $_->name() =~ m<\At/.+\.t\z> }
25             $self->zilla()->files()->flatten() ) {
26              
27             next if $file->isa('Dist::Zilla::File::InMemory');
28              
29             $self->_copy_file($file);
30             }
31             }
32              
33             sub _copy_file {
34             my $self = shift;
35             my $file = shift;
36              
37             ( my $name = $file->name() ) =~ s{/([^/]+)$}{/release-pp-$1};
38              
39             my $content = $file->content();
40              
41             return if $content =~ /^\#\s*no\s+pp\s+test\s*$/m;
42              
43             my $env_var = $self->env_var();
44              
45             my $perl_line = q{};
46              
47             if ( $content =~ s/^(\#![^\n]+)\n// ) {
48             $perl_line = $1;
49             }
50              
51             $content = <<"EOF";
52             $perl_line
53              
54             use Test::More;
55              
56             BEGIN {
57             unless ( \$ENV{RELEASE_TESTING} ) {
58             plan skip_all => 'these tests are for release testing';
59             }
60              
61             \$ENV{$env_var} = 1;
62             }
63              
64             $content
65             EOF
66              
67             $self->log( 'rewriting ' . $file->name() . " to $name" );
68              
69             my $pp_file = Dist::Zilla::File::InMemory->new(
70             name => $name,
71             content => $content,
72             );
73              
74             $self->add_file($pp_file);
75             }
76              
77             __PACKAGE__->meta()->make_immutable();
78              
79             1;
80              
81             # ABSTRACT: Run all your tests twice, once with XS code and once with pure Perl
82              
83             __END__
84              
85             =pod
86              
87             =head1 NAME
88              
89             Dist::Zilla::Plugin::PurePerlTests - Run all your tests twice, once with XS code and once with pure Perl
90              
91             =head1 VERSION
92              
93             version 0.04
94              
95             =head1 SYNOPSIS
96              
97             In your F<dist.ini>:
98              
99             [PurePerlTests]
100             env_var = MY_MODULE_PURE_PERL
101              
102             =head1 DESCRIPTION
103              
104             This plugin is for modules which ship with a dual XS/pure Perl implementation.
105              
106             The plugin makes a copy of all your tests when doing release testing (via
107             C<dzil test> or C<dzil release>). The copy will set an environment value that
108             you specify in a C<BEGIN> block. You can use this to force your code to not
109             load the XS implementation.
110              
111             =for Pod::Coverage gather_files
112              
113             =head1 CONFIGURATION
114              
115             This plugin takes one configuration key, "env_var", which is required.
116              
117             =head1 SKIPPING TESTS
118              
119             If you don't want to run the a given test file in pure Perl mode, you can put
120             a comment like this in your test:
121              
122             # no pp test
123              
124             This must be on a line by itself. This plugin will skip any tests which
125             contain this comment.
126              
127             =head1 SUPPORT
128              
129             Please report any bugs or feature requests to
130             C<bug-dist-zilla-plugin-pureperltests@rt.cpan.org>, or through the web
131             interface at L<http://rt.cpan.org>. I will be notified, and then you'll
132             automatically be notified of progress on your bug as I make changes.
133              
134             =head1 DONATIONS
135              
136             If you'd like to thank me for the work I've done on this module, please
137             consider making a "donation" to me via PayPal. I spend a lot of free time
138             creating free software, and would appreciate any support you'd care to offer.
139              
140             Please note that B<I am not suggesting that you must do this> in order for me
141             to continue working on this particular software. I will continue to do so,
142             inasmuch as I have in the past, for as long as it interests me.
143              
144             Similarly, a donation made in this way will probably not make me work on this
145             software much more, unless I get so many donations that I can consider working
146             on free software full time, which seems unlikely at best.
147              
148             To donate, log into PayPal and send money to autarch@urth.org or use the
149             button on this page: L<http://www.urth.org/~autarch/fs-donation.html>
150              
151             =head1 AUTHOR
152              
153             Dave Rolsky <autarch@urth.org>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is Copyright (c) 2010 - 2014 by Dave Rolsky.
158              
159             This is free software, licensed under:
160              
161             The Artistic License 2.0 (GPL Compatible)
162              
163             =cut