Merge the relationship resolution rework
[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
38use Test::More;
39
70cf159f 40use DBICTest;
9c1700e3 41use File::Find;
b090048f 42use DBIx::Class::_Util qw( get_subname describe_class_methods );
9c1700e3 43
44# makes sure we can load at least something
45use DBIx::Class;
90cfe42b 46use DBIx::Class::Carp;
9c1700e3 47
5f0174dc 48my @modules = grep {
49 my $mod = $_;
9c1700e3 50
9c1700e3 51 # not all modules are loadable at all times
88b1530a 52 do {
53 # trap deprecation warnings and whatnot
54 local $SIG{__WARN__} = sub {};
55 eval "require $mod";
56 } ? $mod : do {
57 SKIP: { skip "Failed require of $mod: " . ($@ =~ /^(.+?)$/m)[0], 1 };
58 (); # empty RV for @modules
9c1700e3 59 };
60
9c1700e3 61} find_modules();
62
63# have an exception table for old and/or weird code we are not sure
64# we *want* to clean in the first place
65my $skip_idx = { map { $_ => 1 } (
51ec0382 66 'SQL::Translator::Producer::DBIx::Class::File', # too crufty to touch
9c1700e3 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',
9f98c4b2 79
b1dbf716 80 # utility classes, not part of the inheritance chain
3f5e367a 81 'DBIx::Class::Optional::Dependencies',
9f98c4b2 82 'DBIx::Class::ResultSource::RowParser::Util',
616ca57f 83 'DBIx::Class::ResultSource::FromSpec::Util',
497d0451 84 'DBIx::Class::SQLMaker::Util',
b1dbf716 85 'DBIx::Class::_Util',
9c1700e3 86) };
87
bfcecabc 88my $has_moose = eval { require Moose::Util };
9c1700e3 89
9c1700e3 90my $seen; #inheritance means we will see the same method multiple times
91
92for my $mod (@modules) {
93 SKIP: {
94 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
95
b090048f 96 my %all_method_like =
97 map
98 { $_->[0]{name} => $mod->can( $_->[0]{name} ) }
99 grep
100 { $_->[0]{via_class} ne 'UNIVERSAL' }
101 values %{ describe_class_methods($mod)->{methods} }
102 ;
9c1700e3 103
104 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
105
106 my %roles;
bfcecabc 107 if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
9c1700e3 108 if ($mc->can('calculate_all_roles_with_inheritance')) {
109 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
110 }
111 }
112
113 for my $name (keys %all_method_like) {
114
6a0067ea 115 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 116 next if $name =~ /^\(/;
117
86a432d4 118 my ($origin, $cv_name) = get_subname($all_method_like{$name});
9c1700e3 119
86a432d4 120 is ($cv_name, $name, "Properly named $name method at $origin" . ($origin eq $mod
8b4d8064 121 ? ''
122 : " (inherited by $mod)"
123 ));
9c1700e3 124
86a432d4 125 next if $seen->{"${origin}::${name}"}++;
70c28808 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)
e2741c7f 163 for my $f (qw(
164 carp carp_once carp_unique croak confess cluck
165 try catch finally dbic_internal_try dbic_internal_catch
166 )) {
70c28808 167 if ($mod->can($f)) {
168 my $via;
169 for (reverse @{mro::get_linear_isa($mod)} ) {
170 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
171 $via = $_;
172 last;
173 }
174 }
175 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
176 . ($via || 'UNKNOWN')
177 );
178 }
179 else {
180 pass ("Import $f not leaked into method list of $mod");
181 }
182 }
9c1700e3 183 }
184}
185
186sub find_modules {
187 my @modules;
188
35f4cebe 189 find( {
9c1700e3 190 wanted => sub {
191 -f $_ or return;
399b9455 192 $_ =~ m|lib/DBIx/Class/_TempExtlib| and return;
9c1700e3 193 s/\.pm$// or return;
194 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
5f0174dc 195 s/[\/\\]/::/g;
196 push @modules, ( $_ =~ /(.+)/ );
9c1700e3 197 },
198 no_chdir => 1,
35f4cebe 199 }, (
200 # find them in both lib and blib, duplicates are fine, since
201 # @INC is preadjusted for us by the harness
202 'lib',
203 ( -e 'blib' ? 'blib' : () ),
204 ));
9c1700e3 205
206 return sort @modules;
207}
208
9c1700e3 209done_testing;