/usr/share/perl5/Test/CleanNamespaces.pm is in libtest-cleannamespaces-perl 0.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | use strict;
use warnings;
package Test::CleanNamespaces; # git description: v0.17-1-gc38f0ce
# ABSTRACT: Check for uncleaned imports
# KEYWORDS: testing namespaces clean dirty imports exports subroutines methods
$Test::CleanNamespaces::VERSION = '0.18';
use Module::Runtime qw(require_module module_notional_filename);
use Sub::Identify qw(sub_fullname stash_name);
use Package::Stash 0.14;
use Test::Builder;
use File::Find::Rule;
use File::Find::Rule::Perl;
use File::Spec::Functions 'splitdir';
use namespace::clean;
use Sub::Exporter -setup => {
exports => [
namespaces_clean => \&build_namespaces_clean,
all_namespaces_clean => \&build_all_namespaces_clean,
],
groups => {
default => [qw/namespaces_clean all_namespaces_clean/],
},
};
#pod =head1 SYNOPSIS
#pod
#pod use strict;
#pod use warnings;
#pod use Test::CleanNamespaces;
#pod
#pod all_namespaces_clean;
#pod
#pod =head1 DESCRIPTION
#pod
#pod This module lets you check your module's namespaces for imported functions you
#pod might have forgotten to remove with L<namespace::autoclean> or
#pod L<namespace::clean> and are therefore available to be called as methods, which
#pod usually isn't want you want.
#pod
#pod =head1 FUNCTIONS
#pod
#pod All functions are exported by default.
#pod
#pod =head2 namespaces_clean
#pod
#pod namespaces_clean('YourModule', 'AnotherModule');
#pod
#pod Tests every specified namespace for uncleaned imports. If the module couldn't
#pod be loaded it will be skipped.
#pod
#pod =head2 all_namespaces_clean
#pod
#pod all_namespaces_clean;
#pod
#pod Runs L</namespaces_clean> for all modules in your distribution.
#pod
#pod =head1 METHODS
#pod
#pod The exported functions are constructed using the the following methods. This is
#pod what you want to override if you're subclassing this module.
#pod
#pod =head2 build_namespaces_clean
#pod
#pod my $coderef = Test::CleanNamespaces->build_namespaces_clean;
#pod
#pod Returns a coderef that will be exported as C<namespaces_clean> (or the
#pod specified sub name, if provided).
#pod
#pod =cut
sub build_namespaces_clean {
my ($class, $name) = @_;
return sub {
my (@namespaces) = @_;
local $@;
my $result = 1;
for my $ns (@namespaces) {
unless (eval { require_module($ns); 1 }) {
$class->builder->skip("failed to load ${ns}: $@");
next;
}
my $imports = _remaining_imports($ns);
my $ok = $class->builder->ok(!keys(%$imports), "${ns} contains no imported functions");
$ok or $class->builder->diag($class->builder->explain('remaining imports: ' => $imports));
$result &&= $ok;
}
return $result;
};
}
#pod =head2 build_all_namespaces_clean
#pod
#pod my $coderef = Test::CleanNamespaces->build_all_namespaces_clean;
#pod
#pod Returns a coderef that will be exported as C<all_namespaces_clean>.
#pod (or the specified sub name, if provided).
#pod It will use
#pod the C<find_modules> method to get the list of modules to check.
#pod
#pod =cut
sub build_all_namespaces_clean {
my ($class, $name) = @_;
my $namespaces_clean = $class->build_namespaces_clean();
return sub {
my @modules = $class->find_modules(@_);
$class->builder->plan(tests => scalar @modules);
$namespaces_clean->(@modules);
};
}
# given a package name, returns a hashref of all remaining imports
sub _remaining_imports {
my $ns = shift;
my $symbols = Package::Stash->new($ns)->get_all_symbols('CODE');
my @imports;
my $meta;
if ($INC{ module_notional_filename('Class::MOP') }
and $meta = Class::MOP::class_of($ns)
and $meta->can('get_method_list'))
{
my %subs = %$symbols;
delete @subs{ $meta->get_method_list };
@imports = keys %subs;
}
elsif ($INC{ module_notional_filename('Mouse::Util') }
and Mouse::Util->can('class_of') and $meta = Mouse::Util::class_of($ns))
{
warn 'Mouse class detected - chance of false negatives is high!';
my %subs = %$symbols;
# ugh, this returns far more than the true list of methods
delete @subs{ $meta->get_method_list };
@imports = keys %subs;
}
else
{
@imports = grep {
my $stash = stash_name($symbols->{$_});
$stash ne $ns
and $stash ne 'Role::Tiny'
and not eval { require Role::Tiny; Role::Tiny->is_role($stash) }
} keys %$symbols;
}
my %imports; @imports{@imports} = map { sub_fullname($symbols->{$_}) } @imports;
# these subs are special-cased - they are often provided by other
# modules, but cannot be wrapped with Sub::Name as the call stack
# is important
delete @imports{qw(import unimport)};
my @overloads = grep { $imports{$_} eq 'overload::nil' || $imports{$_} eq 'overload::_nil' } keys %imports;
delete @imports{@overloads} if @overloads;
if ($] < 5.010)
{
my @constants = grep { $imports{$_} eq 'constant::__ANON__' } keys %imports;
delete @imports{@constants} if @constants;
}
return \%imports;
}
#pod =head2 find_modules
#pod
#pod my @modules = Test::CleanNamespaces->find_modules;
#pod
#pod Returns a list of modules in the current distribution. It'll search in
#pod C<blib/>, if it exists. C<lib/> will be searched otherwise.
#pod
#pod =cut
sub find_modules {
my ($class) = @_;
my @modules = map {
/^blib/
? s/^blib.(?:lib|arch).//
: s/^lib.//;
s/\.pm$//;
join '::' => splitdir($_);
} File::Find::Rule->perl_module->in(-e 'blib' ? 'blib' : 'lib');
return @modules;
}
#pod =head2 builder
#pod
#pod my $builder = Test::CleanNamespaces->builder;
#pod
#pod Returns the C<Test::Builder> used by the test functions.
#pod
#pod =cut
{
my $Test = Test::Builder->new;
sub builder { $Test }
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test::CleanNamespaces - Check for uncleaned imports
=head1 VERSION
version 0.18
=head1 SYNOPSIS
use strict;
use warnings;
use Test::CleanNamespaces;
all_namespaces_clean;
=head1 DESCRIPTION
This module lets you check your module's namespaces for imported functions you
might have forgotten to remove with L<namespace::autoclean> or
L<namespace::clean> and are therefore available to be called as methods, which
usually isn't want you want.
=head1 FUNCTIONS
All functions are exported by default.
=head2 namespaces_clean
namespaces_clean('YourModule', 'AnotherModule');
Tests every specified namespace for uncleaned imports. If the module couldn't
be loaded it will be skipped.
=head2 all_namespaces_clean
all_namespaces_clean;
Runs L</namespaces_clean> for all modules in your distribution.
=head1 METHODS
The exported functions are constructed using the the following methods. This is
what you want to override if you're subclassing this module.
=head2 build_namespaces_clean
my $coderef = Test::CleanNamespaces->build_namespaces_clean;
Returns a coderef that will be exported as C<namespaces_clean> (or the
specified sub name, if provided).
=head2 build_all_namespaces_clean
my $coderef = Test::CleanNamespaces->build_all_namespaces_clean;
Returns a coderef that will be exported as C<all_namespaces_clean>.
(or the specified sub name, if provided).
It will use
the C<find_modules> method to get the list of modules to check.
=head2 find_modules
my @modules = Test::CleanNamespaces->find_modules;
Returns a list of modules in the current distribution. It'll search in
C<blib/>, if it exists. C<lib/> will be searched otherwise.
=head2 builder
my $builder = Test::CleanNamespaces->builder;
Returns the C<Test::Builder> used by the test functions.
=head1 KNOWN ISSUES
Uncleaned imports from L<Mouse> classes are incompletely detected, due to its
lack of ability to return the correct method list -- it assumes that all subs
are meant to be callable as methods unless they originated from (were imported
by) one of: L<Mouse>, L<Mouse::Role>, L<Mouse::Util>,
L<Mouse::Util::TypeConstraints>, L<Carp>, L<Scalar::Util>, or L<List::Util>.
=head1 SEE ALSO
=over 4
=item *
L<namespace::clean>
=item *
L<namespace::autoclean>
=item *
L<namespace::sweep>
=item *
L<Sub::Exporter::ForMethods>
=item *
L<Test::API>
=item *
L<Sub::Name>
=item *
L<Sub::Install>
=item *
L<MooseX::MarkAsMethods>
=item *
L<Dist::Zilla::Plugin::Test::CleanNamespaces>
=back
=head1 AUTHOR
Florian Ragwitz <rafl@debian.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2009 by Florian Ragwitz.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 CONTRIBUTOR
=for stopwords Karen Etheridge
Karen Etheridge <ether@cpan.org>
=cut
|