4caddf898948dacaa5f3c8c94e1ed0c8d84e8837
[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 use Test::More;
37
38 use File::Find;
39 use File::Spec;
40 use B qw/svref_2object/;
41 use Package::Stash;
42
43 # makes sure we can load at least something
44 use DBIx::Class;
45 use DBIx::Class::Carp;
46
47 my @modules = grep {
48   my $mod = $_;
49
50   # not all modules are loadable at all times
51   do {
52     # trap deprecation warnings and whatnot
53     local $SIG{__WARN__} = sub {};
54     eval "require $mod";
55   } ? $mod : do {
56     SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
57     (); # empty RV for @modules
58   };
59
60 } find_modules();
61
62 # have an exception table for old and/or weird code we are not sure
63 # we *want* to clean in the first place
64 my $skip_idx = { map { $_ => 1 } (
65   (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
66   'SQL::Translator::Producer::DBIx::Class::File', # ditto
67
68   # not sure how to handle type libraries
69   'DBIx::Class::Storage::DBI::Replicated::Types',
70   'DBIx::Class::Admin::Types',
71
72   # G::L::D is unclean, but we never inherit from it
73   'DBIx::Class::Admin::Descriptive',
74   'DBIx::Class::Admin::Usage',
75
76   # this subclass is expected to inherit whatever crap comes
77   # from the parent
78   'DBIx::Class::ResultSet::Pager',
79
80   # this is not part of the inheritance tree (plus is a temporary fix anyway)
81   'DBIx::Class::GlobalDestruction',
82
83   # Moo does not name its generated methods, fix pending
84   'DBIx::Class::Storage::BlockRunner',
85 ) };
86
87 my $has_cmop = eval { require Class::MOP };
88
89 # can't use Class::Inspector for the mundane parts as it does not
90 # distinguish imports from anything else, what a crock of...
91 # Class::MOP is not always available either - hence just do it ourselves
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_cmop and my $mc = Class::MOP::class_of($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       next if ( DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN() and $name =~ /^carp(?:_unique|_once)?$/ );
116
117       # overload is a funky thing - it is not cleaned, and its imports are named funny
118       next if $name =~ /^\(/;
119
120       my $gv = svref_2object($all_method_like{$name})->GV;
121       my $origin = $gv->STASH->NAME;
122
123       TODO: {
124         local $TODO;
125         if ($name =~ /^__CAG_/) {
126           $TODO = 'CAG does not clean its BEGIN constants';
127         }
128
129         is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
130           ? ''
131           : " (inherited by $mod)"
132         ));
133       }
134
135       next if $seen->{"${origin}:${name}"}++;
136
137       if ($origin eq $mod) {
138         pass ("$name is a native $mod method");
139       }
140       elsif ($roles{$origin}) {
141         pass ("${mod}::${name} came from consumption of role $origin");
142       }
143       elsif ($parents{$origin}) {
144         pass ("${mod}::${name} came from proper parent-class $origin");
145       }
146       else {
147         my $via;
148         for (reverse @{mro::get_linear_isa($mod)} ) {
149           if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
150             $via = $_;
151             last;
152           }
153         }
154         fail ("${mod}::${name} appears to have entered inheritance chain by import into "
155             . ($via || 'UNKNOWN')
156         );
157       }
158     }
159
160     next if DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN();
161
162     # some common import names (these should never ever be methods)
163     for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
164       if ($mod->can($f)) {
165         my $via;
166         for (reverse @{mro::get_linear_isa($mod)} ) {
167           if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
168             $via = $_;
169             last;
170           }
171         }
172         fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
173             . ($via || 'UNKNOWN')
174         );
175       }
176       else {
177         pass ("Import $f not leaked into method list of $mod");
178       }
179     }
180   }
181 }
182
183 sub find_modules {
184   my @modules;
185
186   find({
187     wanted => sub {
188       -f $_ or return;
189       s/\.pm$// or return;
190       s/^ (?: lib | blib . (?:lib|arch) ) . //x;
191       push @modules, join ('::', File::Spec->splitdir($_));
192     },
193     no_chdir => 1,
194   }, (-e 'blib' ? 'blib' : 'lib') );
195
196   return sort @modules;
197 }
198
199 done_testing;