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