More robust test suite skip-checks, check ENV before loading modules
[dbsrgits/DBIx-Class.git] / t / 55namespaces_cleaned.t
CommitLineData
88b1530a 1# Pre-5.10 perls pollute %INC on unsuccesfull module
2# require, making it appear as if the module is already
3# loaded on subsequent require()s
4# Can't seem to find the exact RT/perldelta entry
6a0067ea 5#
6# we want to do this here, in the very beginning, before even
7# warnings/strict are loaded
88b1530a 8BEGIN {
9 if ($] < 5.010) {
6a0067ea 10
11 # All of this almost verbatim copied from Lexical::SealRequireHints
12 # Zefram++
13
14 # a potential caller() in $next_require must see the correct
15 # immediate frame caller
16 my $caller = caller(0);
17
18 our $next_require = defined(&CORE::GLOBAL::require)
19 ? \&CORE::GLOBAL::require
20 : sub {
21 my ($arg) = @_;
22
23 # The shenanigans with $CORE::GLOBAL::{require}
24 # are required because if there's a
25 # &CORE::GLOBAL::require when the eval is
26 # executed then the CORE::require in there is
27 # interpreted as plain require on some Perl
28 # versions, leading to recursion.
29 my $grequire = delete $CORE::GLOBAL::{require};
30
31 my $result = eval sprintf '
32 local $SIG{__DIE__};
33 $CORE::GLOBAL::{require} = $grequire;
34 package %s;
35 CORE::require($arg);
36 ', $caller;
37
38 die $@ if $@ ne '';
39 return $result;
40 }
41 ;
88b1530a 42
43 *CORE::GLOBAL::require = sub {
6a0067ea 44 die "wrong number of arguments to require\n"
45 unless @_ == 1;
46
47 my $res = eval "package $caller; \$next_require->(\@_)";
48 if ($@ ne '') {
88b1530a 49 delete $INC{$_[0]};
6a0067ea 50 die $@;
88b1530a 51 }
6a0067ea 52
88b1530a 53 $res;
6a0067ea 54 };
88b1530a 55 }
56}
57
9c1700e3 58use strict;
59use warnings;
60
61use Test::More;
62
63use File::Find;
64use File::Spec;
65use B qw/svref_2object/;
90cfe42b 66use Package::Stash;
9c1700e3 67
68# makes sure we can load at least something
69use DBIx::Class;
90cfe42b 70use DBIx::Class::Carp;
9c1700e3 71
72my @modules = grep {
73 my $mod = $_;
74
9c1700e3 75 # not all modules are loadable at all times
88b1530a 76 do {
77 # trap deprecation warnings and whatnot
78 local $SIG{__WARN__} = sub {};
79 eval "require $mod";
80 } ? $mod : do {
81 SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
82 (); # empty RV for @modules
9c1700e3 83 };
84
9c1700e3 85} find_modules();
86
87# have an exception table for old and/or weird code we are not sure
88# we *want* to clean in the first place
89my $skip_idx = { map { $_ => 1 } (
90 (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
91 'SQL::Translator::Producer::DBIx::Class::File', # ditto
92
93 # not sure how to handle type libraries
94 'DBIx::Class::Storage::DBI::Replicated::Types',
95 'DBIx::Class::Admin::Types',
96
97 # G::L::D is unclean, but we never inherit from it
98 'DBIx::Class::Admin::Descriptive',
99 'DBIx::Class::Admin::Usage',
cd122820 100
101 # this subclass is expected to inherit whatever crap comes
102 # from the parent
103 'DBIx::Class::ResultSet::Pager',
9c1700e3 104) };
105
106my $has_cmop = eval { require Class::MOP };
107
108# can't use Class::Inspector for the mundane parts as it does not
109# distinguish imports from anything else, what a crock of...
110# Class::MOP is not always available either - hence just do it ourselves
111
112my $seen; #inheritance means we will see the same method multiple times
113
114for my $mod (@modules) {
115 SKIP: {
116 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
117
90cfe42b 118 my %all_method_like = (map
119 { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
120 (reverse @{mro::get_linear_isa($mod)})
121 );
9c1700e3 122
123 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
124
125 my %roles;
126 if ($has_cmop and my $mc = Class::MOP::class_of($mod)) {
127 if ($mc->can('calculate_all_roles_with_inheritance')) {
128 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
129 }
130 }
131
132 for my $name (keys %all_method_like) {
133
e0b2dc74 134 next if ( DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN() and $name =~ /^carp(?:_unique|_once)?$/ );
90cfe42b 135
6a0067ea 136 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 137 next if $name =~ /^\(/;
138
139 my $gv = svref_2object($all_method_like{$name})->GV;
140 my $origin = $gv->STASH->NAME;
141
9c1700e3 142 TODO: {
143 local $TODO = 'CAG does not clean its BEGIN constants' if $name =~ /^__CAG_/;
70c28808 144 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
145 ? ''
146 : " (inherited by $mod)"
147 ));
9c1700e3 148 }
149
70c28808 150 next if $seen->{"${origin}:${name}"}++;
151
9c1700e3 152 if ($origin eq $mod) {
153 pass ("$name is a native $mod method");
154 }
155 elsif ($roles{$origin}) {
156 pass ("${mod}::${name} came from consumption of role $origin");
157 }
158 elsif ($parents{$origin}) {
159 pass ("${mod}::${name} came from proper parent-class $origin");
160 }
161 else {
162 my $via;
163 for (reverse @{mro::get_linear_isa($mod)} ) {
164 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
165 $via = $_;
166 last;
167 }
168 }
169 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
170 . ($via || 'UNKNOWN')
171 );
172 }
173 }
70c28808 174
e0b2dc74 175 next if DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN();
90cfe42b 176
70c28808 177 # some common import names (these should never ever be methods)
178 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
179 if ($mod->can($f)) {
180 my $via;
181 for (reverse @{mro::get_linear_isa($mod)} ) {
182 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
183 $via = $_;
184 last;
185 }
186 }
187 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
188 . ($via || 'UNKNOWN')
189 );
190 }
191 else {
192 pass ("Import $f not leaked into method list of $mod");
193 }
194 }
9c1700e3 195 }
196}
197
198sub find_modules {
199 my @modules;
200
201 find({
202 wanted => sub {
203 -f $_ or return;
204 s/\.pm$// or return;
205 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
206 push @modules, join ('::', File::Spec->splitdir($_));
207 },
208 no_chdir => 1,
209 }, (-e 'blib' ? 'blib' : 'lib') );
210
211 return sort @modules;
212}
213
214
215done_testing;