File Coverage

blib/lib/Dist/Zilla/Plugin/Test/CleanNamespaces.pm
Criterion Covered Total %
statement 34 34 100.0
branch 3 4 75.0
condition n/a
subroutine 13 13 100.0
pod 0 5 0.0
total 50 56 89.2


line stmt bran cond sub pod time code
1 4     4   2680225 use strict;
  4         9  
  4         158  
2 4     4   16 use warnings;
  4         5  
  4         227  
3             package Dist::Zilla::Plugin::Test::CleanNamespaces;
4             # git description: v0.005-2-g77ab320
5             $Dist::Zilla::Plugin::Test::CleanNamespaces::VERSION = '0.006';
6             # ABSTRACT: Generate a test to check that all namespaces are clean
7             # KEYWORDS: plugin testing namespaces clean dirty imports exports subroutines methods
8             # vim: set ts=8 sw=4 tw=78 et :
9              
10 4     4   506 use Moose;
  4         336455  
  4         36  
11             with (
12             'Dist::Zilla::Role::FileGatherer',
13             'Dist::Zilla::Role::FileMunger',
14             'Dist::Zilla::Role::TextTemplate',
15             'Dist::Zilla::Role::PrereqSource',
16             );
17 4     4   23966 use MooseX::Types::Stringlike 'Stringlike';
  4         181320  
  4         30  
18 4     4   5321 use Moose::Util::TypeConstraints 'role_type';
  4         10  
  4         28  
19 4     4   2388 use Path::Tiny;
  4         10090  
  4         253  
20 4     4   21 use namespace::autoclean;
  4         6  
  4         34  
