File Coverage

blib/lib/Dist/Zilla/Plugin/ExtraTests.pm
Criterion Covered Total %
statement 27 27 100.0
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 38 40 95.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::ExtraTests 6.037;
2             # ABSTRACT: rewrite ./xt tests to ./t tests with skips
3              
4 11     11   9697 use Moose;
  11         28  
  11         104  
5             with 'Dist::Zilla::Role::FileMunger';
6              
7 11     11   76681 use Dist::Zilla::Pragmas;
  11         25  
  11         110  
8              
9 11     11   81 use namespace::autoclean;
  11         27  
  11         163  
10              
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This plugin rewrites tests found in the following directories:
14             #pod
15             #pod ./xt/author - tests for author testing (env AUTHOR_TESTING is true)
16             #pod ./xt/release - tests for pre-release testers (env RELEASE_TESTING is true)
17             #pod ./xt/smoke - tests for automated testers (env AUTOMATED_TESTING is true)
18             #pod
19             #pod The tests are renamed and moved to F<./t>, and they are rewritten to include
20             #pod some simple Perl code to skip all included tests if the correct env vars are
21             #pod not set.
22             #pod
23             #pod =cut
24              
25             sub munge_file {
26 84     84 0 150 my ($self, $file) = @_;
27              
28 84 100       293 return unless $file->name =~ m{\Axt/(smoke|author|release)/.+\.t\z};
29 20         94 my $method = "_rewrite_$1\_test";
30              
31 20         101 $self->log("rewriting $1 test " . $file->name);
32              
33 20         6670 $self->$method($file);
34             }
35              
36             sub _rewrite_smoke_test {
37 1     1   4 my ($self, $file) = @_;
38 1         4 $self->_rewrite($file, 'AUTOMATED_TESTING', '"smoke bot" testing');
39             }
40              
41             sub _rewrite_author_test {
42 18     18   54 my ($self, $file) = @_;
43 18         71 $self->_rewrite($file, 'AUTHOR_TESTING', 'testing by the author');
44             }
45              
46             sub _rewrite_release_test {
47 1     1   4 my ($self, $file) = @_;
48 1         7 $self->_rewrite($file, 'RELEASE_TESTING', 'release candidate testing');
49             }
50              
51             sub _rewrite {
52 20     20   61 my ($self, $file, $env, $msg) = @_;
53              
54 20         127 my $name = $file->name =~ s{^xt/([^/]+)/}{t/$1-}r;
55              
56 20         92 $file->name($name);
57              
58 20         94 my @lines = split /\n/, $file->content;
59 20 50       138 my $after = $lines[0] =~ /\A#!/ ? 1 : 0;
60 20         84 splice @lines, $after, 0, qq|
61             BEGIN {
62             unless (\$ENV{$env}) {
63             print qq{1..0 # SKIP these tests are for $msg\\n};
64             exit
65             }
66             }
67             |;
68              
69 20         552 $file->content(join "\n", @lines, '');
70             }
71              
72             __PACKAGE__->meta->make_immutable;
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Dist::Zilla::Plugin::ExtraTests - rewrite ./xt tests to ./t tests with skips
84              
85             =head1 VERSION
86              
87             version 6.037
88              
89             =head1 DESCRIPTION
90              
91             This plugin rewrites tests found in the following directories:
92              
93             ./xt/author - tests for author testing (env AUTHOR_TESTING is true)
94             ./xt/release - tests for pre-release testers (env RELEASE_TESTING is true)
95             ./xt/smoke - tests for automated testers (env AUTOMATED_TESTING is true)
96              
97             The tests are renamed and moved to F<./t>, and they are rewritten to include
98             some simple Perl code to skip all included tests if the correct env vars are
99             not set.
100              
101             =head1 PERL VERSION
102              
103             This module should work on any version of perl still receiving updates from
104             the Perl 5 Porters. This means it should work on any version of perl
105             released in the last two to three years. (That is, if the most recently
106             released version is v5.40, then this module should work on both v5.40 and
107             v5.38.)
108              
109             Although it may work on older versions of perl, no guarantee is made that the
110             minimum required version will not be increased. The version may be increased
111             for any reason, and there is no promise that patches will be accepted to
112             lower the minimum required perl.
113              
114             =head1 AUTHOR
115              
116             Ricardo SIGNES 😏 <cpan@semiotic.systems>
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2026 by Ricardo SIGNES.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut