Fix more $schema leaks in the SQLT DBIC Parser (AUGHHHHH!!!!)
[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
36use Test::More;
37
38use File::Find;
39use File::Spec;
40use B qw/svref_2object/;
90cfe42b 41use Package::Stash;
9c1700e3 42
43# makes sure we can load at least something
44use DBIx::Class;
90cfe42b 45use DBIx::Class::Carp;
9c1700e3 46
47my @modules = grep {
48 my $mod = $_;
49
9c1700e3 50 # not all modules are loadable at all times
88b1530a 51 do {
52 # trap deprecation warnings and whatnot
53 local $SIG{__WARN__} = sub {};
54 eval "require $mod";
55 } ? $mod : do {
56 SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
57 (); # empty RV for @modules
9c1700e3 58 };
59
9c1700e3 60} find_modules();
61
62# have an exception table for old and/or weird code we are not sure
63# we *want* to clean in the first place
64my $skip_idx = { map { $_ => 1 } (
65 (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch
66 'SQL::Translator::Producer::DBIx::Class::File', # ditto
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',
cd122820 75
76 # this subclass is expected to inherit whatever crap comes
77 # from the parent
78 'DBIx::Class::ResultSet::Pager',
9c1700e3 79) };
80
81my $has_cmop = eval { require Class::MOP };
82
83# can't use Class::Inspector for the mundane parts as it does not
84# distinguish imports from anything else, what a crock of...
85# Class::MOP is not always available either - hence just do it ourselves
86
87my $seen; #inheritance means we will see the same method multiple times
88
89for my $mod (@modules) {
90 SKIP: {
91 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
92
90cfe42b 93 my %all_method_like = (map
94 { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
95 (reverse @{mro::get_linear_isa($mod)})
96 );
9c1700e3 97
98 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
99
100 my %roles;
101 if ($has_cmop and my $mc = Class::MOP::class_of($mod)) {
102 if ($mc->can('calculate_all_roles_with_inheritance')) {
103 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
104 }
105 }
106
107 for my $name (keys %all_method_like) {
108
e0b2dc74 109 next if ( DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN() and $name =~ /^carp(?:_unique|_once)?$/ );
90cfe42b 110
6a0067ea 111 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 112 next if $name =~ /^\(/;
113
114 my $gv = svref_2object($all_method_like{$name})->GV;
115 my $origin = $gv->STASH->NAME;
116
9c1700e3 117 TODO: {
118 local $TODO = 'CAG does not clean its BEGIN constants' if $name =~ /^__CAG_/;
70c28808 119 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
120 ? ''
121 : " (inherited by $mod)"
122 ));
9c1700e3 123 }
124
70c28808 125 next if $seen->{"${origin}:${name}"}++;
126
9c1700e3 127 if ($origin eq $mod) {
128 pass ("$name is a native $mod method");
129 }
130 elsif ($roles{$origin}) {
131 pass ("${mod}::${name} came from consumption of role $origin");
132 }
133 elsif ($parents{$origin}) {
134 pass ("${mod}::${name} came from proper parent-class $origin");
135 }
136 else {
137 my $via;
138 for (reverse @{mro::get_linear_isa($mod)} ) {
139 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
140 $via = $_;
141 last;
142 }
143 }
144 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
145 . ($via || 'UNKNOWN')
146 );
147 }
148 }
70c28808 149
e0b2dc74 150 next if DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN();
90cfe42b 151
70c28808 152 # some common import names (these should never ever be methods)
153 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
154 if ($mod->can($f)) {
155 my $via;
156 for (reverse @{mro::get_linear_isa($mod)} ) {
157 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
158 $via = $_;
159 last;
160 }
161 }
162 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
163 . ($via || 'UNKNOWN')
164 );
165 }
166 else {
167 pass ("Import $f not leaked into method list of $mod");
168 }
169 }
9c1700e3 170 }
171}
172
173sub find_modules {
174 my @modules;
175
176 find({
177 wanted => sub {
178 -f $_ or return;
179 s/\.pm$// or return;
180 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
181 push @modules, join ('::', File::Spec->splitdir($_));
182 },
183 no_chdir => 1,
184 }, (-e 'blib' ? 'blib' : 'lib') );
185
186 return sort @modules;
187}
188
9c1700e3 189done_testing;