21              
22 3     3 0 1626 sub mvp_multivalue_args { qw(skips) }
23 3     3 0 446 sub mvp_aliases { return { skip => 'skips' } }
24              
25             has skips => (
26             isa => 'ArrayRef[Str]',
27             traits => ['Array'],
28             handles => { skips => 'elements' },
29             lazy => 1,
30             default => sub { [] },
31             );
32              
33             has filename => (
34             is => 'ro', isa => Stringlike,
35             coerce => 1,
36             lazy => 1,
37             default => sub { path('xt', 'author', 'clean-namespaces.t') },
38             );
39              
40 7     7   32682 sub _tcn_prereq { '0.15' }
41              
42             around dump_config => sub
43             {
44             my ($orig, $self) = @_;
45             my $config = $self->$orig;
46              
47             $config->{+__PACKAGE__} = {
48             skips => [ $self->skips ],
49             filename => $self->filename,
50             };
51              
52             return $config;
53             };
54              
55             sub register_prereqs
56             {
57 3     3 0 980 my $self = shift;
58              
59 3 50       118 $self->zilla->register_prereqs(
60             {
61             type => 'requires',
62             phase => $self->filename =~ /^t/ ? 'test' : 'develop',
63             },
64             'Test::CleanNamespaces' => $self->_tcn_prereq,
65             );
66             }
67              
68             has _file => (
69             is => 'rw', isa => role_type('Dist::Zilla::Role::File'),
70             );
71              
72             sub gather_files
73             {
74 3     3 0 170714 my $self = shift;
75              
76 3         1900 require Dist::Zilla::File::InMemory;
77 3         215687 $self->add_file( $self->_file(
78             Dist::Zilla::File::InMemory->new(
79             name => $self->filename,
80             content => <<'TEST',
81             use strict;
82             use warnings;
83              
84             # this test was generated with {{ ref($plugin) . ' ' . ($plugin->VERSION || '<self>') }}
85              
86             use Test::More 0.94;
87             use Test::CleanNamespaces {{ $tcn_prereq }};
88              
89             subtest all_namespaces_clean => sub {{
90             $skips
91             ? "{\n namespaces_clean(
92             " . 'grep { my $mod = $_; not grep { $mod =~ $_ } ' . $skips . " }
93             Test::CleanNamespaces->find_modules\n );\n};"
94             : '{ all_namespaces_clean() };'
95             }}
96              
97             done_testing;
98             TEST
99             ))
100             );
101             }
102              
103             sub munge_file
104             {
105 11     11 0 7966 my ($self, $file) = @_;
106              
107 11 100       407 return unless $file == $self->_file;
108              
109 2         14 $file->content(
110             $self->fill_in_string(
111             $file->content,
112             {
113             dist => \($self->zilla),
114             plugin => \$self,
115 3         13 skips => \( join(', ', map { 'qr/' . $_ . '/' } $self->skips) ),
116             tcn_prereq => \($self->_tcn_prereq),
117             }
118             )
119             );
120              
121 3         4878 return;
122             }
123              
124             __PACKAGE__->meta->make_immutable;
125              
126             __END__
127              
128             =pod
129              
130             =encoding UTF-8
131              
132             =head1 NAME
133              
134             Dist::Zilla::Plugin::Test::CleanNamespaces - Generate a test to check that all namespaces are clean
135              
136             =head1 VERSION
137              
138             version 0.006
139              
140             =head1 SYNOPSIS
141              
142             In your F<dist.ini>:
143              
144             [Test::CleanNamespaces]
145             skip = ::Dirty$
146              
147             =head1 DESCRIPTION
148              
149             This is a L<Dist::Zilla> plugin that runs at the
150             L<gather files|Dist::Zilla::Role::FileGatherer> stage, providing a test file
151             (configurable, defaulting to F<xt/author/clean-namespaces.t>).
152              
153             This test will scan all modules in your distribution and check that their
154             namespaces are "clean" -- that is, that there are no remaining imported
155             subroutines from other modules that are now callable as methods at runtime.
156              
157             You can fix this in your code with L<namespace::clean> or
158             L<namespace::autoclean>.
159              
160             =for Pod::Coverage mvp_multivalue_args mvp_aliases register_prereqs gather_files munge_file
161              
162             =head1 CONFIGURATION OPTIONS
163              
164             =head2 filename
165              
166             The name of the generated test. Defaults to F<xt/author/clean-namespaces.t>.
167              
168             =head2 skip
169              
170             A regular expression describing a module name that should not be checked. Can
171             be used more than once.
172              
173             =head1 TO DO (or: POSSIBLE FEATURES COMING IN FUTURE RELEASES)
174              
175             =for stopwords FileFinder
176              
177             =over 4
178              
179             =item *
180              
181             use of a configurable L<FileFinder|Dist::Zilla::Role::FileFinder> for finding
182              
183             source files to check (depends on changes planned in L<Test::CleanNamespaces>)
184              
185             =back
186              
187             =head1 SUPPORT
188              
189             =for stopwords irc
190              
191             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-Test-CleanNamespaces>
192             (or L<bug-Dist-Zilla-Plugin-Test-CleanNamespaces@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-Test-CleanNamespaces@rt.cpan.org>).
193             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
194              
195             =head1 SEE ALSO
196              
197             =over 4
198              
199             =item *
200              
201             L<Test::CleanNamespaces>
202              
203             =item *
204              
205             L<namespace::clean>
206              
207             =item *
208              
209             L<namespace::autoclean>
210              
211             =item *
212              
213             L<namespace::sweep>
214              
215             =item *
216              
217             L<Sub::Exporter::ForMethods>
218              
219             =item *
220              
221             L<Sub::Name>
222              
223             =item *
224              
225             L<Sub::Install>
226              
227             =item *
228              
229             L<MooseX::MarkAsMethods>
230              
231             =back
232              
233             =head1 AUTHOR
234              
235             Karen Etheridge <ether@cpan.org>
236              
237             =head1 COPYRIGHT AND LICENSE
238              
239             This software is copyright (c) 2014 by Karen Etheridge.
240              
241             This is free software; you can redistribute it and/or modify it under
242             the same terms as the Perl 5 programming language system itself.
243              
244             =cut