Add a get_subname to _Util
[dbsrgits/DBIx-Class.git] / xt / extra / internals / namespaces_cleaned.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
88b1530a 3BEGIN {
750a4ad2 4 if ( "$]" < 5.010) {
6a0067ea 5
45638aed 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
c0329273 21
45638aed 22 require DBICTest::Util::OverrideRequire;
23
24 DBICTest::Util::OverrideRequire::override_global_require( sub {
25 my $res = eval { $_[0]->() };
6a0067ea 26 if ($@ ne '') {
45638aed 27 delete $INC{$_[1]};
6a0067ea 28 die $@;
88b1530a 29 }
45638aed 30 return $res;
31 } );
88b1530a 32 }
33}
34
9c1700e3 35use strict;
36use warnings;
37
652d9b76 38# FIXME This is a crock of shit, needs to go away
39# currently here to work around https://rt.cpan.org/Ticket/Display.html?id=74151
40# kill with fire when PS::XS / RT#74151 is *finally* fixed
41BEGIN {
42 my $PS_provider;
43
44 if ( "$]" < 5.010 ) {
45 require Package::Stash::PP;
46 $PS_provider = 'Package::Stash::PP';
47 }
48 else {
49 require Package::Stash;
50 $PS_provider = 'Package::Stash';
51 }
52 eval <<"EOS" or die $@;
53
54sub stash_for (\$) {
55 $PS_provider->new(\$_[0]);
56}
571;
58EOS
59}
60
9c1700e3 61use Test::More;
62
70cf159f 63use DBICTest;
9c1700e3 64use File::Find;
65use File::Spec;
86a432d4 66use DBIx::Class::_Util 'get_subname';
9c1700e3 67
68# makes sure we can load at least something
69use DBIx::Class;
90cfe42b 70use DBIx::Class::Carp;
9c1700e3 71
72my @modules = grep {
f3ec358e 73 my ($mod) = $_ =~ /(.+)/;
9c1700e3 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 } (
51ec0382 90 'SQL::Translator::Producer::DBIx::Class::File', # too crufty to touch
9c1700e3 91
92 # not sure how to handle type libraries
93 'DBIx::Class::Storage::DBI::Replicated::Types',
94 'DBIx::Class::Admin::Types',
95
96 # G::L::D is unclean, but we never inherit from it
97 'DBIx::Class::Admin::Descriptive',
98 'DBIx::Class::Admin::Usage',
cd122820 99
100 # this subclass is expected to inherit whatever crap comes
101 # from the parent
102 'DBIx::Class::ResultSet::Pager',
9f98c4b2 103
b1dbf716 104 # utility classes, not part of the inheritance chain
3f5e367a 105 'DBIx::Class::Optional::Dependencies',
9f98c4b2 106 'DBIx::Class::ResultSource::RowParser::Util',
b1dbf716 107 'DBIx::Class::_Util',
9c1700e3 108) };
109
bfcecabc 110my $has_moose = eval { require Moose::Util };
9c1700e3 111
9c1700e3 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
652d9b76 119 { %{stash_for($_)->get_all_symbols('CODE')} }
90cfe42b 120 (reverse @{mro::get_linear_isa($mod)})
121 );
9c1700e3 122
123 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
124
125 my %roles;
bfcecabc 126 if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
9c1700e3 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
6a0067ea 134 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 135 next if $name =~ /^\(/;
136
86a432d4 137 my ($origin, $cv_name) = get_subname($all_method_like{$name});
9c1700e3 138
86a432d4 139 is ($cv_name, $name, "Properly named $name method at $origin" . ($origin eq $mod
8b4d8064 140 ? ''
141 : " (inherited by $mod)"
142 ));
9c1700e3 143
86a432d4 144 next if $seen->{"${origin}::${name}"}++;
70c28808 145
9c1700e3 146 if ($origin eq $mod) {
147 pass ("$name is a native $mod method");
148 }
149 elsif ($roles{$origin}) {
150 pass ("${mod}::${name} came from consumption of role $origin");
151 }
152 elsif ($parents{$origin}) {
153 pass ("${mod}::${name} came from proper parent-class $origin");
154 }
155 else {
156 my $via;
157 for (reverse @{mro::get_linear_isa($mod)} ) {
158 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
159 $via = $_;
160 last;
161 }
162 }
b5ce6748 163
164 # exception time
165 if (
166 ( $name eq 'import' and $via = 'Exporter' )
51ec0382 167 or
168 # jesus christ nobody had any idea how to design an interface back then
169 ( $name =~ /_trigger/ and $via = 'Class::Trigger' )
b5ce6748 170 ) {
171 pass("${mod}::${name} is a valid uncleaned import from ${name}");
172 }
173 else {
174 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
175 . ($via || 'UNKNOWN')
176 );
177 }
9c1700e3 178 }
179 }
70c28808 180
181 # some common import names (these should never ever be methods)
182 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
183 if ($mod->can($f)) {
184 my $via;
185 for (reverse @{mro::get_linear_isa($mod)} ) {
186 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
187 $via = $_;
188 last;
189 }
190 }
191 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
192 . ($via || 'UNKNOWN')
193 );
194 }
195 else {
196 pass ("Import $f not leaked into method list of $mod");
197 }
198 }
9c1700e3 199 }
200}
201
202sub find_modules {
203 my @modules;
204
35f4cebe 205 find( {
9c1700e3 206 wanted => sub {
207 -f $_ or return;
399b9455 208 $_ =~ m|lib/DBIx/Class/_TempExtlib| and return;
9c1700e3 209 s/\.pm$// or return;
210 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
211 push @modules, join ('::', File::Spec->splitdir($_));
212 },
213 no_chdir => 1,
35f4cebe 214 }, (
215 # find them in both lib and blib, duplicates are fine, since
216 # @INC is preadjusted for us by the harness
217 'lib',
218 ( -e 'blib' ? 'blib' : () ),
219 ));
9c1700e3 220
221 return sort @modules;
222}
223
9c1700e3 224done_testing;