Work around the FIXME in the previous commit
[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',
b1dbf716 83 'DBIx::Class::_Util',
9c1700e3 84) };
85
bfcecabc 86my $has_moose = eval { require Moose::Util };
9c1700e3 87
9c1700e3 88my $seen; #inheritance means we will see the same method multiple times
89
90for my $mod (@modules) {
91 SKIP: {
92 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
93
b090048f 94 my %all_method_like =
95 map
96 { $_->[0]{name} => $mod->can( $_->[0]{name} ) }
97 grep
98 { $_->[0]{via_class} ne 'UNIVERSAL' }
99 values %{ describe_class_methods($mod)->{methods} }
100 ;
9c1700e3 101
102 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
103
104 my %roles;
bfcecabc 105 if ($has_moose and my $mc = Moose::Util::find_meta($mod)) {
9c1700e3 106 if ($mc->can('calculate_all_roles_with_inheritance')) {
107 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
108 }
109 }
110
111 for my $name (keys %all_method_like) {
112
6a0067ea 113 # overload is a funky thing - it is not cleaned, and its imports are named funny
9c1700e3 114 next if $name =~ /^\(/;
115
86a432d4 116 my ($origin, $cv_name) = get_subname($all_method_like{$name});
9c1700e3 117
86a432d4 118 is ($cv_name, $name, "Properly named $name method at $origin" . ($origin eq $mod
8b4d8064 119 ? ''
120 : " (inherited by $mod)"
121 ));
9c1700e3 122
86a432d4 123 next if $seen->{"${origin}::${name}"}++;
70c28808 124
9c1700e3 125 if ($origin eq $mod) {
126 pass ("$name is a native $mod method");
127 }
128 elsif ($roles{$origin}) {
129 pass ("${mod}::${name} came from consumption of role $origin");
130 }
131 elsif ($parents{$origin}) {
132 pass ("${mod}::${name} came from proper parent-class $origin");
133 }
134 else {
135 my $via;
136 for (reverse @{mro::get_linear_isa($mod)} ) {
137 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
138 $via = $_;
139 last;
140 }
141 }
b5ce6748 142
143 # exception time
144 if (
145 ( $name eq 'import' and $via = 'Exporter' )
51ec0382 146 or
147 # jesus christ nobody had any idea how to design an interface back then
148 ( $name =~ /_trigger/ and $via = 'Class::Trigger' )
b5ce6748 149 ) {
150 pass("${mod}::${name} is a valid uncleaned import from ${name}");
151 }
152 else {
153 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
154 . ($via || 'UNKNOWN')
155 );
156 }
9c1700e3 157 }
158 }
70c28808 159
160 # some common import names (these should never ever be methods)
161 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
162 if ($mod->can($f)) {
163 my $via;
164 for (reverse @{mro::get_linear_isa($mod)} ) {
165 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
166 $via = $_;
167 last;
168 }
169 }
170 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
171 . ($via || 'UNKNOWN')
172 );
173 }
174 else {
175 pass ("Import $f not leaked into method list of $mod");
176 }
177 }
9c1700e3 178 }
179}
180
181sub find_modules {
182 my @modules;
183
35f4cebe 184 find( {
9c1700e3 185 wanted => sub {
186 -f $_ or return;
399b9455 187 $_ =~ m|lib/DBIx/Class/_TempExtlib| and return;
9c1700e3 188 s/\.pm$// or return;
189 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
5f0174dc 190 s/[\/\\]/::/g;
191 push @modules, ( $_ =~ /(.+)/ );
9c1700e3 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;