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