3f702628a5447955f764e2ae656ad3318f9b35ae
[dbsrgits/DBIx-Class.git] / xt / extra / internals / namespaces_cleaned.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 BEGIN {
4   if ( "$]" < 5.010) {
5
6     # Pre-5.10 perls pollute %INC on unsuccesfull module
7     # require, making it appear as if the module is already
8     # loaded on subsequent require()s
9     # Can't seem to find the exact RT/perldelta entry
10     #
11     # The reason we can't just use a sane, clean loader, is because
12     # if a Module require()s another module the %INC will still
13     # get filled with crap and we are back to square one. A global
14     # fix is really the only way for this test, as we try to load
15     # each available module separately, and have no control (nor
16     # knowledge) over their common dependencies.
17     #
18     # we want to do this here, in the very beginning, before even
19     # warnings/strict are loaded
20
21
22     require DBICTest::Util::OverrideRequire;
23
24     DBICTest::Util::OverrideRequire::override_global_require( sub {
25       my $res = eval { $_[0]->() };
26       if ($@ ne '') {
27         delete $INC{$_[1]};
28         die $@;
29       }
30       return $res;
31     } );
32   }
33 }
34
35 use strict;
36 use warnings;
37
38 use Test::More;
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 my $seen; #inheritance means we will see the same method multiple times
94
95 for my $mod (@modules) {
96   SKIP: {
97     skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
98
99     my %all_method_like = (map
100       { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
101       (reverse @{mro::get_linear_isa($mod)})
102     );
103
104     my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
105
106     my %roles;
107     if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
108       if ($mc->can('calculate_all_roles_with_inheritance')) {
109         $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
110       }
111     }
112
113     for my $name (keys %all_method_like) {
114
115       # overload is a funky thing - it is not cleaned, and its imports are named funny
116       next if $name =~ /^\(/;
117
118       my $gv = svref_2object($all_method_like{$name})->GV;
119       my $origin = $gv->STASH->NAME;
120
121       is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
122         ? ''
123         : " (inherited by $mod)"
124       ));
125
126       next if $seen->{"${origin}:${name}"}++;
127
128       if ($origin eq $mod) {
129         pass ("$name is a native $mod method");
130       }
131       elsif ($roles{$origin}) {
132         pass ("${mod}::${name} came from consumption of role $origin");
133       }
134       elsif ($parents{$origin}) {
135         pass ("${mod}::${name} came from proper parent-class $origin");
136       }
137       else {
138         my $via;
139         for (reverse @{mro::get_linear_isa($mod)} ) {
140           if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
141             $via = $_;
142             last;
143           }
144         }
145
146         # exception time
147         if (
148           ( $name eq 'import' and $via = 'Exporter' )
149         ) {
150           pass("${mod}::${name} is a valid uncleaned import from ${name}");
151         }
152         else {
153           fail ("${mod}::${name} appears to have entered inheritance chain by import into "
154               . ($via || 'UNKNOWN')
155           );
156         }
157       }
158     }
159
160     # some common import names (these should never ever be methods)
161     for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
162       if ($mod->can($f)) {
163         my $via;
164         for (reverse @{mro::get_linear_isa($mod)} ) {
165           if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
166             $via = $_;
167             last;
168           }
169         }
170         fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
171             . ($via || 'UNKNOWN')
172         );
173       }
174       else {
175         pass ("Import $f not leaked into method list of $mod");
176       }
177     }
178   }
179 }
180
181 sub find_modules {
182   my @modules;
183
184   find( {
185     wanted => sub {
186       -f $_ or return;
187       s/\.pm$// or return;
188       s/^ (?: lib | blib . (?:lib|arch) ) . //x;
189       push @modules, join ('::', File::Spec->splitdir($_));
190     },
191     no_chdir => 1,
192   }, (
193     # find them in both lib and blib, duplicates are fine, since
194     # @INC is preadjusted for us by the harness
195     'lib',
196     ( -e 'blib' ? 'blib' : () ),
197   ));
198
199   return sort @modules;
200 }
201
202 done_testing;