Remove use of Try::Tiny entirely (the missing part of ddcc02d1)
[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 DBIx::Class::_Util qw( get_subname describe_class_methods );
43
44 # makes sure we can load at least something
45 use DBIx::Class;
46 use DBIx::Class::Carp;
47
48 my @modules = grep {
49   my $mod = $_;
50
51   # not all modules are loadable at all times
52   do {
53     # trap deprecation warnings and whatnot
54     local $SIG{__WARN__} = sub {};
55     eval "require $mod";
56   } ? $mod : do {
57     SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
58     (); # empty RV for @modules
59   };
60
61 } find_modules();
62
63 # have an exception table for old and/or weird code we are not sure
64 # we *want* to clean in the first place
65 my $skip_idx = { map { $_ => 1 } (
66   'SQL::Translator::Producer::DBIx::Class::File', # too crufty to touch
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   # utility classes, not part of the inheritance chain
81   'DBIx::Class::Optional::Dependencies',
82   'DBIx::Class::ResultSource::RowParser::Util',
83   'DBIx::Class::SQLMaker::Util',
84   'DBIx::Class::_Util',
85 ) };
86
87 my $has_moose = eval { require Moose::Util };
88
89 my $seen; #inheritance means we will see the same method multiple times
90
91 for my $mod (@modules) {
92   SKIP: {
93     skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
94
95     my %all_method_like =
96       map
97         { $_->[0]{name} => $mod->can( $_->[0]{name} ) }
98         grep
99           { $_->[0]{via_class} ne 'UNIVERSAL' }
100           values %{ describe_class_methods($mod)->{methods} }
101     ;
102
103     my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
104
105     my %roles;
106     if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
107       if ($mc->can('calculate_all_roles_with_inheritance')) {
108         $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
109       }
110     }
111
112     for my $name (keys %all_method_like) {
113
114       # overload is a funky thing - it is not cleaned, and its imports are named funny
115       next if $name =~ /^\(/;
116
117       my ($origin, $cv_name) = get_subname($all_method_like{$name});
118
119       is ($cv_name, $name, "Properly named $name method at $origin" . ($origin eq $mod
120         ? ''
121         : " (inherited by $mod)"
122       ));
123
124       next if $seen->{"${origin}::${name}"}++;
125
126       if ($origin eq $mod) {
127         pass ("$name is a native $mod method");
128       }
129       elsif ($roles{$origin}) {
130         pass ("${mod}::${name} came from consumption of role $origin");
131       }
132       elsif ($parents{$origin}) {
133         pass ("${mod}::${name} came from proper parent-class $origin");
134       }
135       else {
136         my $via;
137         for (reverse @{mro::get_linear_isa($mod)} ) {
138           if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
139             $via = $_;
140             last;
141           }
142         }
143
144         # exception time
145         if (
146           ( $name eq 'import' and $via = 'Exporter' )
147             or
148           # jesus christ nobody had any idea how to design an interface back then
149           ( $name =~ /_trigger/ and $via = 'Class::Trigger' )
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(
163       carp carp_once carp_unique croak confess cluck
164       try catch finally dbic_internal_try dbic_internal_catch
165     )) {
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       $_ =~ m|lib/DBIx/Class/_TempExtlib| and return;
192       s/\.pm$// or return;
193       s/^ (?: lib | blib . (?:lib|arch) ) . //x;
194       s/[\/\\]/::/g;
195       push @modules, ( $_ =~ /(.+)/ );
196     },
197     no_chdir => 1,
198   }, (
199     # find them in both lib and blib, duplicates are fine, since
200     # @INC is preadjusted for us by the harness
201     'lib',
202     ( -e 'blib' ? 'blib' : () ),
203   ));
204
205   return sort @modules;
206 }
207
208 done_testing;