552a81ecf5904289d9b38eea211a5cf37d26a920
[dbsrgits/DBIx-Class.git] / xt / extra / internals / namespaces_cleaned.t
1 BEGIN {
2   if ( "$]" < 5.010) {
3
4     # Pre-5.10 perls pollute %INC on unsuccesfull module
5     # require, making it appear as if the module is already
6     # loaded on subsequent require()s
7     # Can't seem to find the exact RT/perldelta entry
8     #
9     # The reason we can't just use a sane, clean loader, is because
10     # if a Module require()s another module the %INC will still
11     # get filled with crap and we are back to square one. A global
12     # fix is really the only way for this test, as we try to load
13     # each available module separately, and have no control (nor
14     # knowledge) over their common dependencies.
15     #
16     # we want to do this here, in the very beginning, before even
17     # warnings/strict are loaded
18
19     unshift @INC, 't/lib';
20     require DBICTest::Util::OverrideRequire;
21
22     DBICTest::Util::OverrideRequire::override_global_require( sub {
23       my $res = eval { $_[0]->() };
24       if ($@ ne '') {
25         delete $INC{$_[1]};
26         die $@;
27       }
28       return $res;
29     } );
30   }
31 }
32
33 use strict;
34 use warnings;
35
36 use Test::More;
37
38 use lib 't/lib';
39
40 use DBICTest;
41 use File::Find;
42 use File::Spec;
43 use B qw/svref_2object/;
44 use Package::Stash;
45
46 # makes sure we can load at least something
47 use DBIx::Class;
48 use DBIx::Class::Carp;
49
50 my @modules = grep {
51   my ($mod) = $_ =~ /(.+)/;
52
53   # not all modules are loadable at all times
54   do {
55     # trap deprecation warnings and whatnot
56     local $SIG{__WARN__} = sub {};
57     eval "require $mod";
58   } ? $mod : do {
59     SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
60     (); # empty RV for @modules
61   };
62
63 } find_modules();
64
65 # have an exception table for old and/or weird code we are not sure
66 # we *want* to clean in the first place
67 my $skip_idx = { map { $_ => 1 } (
68   (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
69   'SQL::Translator::Producer::DBIx::Class::File', # ditto
70
71   # not sure how to handle type libraries
72   'DBIx::Class::Storage::DBI::Replicated::Types',
73   'DBIx::Class::Admin::Types',
74
75   # G::L::D is unclean, but we never inherit from it
76   'DBIx::Class::Admin::Descriptive',
77   'DBIx::Class::Admin::Usage',
78
79   # this subclass is expected to inherit whatever crap comes
80   # from the parent
81   'DBIx::Class::ResultSet::Pager',
82
83   # utility classes, not part of the inheritance chain
84   'DBIx::Class::Optional::Dependencies',
85   'DBIx::Class::ResultSource::RowParser::Util',
86   'DBIx::Class::_Util',
87 ) };
88
89 my $has_moose = eval { require Moose::Util };
90
91 Sub::Defer::undefer_all();
92
93 # can't use Class::Inspector for the mundane parts as it does not
94 # distinguish imports from anything else, what a crock of...
95 # Moose is not always available either - hence just do it ourselves
96
97 my $seen; #inheritance means we will see the same method multiple times
98
99 for my $mod (@modules) {
100   SKIP: {
101     skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
102
103     my %all_method_like = (map
104       { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
105       (reverse @{mro::get_linear_isa($mod)})
106     );
107
108     my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
109
110     my %roles;
111     if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
112       if ($mc->can('calculate_all_roles_with_inheritance')) {
113         $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
114       }
115     }
116
117     for my $name (keys %all_method_like) {
118
119       # overload is a funky thing - it is not cleaned, and its imports are named funny
120       next if $name =~ /^\(/;
121
122       my $gv = svref_2object($all_method_like{$name})->GV;
123       my $origin = $gv->STASH->NAME;
124
125       is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
126         ? ''
127         : " (inherited by $mod)"
128       ));
129
130       next if $seen->{"${origin}:${name}"}++;
131
132       if ($origin eq $mod) {
133         pass ("$name is a native $mod method");
134       }
135       elsif ($roles{$origin}) {
136         pass ("${mod}::${name} came from consumption of role $origin");
137       }
138       elsif ($parents{$origin}) {
139         pass ("${mod}::${name} came from proper parent-class $origin");
140       }
141       else {
142         my $via;
143         for (reverse @{mro::get_linear_isa($mod)} ) {
144           if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
145             $via = $_;
146             last;
147           }
148         }
149
150         # exception time
151         if (
152           ( $name eq 'import' and $via = 'Exporter' )
153         ) {
154           pass("${mod}::${name} is a valid uncleaned import from ${name}");
155         }
156         else {
157           fail ("${mod}::${name} appears to have entered inheritance chain by import into "
158               . ($via || 'UNKNOWN')
159           );
160         }
161       }
162     }
163
164     # some common import names (these should never ever be methods)
165     for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
166       if ($mod->can($f)) {
167         my $via;
168         for (reverse @{mro::get_linear_isa($mod)} ) {
169           if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
170             $via = $_;
171             last;
172           }
173         }
174         fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
175             . ($via || 'UNKNOWN')
176         );
177       }
178       else {
179         pass ("Import $f not leaked into method list of $mod");
180       }
181     }
182   }
183 }
184
185 sub find_modules {
186   my @modules;
187
188   find( {
189     wanted => sub {
190       -f $_ or return;
191       s/\.pm$// or return;
192       s/^ (?: lib | blib . (?:lib|arch) ) . //x;
193       push @modules, join ('::', File::Spec->splitdir($_));
194     },
195     no_chdir => 1,
196   }, (
197     # find them in both lib and blib, duplicates are fine, since
198     # @INC is preadjusted for us by the harness
199     'lib',
200     ( -e 'blib' ? 'blib' : () ),
201   ));
202
203   return sort @modules;
204 }
205
206 done_testing;