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