File Coverage

blib/lib/Dist/Zilla/Plugin/Test/DiagINC.pm
Criterion Covered Total %
statement 41 42 97.6
branch 5 6 83.3
condition n/a
subroutine 10 10 100.0
pod 0 3 0.0
total 56 61 91.8


line stmt bran cond sub pod time code
1 1     1   2530419 use 5.008001;
  1         2  
2 1     1   4 use strict;
  1         1  
  1         19  
3 1     1   3 use warnings;
  1         1  
  1         51  
4              
5             package Dist::Zilla::Plugin::Test::DiagINC;
6             # ABSTRACT: Add Test::DiagINC to all .t files
7              
8             our $VERSION = '0.002';
9              
10 1     1   4 use Moose;
  1         1  
  1         8  
11             with(
12             'Dist::Zilla::Role::FileMunger',
13             'Dist::Zilla::Role::FileFinderUser' => {
14             default_finders => [':TestFiles'],
15             },
16             'Dist::Zilla::Role::PPI',
17             'Dist::Zilla::Role::PrereqSource',
18             );
19              
20 1     1   4749 use PPI;
  1         84590  
  1         41  
21 1     1   472 use Syntax::Keyword::Junction qw/any/;
  1         6023  
  1         7  
22 1     1   115 use namespace::autoclean;
  1         2  
  1         8  
23              
24             sub munge_files {
25 1     1 0 83692 my ($self) = @_;
26 1         1 $self->munge_file($_) for grep { $_->name =~ /\.t$/ } @{ $self->found_files };
  1         1161  
  1         6  
27             }
28              
29             sub munge_file {
30 1     1 0 35 my ( $self, $file ) = @_;
31              
32 1         5 my $document = $self->ppi_document_for_file($file);
33              
34             # using ::Comment is a hack for adding code copied from PkgVersion
35 1         4626 my $add =
36             PPI::Token::Comment->new(q[use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC'; ]);
37              
38 1         6 my $was_munged;
39              
40             # XXX should errors get reported? -- xdg, 2014-02-04
41 1         1 eval {
42 1         2 my @includes = @{ $document->find('PPI::Statement::Include') };
  1         5  
43              
44 1         1022 for my $s (@includes) {
45 4 100       135 next if $s->version;
46 3 100       43 next if $s->module eq any(qw/strict warnings/);
47 1         25 $was_munged = $s->first_token->insert_before($add);
48 1         62 last;
49             }
50             };
51              
52 1 50       3 if ($was_munged) {
53 1         5 $self->save_ppi_document_to_file( $document, $file );
54 1         1188 $self->log_debug( [ "added Test::DiagINC line to %s", $file->name ] );
55             }
56             else {
57 0         0 $self->log( [ "skipping %s: couldn't add Test::DiagINC line", $file->name ] );
58             }
59              
60             }
61              
62             sub register_prereqs {
63 1     1 0 2251 my $self = shift;
64              
65 1         23 $self->zilla->register_prereqs(
66             {
67             phase => 'test',
68             type => 'requires',
69             },
70             'Test::DiagINC' => '0.002',
71             );
72             }
73              
74             __PACKAGE__->meta->make_immutable;
75              
76             1;
77              
78              
79             # vim: ts=4 sts=4 sw=4 et:
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Dist::Zilla::Plugin::Test::DiagINC - Add Test::DiagINC to all .t files
90              
91             =head1 VERSION
92              
93             version 0.002
94              
95             =head1 SYNOPSIS
96              
97             # in dist.ini
98             [Test::DiagINC]
99              
100             =head1 DESCRIPTION
101              
102             This L<Dist::Zilla> plugin adds the following L<Test::DiagINC> line to all
103             C<.t> files under the C<t/> directory:
104              
105             use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC';
106              
107             It will be inserted before the first module loaded (without adding a line
108             to preserve line numbering), excluding C<strict> and C<warnings>. This
109             makes sure that it is loaded before L<Test::More>, which L<Test::DiagINC>
110             requires.
111              
112             For example, it will turn this:
113              
114             use 5.008001;
115             use strict;
116             use warnings;
117              
118             use Test::More;
119             # etc.
120              
121             Into this:
122              
123             use 5.008001;
124             use strict;
125             use warnings;
126              
127             use if $ENV{AUTOMATED_TESTING}, 'Test::DiagINC'; use Test::More;
128             # etc.
129              
130             =for Pod::Coverage BUILD munge_files munge_file register_prereqs
131              
132             =head1 RATIONALE
133              
134             Prerequisite reporting modules like L<Dist::Zilla::Plugin::Test::ReportPrereqs>
135             and similar modules give an overview of prerequisites, but don't generally list
136             I<deep> dependencies — i.e. the modules used by the modules you use.
137              
138             L<Dist::Zilla::Plugin::Test::PrereqsFromMeta> offers a feature to report from
139             C<%INC> after loading all prerequisites, but it doesn't cover all types of
140             dependencies and can't account for optional dependencies.
141              
142             What I find most relevant is knowing exactly what modules are loaded when any
143             given test fails. This would include test modules, optional modules and so on.
144             It is I<specific> to the failure situation.
145              
146             That sort of output is also verbose, so this plugin only generates that output
147             if C<$ENV{AUTOMATED_TESTING}> is true. That means it will show up on CPAN
148             Testers, but not clutter up manual test output, which seems to me like the
149             right trade-off.
150              
151             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
152              
153             =head1 SUPPORT
154              
155             =head2 Bugs / Feature Requests
156              
157             Please report any bugs or feature requests through the issue tracker
158             at L<https://github.com/dagolden/Dist-Zilla-Plugin-Test-DiagINC/issues>.
159             You will be notified automatically of any progress on your issue.
160              
161             =head2 Source Code
162              
163             This is open source software. The code repository is available for
164             public review and contribution under the terms of the license.
165              
166             L<https://github.com/dagolden/Dist-Zilla-Plugin-Test-DiagINC>
167              
168             git clone https://github.com/dagolden/Dist-Zilla-Plugin-Test-DiagINC.git
169              
170             =head1 AUTHOR
171              
172             David Golden <dagolden@cpan.org>
173              
174             =head1 COPYRIGHT AND LICENSE
175              
176             This software is Copyright (c) 2014 by David Golden.
177              
178             This is free software, licensed under:
179              
180             The Apache License, Version 2.0, January 2004
181              
182             =cut