Fix last remaining tests with -T under < 5.10
[dbsrgits/DBIx-Class.git] / t / 55namespaces_cleaned.t
CommitLineData
88b1530a 1BEGIN {
2 if ($] < 5.010) {
6a0067ea 3
45638aed 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]->() };
6a0067ea 24 if ($@ ne '') {
45638aed 25 delete $INC{$_[1]};
6a0067ea 26 die $@;
88b1530a 27 }
45638aed 28 return $res;
29 } );
88b1530a 30 }
31}
32
9c1700e3 33use strict;
34use warnings;
35
21357344 36# FIXME This is a crock of shit, needs to go away
37# currently here to work around https://rt.cpan.org/Ticket/Display.html?id=74151
38# kill with fire when PS::XS / RT#74151 is *finally* fixed
39BEGIN {
40 my $PS_provider;
41
42 if ( "$]" < 5.010 ) {
43 require Package::Stash::PP;
44 $PS_provider = 'Package::Stash::PP';
45 }
46 else {
47 require Package::Stash;
48 $PS_provider = 'Package::Stash';
49 }
50 eval <<"EOS" or die $@;
51
52sub stash_for (\$) {
53 $PS_provider->new(\$_[0]);
54}
551;
56EOS
57}
58
9c1700e3 59use Test::More;
60
8d6b1478 61use lib 't/lib';
8d6b1478 62
70cf159f 63BEGIN {
64 require DBICTest::RunMode;
65 plan( skip_all => "Skipping test on plain module install" )
66 if DBICTest::RunMode->is_plain;
67}
68
69use DBICTest;
9c1700e3 70use File::Find;
71use File::Spec;
72use B qw/svref_2object/;
73
74# makes sure we can load at least something
75use DBIx::Class;
90cfe42b 76use DBIx::Class::Carp;
9c1700e3 77
78my @modules = grep {
f3ec358e 79 my ($mod) = $_ =~ /(.+)/;
9c1700e3 80
9c1700e3 81 # not all modules are loadable at all times
88b1530a 82 do {
83 # trap deprecation warnings and whatnot
84 local $SIG{__WARN__} = sub {};
85 eval "require $mod";
86 } ? $mod : do {
87 SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
88 (); # empty RV for @modules
9c1700e3 89 };
90
9c1700e3 91} find_modules();
92
93# have an exception table for old and/or weird code we are not sure
94# we *want* to clean in the first place
95my $skip_idx = { map { $_ => 1 } (
96 (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
97 'SQL::Translator::Producer::DBIx::Class::File', # ditto
98
99 # not sure how to handle type libraries
100 'DBIx::Class::Storage::DBI::Replicated::Types',
101 'DBIx::Class::Admin::Types',
102
103 # G::L::D is unclean, but we never inherit from it
104 'DBIx::Class::Admin::Descriptive',
105 'DBIx::Class::Admin::Usage',
cd122820 106
107 # this subclass is expected to inherit whatever crap comes
108 # from the parent
109 'DBIx::Class::ResultSet::Pager',
9f98c4b2 110
b1dbf716 111 # utility classes, not part of the inheritance chain
9f98c4b2 112 'DBIx::Class::ResultSource::RowParser::Util',
b1dbf716 113 'DBIx::Class::_Util',
9c1700e3 114) };
115
bfcecabc 116my $has_moose = eval { require Moose::Util };
9c1700e3 117
8d73fcd4 118Sub::Defer::undefer_all();
119
9c1700e3 120# can't use Class::Inspector for the mundane parts as it does not
121# distinguish imports from anything else, what a crock of...
bfcecabc 122# Moose is not always available either - hence just do it ourselves
9c1700e3 123
124my $seen; #inheritance means we will see the same method multiple times
125
126for my $mod (@modules) {
127 SKIP: {
128 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
129
90cfe42b 130 my %all_method_like = (map
21357344 131 { %{stash_for($_)->get_all_symbols('CODE')} }
90cfe42b 132 (reverse @{mro::get_linear_isa($mod)})
133 );
9c1700e3 134
135 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
136
137 my %roles;
bfcecabc 138 if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
9c1700e3 139 if ($mc->can('calculate_all_roles_with_inheritance')) {
140 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
141 }
142 }
143
144 for my $name (keys %all_method_like) {
145
6a0067ea 146 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 147 next if $name =~ /^\(/;
148
149 my $gv = svref_2object($all_method_like{$name})->GV;
150 my $origin = $gv->STASH->NAME;
151
8b4d8064 152 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
153 ? ''
154 : " (inherited by $mod)"
155 ));
9c1700e3 156
70c28808 157 next if $seen->{"${origin}:${name}"}++;
158
9c1700e3 159 if ($origin eq $mod) {
160 pass ("$name is a native $mod method");
161 }
162 elsif ($roles{$origin}) {
163 pass ("${mod}::${name} came from consumption of role $origin");
164 }
165 elsif ($parents{$origin}) {
166 pass ("${mod}::${name} came from proper parent-class $origin");
167 }
168 else {
169 my $via;
170 for (reverse @{mro::get_linear_isa($mod)} ) {
171 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
172 $via = $_;
173 last;
174 }
175 }
b5ce6748 176
177 # exception time
178 if (
179 ( $name eq 'import' and $via = 'Exporter' )
180 ) {
181 pass("${mod}::${name} is a valid uncleaned import from ${name}");
182 }
183 else {
184 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
185 . ($via || 'UNKNOWN')
186 );
187 }
9c1700e3 188 }
189 }
70c28808 190
191 # some common import names (these should never ever be methods)
192 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
193 if ($mod->can($f)) {
194 my $via;
195 for (reverse @{mro::get_linear_isa($mod)} ) {
196 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
197 $via = $_;
198 last;
199 }
200 }
201 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
202 . ($via || 'UNKNOWN')
203 );
204 }
205 else {
206 pass ("Import $f not leaked into method list of $mod");
207 }
208 }
9c1700e3 209 }
210}
211
212sub find_modules {
213 my @modules;
214
acc13b63 215 find( {
9c1700e3 216 wanted => sub {
217 -f $_ or return;
218 s/\.pm$// or return;
219 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
220 push @modules, join ('::', File::Spec->splitdir($_));
221 },
222 no_chdir => 1,
acc13b63 223 }, (
224 # find them in both lib and blib, duplicates are fine, since
225 # @INC is preadjusted for us by the harness
226 'lib',
227 ( -e 'blib' ? 'blib' : () ),
228 ));
9c1700e3 229
230 return sort @modules;
231}
232
9c1700e3 233done_testing;