Port ::Replicated from Moose to Moo
[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   # G::L::D is unclean, but we never inherit from it
72   'DBIx::Class::Admin::Descriptive',
73   'DBIx::Class::Admin::Usage',
74
75   # this subclass is expected to inherit whatever crap comes
76   # from the parent
77   'DBIx::Class::ResultSet::Pager',
78
79   # utility classes, not part of the inheritance chain
80   'DBIx::Class::Optional::Dependencies',
81   'DBIx::Class::ResultSource::RowParser::Util',
82   'DBIx::Class::_Util',
83   'DBIx::Class::_Types',
84 ) };
85
86 my $has_moose = eval { require Moose::Util };
87
88 Sub::Defer::undefer_all();
89
90 # can't use Class::Inspector for the mundane parts as it does not
91 # distinguish imports from anything else, what a crock of...
92 # Moose is not always available either - hence just do it ourselves
93
94 my $seen; #inheritance means we will see the same method multiple times
95
96 for my $mod (@modules) {
97   SKIP: {
98     skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
99
100     my %all_method_like = (map
101       { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
102       (reverse @{mro::get_linear_isa($mod)})
103     );
104
105     my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
106
107     my %roles;
108     if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
109       if ($mc->can('calculate_all_roles_with_inheritance')) {
110         $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
111       }
112     }
113
114     for my $name (keys %all_method_like) {
115
116       # overload is a funky thing - it is not cleaned, and its imports are named funny
117       next if $name =~ /^\(/;
118
119       my $gv = svref_2object($all_method_like{$name})->GV;
120       my $origin = $gv->STASH->NAME;
121
122       is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
123         ? ''
124         : " (inherited by $mod)"
125       ));
126
127       next if $seen->{"${origin}:${name}"}++;
128
129       if ($origin eq $mod) {
130         pass ("$name is a native $mod method");
131       }
132       elsif ($roles{$origin}) {
133         pass ("${mod}::${name} came from consumption of role $origin");
134       }
135       elsif ($parents{$origin}) {
136         pass ("${mod}::${name} came from proper parent-class $origin");
137       }
138       else {
139         my $via;
140         for (reverse @{mro::get_linear_isa($mod)} ) {
141           if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
142             $via = $_;
143             last;
144           }
145         }
146
147         # exception time
148         if (
149           ( $name eq 'import' and $via = 'Exporter' )
150         ) {
151           pass("${mod}::${name} is a valid uncleaned import from ${name}");
152         }
153         else {
154           fail ("${mod}::${name} appears to have entered inheritance chain by import into "
155               . ($via || 'UNKNOWN')
156           );
157         }
158       }
159     }
160
161     # some common import names (these should never ever be methods)
162     for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
163       if ($mod->can($f)) {
164         my $via;
165         for (reverse @{mro::get_linear_isa($mod)} ) {
166           if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
167             $via = $_;
168             last;
169           }
170         }
171         fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
172             . ($via || 'UNKNOWN')
173         );
174       }
175       else {
176         pass ("Import $f not leaked into method list of $mod");
177       }
178     }
179   }
180 }
181
182 sub find_modules {
183   my @modules;
184
185   find( {
186     wanted => sub {
187       -f $_ or return;
188       s/\.pm$// or return;
189       s/^ (?: lib | blib . (?:lib|arch) ) . //x;
190       push @modules, join ('::', File::Spec->splitdir($_));
191     },
192     no_chdir => 1,
193   }, (
194     # find them in both lib and blib, duplicates are fine, since
195     # @INC is preadjusted for us by the harness
196     'lib',
197     ( -e 'blib' ? 'blib' : () ),
198   ));
199
200   return sort @modules;
201 }
202
203 done_testing;