Apply debian downstream spelling patch 481e21bf
[dbsrgits/DBIx-Class.git] / t / 55namespaces_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 # FIXME This is a crock of shit, needs to go away
37 # currently here to work around https://rt.cpan.org/Ticket/Display.html?id=74151
38 # kill with fire when PS::XS / RT#74151 is *finally* fixed
39 BEGIN {
40   my $PS_provider;
41
42   if ( "$]" < 5.010 ) {
43     require Package::Stash::PP;
44     $PS_provider = 'Package::Stash::PP';
45   }
46   else {
47     require Package::Stash;
48     $PS_provider = 'Package::Stash';
49   }
50   eval <<"EOS" or die $@;
51
52 sub stash_for (\$) {
53   $PS_provider->new(\$_[0]);
54 }
55 1;
56 EOS
57 }
58
59 use Test::More;
60
61 use lib 't/lib';
62
63 BEGIN {
64   require DBICTest::RunMode;
65   plan( skip_all => "Skipping test on plain module install" )
66     if DBICTest::RunMode->is_plain;
67 }
68
69 use DBICTest;
70 use File::Find;
71 use File::Spec;
72 use B qw/svref_2object/;
73
74 # makes sure we can load at least something
75 use DBIx::Class;
76 use DBIx::Class::Carp;
77
78 my @modules = grep {
79   my ($mod) = $_ =~ /(.+)/;
80
81   # not all modules are loadable at all times
82   do {
83     # trap deprecation warnings and whatnot
84     local $SIG{__WARN__} = sub {};
85     eval "require $mod";
86   } ? $mod : do {
87     SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
88     (); # empty RV for @modules
89   };
90
91 } find_modules();
92
93 # have an exception table for old and/or weird code we are not sure
94 # we *want* to clean in the first place
95 my $skip_idx = { map { $_ => 1 } (
96   (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
97   'SQL::Translator::Producer::DBIx::Class::File', # ditto
98
99   # not sure how to handle type libraries
100   'DBIx::Class::Storage::DBI::Replicated::Types',
101   'DBIx::Class::Admin::Types',
102
103   # G::L::D is unclean, but we never inherit from it
104   'DBIx::Class::Admin::Descriptive',
105   'DBIx::Class::Admin::Usage',
106
107   # this subclass is expected to inherit whatever crap comes
108   # from the parent
109   'DBIx::Class::ResultSet::Pager',
110
111   # utility classes, not part of the inheritance chain
112   'DBIx::Class::ResultSource::RowParser::Util',
113   'DBIx::Class::_Util',
114 ) };
115
116 my $has_moose = eval { require Moose::Util };
117
118 Sub::Defer::undefer_all();
119
120 # can't use Class::Inspector for the mundane parts as it does not
121 # distinguish imports from anything else, what a crock of...
122 # Moose is not always available either - hence just do it ourselves
123
124 my $seen; #inheritance means we will see the same method multiple times
125
126 for my $mod (@modules) {
127   SKIP: {
128     skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
129
130     my %all_method_like = (map
131       { %{stash_for($_)->get_all_symbols('CODE')} }
132       (reverse @{mro::get_linear_isa($mod)})
133     );
134
135     my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
136
137     my %roles;
138     if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
139       if ($mc->can('calculate_all_roles_with_inheritance')) {
140         $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
141       }
142     }
143
144     for my $name (keys %all_method_like) {
145
146       # overload is a funky thing - it is not cleaned, and its imports are named funny
147       next if $name =~ /^\(/;
148
149       my $gv = svref_2object($all_method_like{$name})->GV;
150       my $origin = $gv->STASH->NAME;
151
152       is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
153         ? ''
154         : " (inherited by $mod)"
155       ));
156
157       next if $seen->{"${origin}:${name}"}++;
158
159       if ($origin eq $mod) {
160         pass ("$name is a native $mod method");
161       }
162       elsif ($roles{$origin}) {
163         pass ("${mod}::${name} came from consumption of role $origin");
164       }
165       elsif ($parents{$origin}) {
166         pass ("${mod}::${name} came from proper parent-class $origin");
167       }
168       else {
169         my $via;
170         for (reverse @{mro::get_linear_isa($mod)} ) {
171           if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
172             $via = $_;
173             last;
174           }
175         }
176
177         # exception time
178         if (
179           ( $name eq 'import' and $via = 'Exporter' )
180         ) {
181           pass("${mod}::${name} is a valid uncleaned import from ${name}");
182         }
183         else {
184           fail ("${mod}::${name} appears to have entered inheritance chain by import into "
185               . ($via || 'UNKNOWN')
186           );
187         }
188       }
189     }
190
191     # some common import names (these should never ever be methods)
192     for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
193       if ($mod->can($f)) {
194         my $via;
195         for (reverse @{mro::get_linear_isa($mod)} ) {
196           if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
197             $via = $_;
198             last;
199           }
200         }
201         fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
202             . ($via || 'UNKNOWN')
203         );
204       }
205       else {
206         pass ("Import $f not leaked into method list of $mod");
207       }
208     }
209   }
210 }
211
212 sub find_modules {
213   my @modules;
214
215   find( {
216     wanted => sub {
217       -f $_ or return;
218       s/\.pm$// or return;
219       s/^ (?: lib | blib . (?:lib|arch) ) . //x;
220       push @modules, join ('::', File::Spec->splitdir($_));
221     },
222     no_chdir => 1,
223   }, (
224     # find them in both lib and blib, duplicates are fine, since
225     # @INC is preadjusted for us by the harness
226     'lib',
227     ( -e 'blib' ? 'blib' : () ),
228   ));
229
230   return sort @modules;
231 }
232
233 done_testing;