After 5e0eea35 we can actually test for cleaned namespaces within CDBI
[dbsrgits/DBIx-Class-Historic.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
38use Test::More;
39
70cf159f 40use DBICTest;
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
9c1700e3 63} find_modules();
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 } (
51ec0382 68 'SQL::Translator::Producer::DBIx::Class::File', # too crufty to touch
9c1700e3 69
70 # not sure how to handle type libraries
71 'DBIx::Class::Storage::DBI::Replicated::Types',
72 'DBIx::Class::Admin::Types',
73
74 # G::L::D is unclean, but we never inherit from it
75 'DBIx::Class::Admin::Descriptive',
76 'DBIx::Class::Admin::Usage',
cd122820 77
78 # this subclass is expected to inherit whatever crap comes
79 # from the parent
80 'DBIx::Class::ResultSet::Pager',
9f98c4b2 81
b1dbf716 82 # utility classes, not part of the inheritance chain
3f5e367a 83 'DBIx::Class::Optional::Dependencies',
9f98c4b2 84 'DBIx::Class::ResultSource::RowParser::Util',
b1dbf716 85 'DBIx::Class::_Util',
9c1700e3 86) };
87
bfcecabc 88my $has_moose = eval { require Moose::Util };
9c1700e3 89
8d73fcd4 90Sub::Defer::undefer_all();
91
9c1700e3 92my $seen; #inheritance means we will see the same method multiple times
93
94for my $mod (@modules) {
95 SKIP: {
96 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
97
90cfe42b 98 my %all_method_like = (map
99 { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
100 (reverse @{mro::get_linear_isa($mod)})
101 );
9c1700e3 102
103 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
104
105 my %roles;
bfcecabc 106 if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
9c1700e3 107 if ($mc->can('calculate_all_roles_with_inheritance')) {
108 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
109 }
110 }
111
112 for my $name (keys %all_method_like) {
113
6a0067ea 114 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 115 next if $name =~ /^\(/;
116
117 my $gv = svref_2object($all_method_like{$name})->GV;
118 my $origin = $gv->STASH->NAME;
119
8b4d8064 120 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
121 ? ''
122 : " (inherited by $mod)"
123 ));
9c1700e3 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 }
b5ce6748 144
145 # exception time
146 if (
147 ( $name eq 'import' and $via = 'Exporter' )
51ec0382 148 or
149 # jesus christ nobody had any idea how to design an interface back then
150 ( $name =~ /_trigger/ and $via = 'Class::Trigger' )
b5ce6748 151 ) {
152 pass("${mod}::${name} is a valid uncleaned import from ${name}");
153 }
154 else {
155 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
156 . ($via || 'UNKNOWN')
157 );
158 }
9c1700e3 159 }
160 }
70c28808 161
162 # some common import names (these should never ever be methods)
163 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
164 if ($mod->can($f)) {
165 my $via;
166 for (reverse @{mro::get_linear_isa($mod)} ) {
167 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
168 $via = $_;
169 last;
170 }
171 }
172 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
173 . ($via || 'UNKNOWN')
174 );
175 }
176 else {
177 pass ("Import $f not leaked into method list of $mod");
178 }
179 }
9c1700e3 180 }
181}
182
183sub find_modules {
184 my @modules;
185
35f4cebe 186 find( {
9c1700e3 187 wanted => sub {
188 -f $_ or return;
189 s/\.pm$// or return;
190 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
191 push @modules, join ('::', File::Spec->splitdir($_));
192 },
193 no_chdir => 1,
35f4cebe 194 }, (
195 # find them in both lib and blib, duplicates are fine, since
196 # @INC is preadjusted for us by the harness
197 'lib',
198 ( -e 'blib' ? 'blib' : () ),
199 ));
9c1700e3 200
201 return sort @modules;
202}
203
9c1700e3 204done_testing;