Stop Data::Compare from loading plugins at will
[dbsrgits/DBIx-Class.git] / t / 55namespaces_cleaned.t
CommitLineData
88b1530a 1# Pre-5.10 perls pollute %INC on unsuccesfull module
2# require, making it appear as if the module is already
3# loaded on subsequent require()s
4# Can't seem to find the exact RT/perldelta entry
5BEGIN {
6 if ($] < 5.010) {
7 # shut up spurious warnings without loading warnings.pm
8 *CORE::GLOBAL::require = sub {};
9
10 *CORE::GLOBAL::require = sub {
11 my $res = eval { CORE::require($_[0]) };
12 if ($@) {
13 delete $INC{$_[0]};
14 die
15 }
16 $res;
17 }
18 }
19}
20
9c1700e3 21use strict;
22use warnings;
23
24use Test::More;
25
26use File::Find;
27use File::Spec;
28use B qw/svref_2object/;
90cfe42b 29use Package::Stash;
9c1700e3 30
31# makes sure we can load at least something
32use DBIx::Class;
90cfe42b 33use DBIx::Class::Carp;
9c1700e3 34
35my @modules = grep {
36 my $mod = $_;
37
9c1700e3 38 # not all modules are loadable at all times
88b1530a 39 do {
40 # trap deprecation warnings and whatnot
41 local $SIG{__WARN__} = sub {};
42 eval "require $mod";
43 } ? $mod : do {
44 SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
45 (); # empty RV for @modules
9c1700e3 46 };
47
9c1700e3 48} find_modules();
49
50# have an exception table for old and/or weird code we are not sure
51# we *want* to clean in the first place
52my $skip_idx = { map { $_ => 1 } (
53 (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
54 'SQL::Translator::Producer::DBIx::Class::File', # ditto
55
56 # not sure how to handle type libraries
57 'DBIx::Class::Storage::DBI::Replicated::Types',
58 'DBIx::Class::Admin::Types',
59
60 # G::L::D is unclean, but we never inherit from it
61 'DBIx::Class::Admin::Descriptive',
62 'DBIx::Class::Admin::Usage',
63) };
64
65my $has_cmop = eval { require Class::MOP };
66
67# can't use Class::Inspector for the mundane parts as it does not
68# distinguish imports from anything else, what a crock of...
69# Class::MOP is not always available either - hence just do it ourselves
70
71my $seen; #inheritance means we will see the same method multiple times
72
73for my $mod (@modules) {
74 SKIP: {
75 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
76
90cfe42b 77 my %all_method_like = (map
78 { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
79 (reverse @{mro::get_linear_isa($mod)})
80 );
9c1700e3 81
82 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
83
84 my %roles;
85 if ($has_cmop and my $mc = Class::MOP::class_of($mod)) {
86 if ($mc->can('calculate_all_roles_with_inheritance')) {
87 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
88 }
89 }
90
91 for my $name (keys %all_method_like) {
92
e0b2dc74 93 next if ( DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN() and $name =~ /^carp(?:_unique|_once)?$/ );
90cfe42b 94
9c1700e3 95 # overload is a funky thing - it is neither cleaned, and its imports are named funny
96 next if $name =~ /^\(/;
97
98 my $gv = svref_2object($all_method_like{$name})->GV;
99 my $origin = $gv->STASH->NAME;
100
9c1700e3 101 TODO: {
102 local $TODO = 'CAG does not clean its BEGIN constants' if $name =~ /^__CAG_/;
70c28808 103 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
104 ? ''
105 : " (inherited by $mod)"
106 ));
9c1700e3 107 }
108
70c28808 109 next if $seen->{"${origin}:${name}"}++;
110
9c1700e3 111 if ($origin eq $mod) {
112 pass ("$name is a native $mod method");
113 }
114 elsif ($roles{$origin}) {
115 pass ("${mod}::${name} came from consumption of role $origin");
116 }
117 elsif ($parents{$origin}) {
118 pass ("${mod}::${name} came from proper parent-class $origin");
119 }
120 else {
121 my $via;
122 for (reverse @{mro::get_linear_isa($mod)} ) {
123 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
124 $via = $_;
125 last;
126 }
127 }
128 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
129 . ($via || 'UNKNOWN')
130 );
131 }
132 }
70c28808 133
e0b2dc74 134 next if DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN();
90cfe42b 135
70c28808 136 # some common import names (these should never ever be methods)
137 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
138 if ($mod->can($f)) {
139 my $via;
140 for (reverse @{mro::get_linear_isa($mod)} ) {
141 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
142 $via = $_;
143 last;
144 }
145 }
146 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
147 . ($via || 'UNKNOWN')
148 );
149 }
150 else {
151 pass ("Import $f not leaked into method list of $mod");
152 }
153 }
9c1700e3 154 }
155}
156
157sub find_modules {
158 my @modules;
159
160 find({
161 wanted => sub {
162 -f $_ or return;
163 s/\.pm$// or return;
164 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
165 push @modules, join ('::', File::Spec->splitdir($_));
166 },
167 no_chdir => 1,
168 }, (-e 'blib' ? 'blib' : 'lib') );
169
170 return sort @modules;
171}
172
173
174done_testing;