Commit | Line | Data |
9c1700e3 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | |
6 | use File::Find; |
7 | use File::Spec; |
8 | use B qw/svref_2object/; |
90cfe42b |
9 | use Package::Stash; |
9c1700e3 |
10 | |
11 | # makes sure we can load at least something |
12 | use DBIx::Class; |
90cfe42b |
13 | use DBIx::Class::Carp; |
9c1700e3 |
14 | |
15 | my @modules = grep { |
16 | my $mod = $_; |
17 | |
18 | # trap deprecation warnings and whatnot |
19 | local $SIG{__WARN__} = sub {}; |
20 | |
21 | # not all modules are loadable at all times |
22 | eval "require $mod" ? $mod : do { |
a1249d49 |
23 | SKIP: { skip "Failed require of $mod: $@", 1 }; |
9c1700e3 |
24 | (); |
25 | }; |
26 | |
27 | |
28 | } find_modules(); |
29 | |
30 | # have an exception table for old and/or weird code we are not sure |
31 | # we *want* to clean in the first place |
32 | my $skip_idx = { map { $_ => 1 } ( |
33 | (grep { /^DBIx::Class::CDBICompat/ } @modules), # too crufty to touch |
34 | 'SQL::Translator::Producer::DBIx::Class::File', # ditto |
35 | |
36 | # not sure how to handle type libraries |
37 | 'DBIx::Class::Storage::DBI::Replicated::Types', |
38 | 'DBIx::Class::Admin::Types', |
39 | |
40 | # G::L::D is unclean, but we never inherit from it |
41 | 'DBIx::Class::Admin::Descriptive', |
42 | 'DBIx::Class::Admin::Usage', |
43 | ) }; |
44 | |
45 | my $has_cmop = eval { require Class::MOP }; |
46 | |
47 | # can't use Class::Inspector for the mundane parts as it does not |
48 | # distinguish imports from anything else, what a crock of... |
49 | # Class::MOP is not always available either - hence just do it ourselves |
50 | |
51 | my $seen; #inheritance means we will see the same method multiple times |
52 | |
53 | for my $mod (@modules) { |
54 | SKIP: { |
55 | skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod}; |
56 | |
90cfe42b |
57 | my %all_method_like = (map |
58 | { %{Package::Stash->new($_)->get_all_symbols('CODE')} } |
59 | (reverse @{mro::get_linear_isa($mod)}) |
60 | ); |
9c1700e3 |
61 | |
62 | my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)}; |
63 | |
64 | my %roles; |
65 | if ($has_cmop and my $mc = Class::MOP::class_of($mod)) { |
66 | if ($mc->can('calculate_all_roles_with_inheritance')) { |
67 | $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance); |
68 | } |
69 | } |
70 | |
71 | for my $name (keys %all_method_like) { |
72 | |
e0b2dc74 |
73 | next if ( DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN() and $name =~ /^carp(?:_unique|_once)?$/ ); |
90cfe42b |
74 | |
9c1700e3 |
75 | # overload is a funky thing - it is neither cleaned, and its imports are named funny |
76 | next if $name =~ /^\(/; |
77 | |
78 | my $gv = svref_2object($all_method_like{$name})->GV; |
79 | my $origin = $gv->STASH->NAME; |
80 | |
9c1700e3 |
81 | TODO: { |
82 | local $TODO = 'CAG does not clean its BEGIN constants' if $name =~ /^__CAG_/; |
70c28808 |
83 | is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod |
84 | ? '' |
85 | : " (inherited by $mod)" |
86 | )); |
9c1700e3 |
87 | } |
88 | |
70c28808 |
89 | next if $seen->{"${origin}:${name}"}++; |
90 | |
9c1700e3 |
91 | if ($origin eq $mod) { |
92 | pass ("$name is a native $mod method"); |
93 | } |
94 | elsif ($roles{$origin}) { |
95 | pass ("${mod}::${name} came from consumption of role $origin"); |
96 | } |
97 | elsif ($parents{$origin}) { |
98 | pass ("${mod}::${name} came from proper parent-class $origin"); |
99 | } |
100 | else { |
101 | my $via; |
102 | for (reverse @{mro::get_linear_isa($mod)} ) { |
103 | if ( ($_->can($name)||'') eq $all_method_like{$name} ) { |
104 | $via = $_; |
105 | last; |
106 | } |
107 | } |
108 | fail ("${mod}::${name} appears to have entered inheritance chain by import into " |
109 | . ($via || 'UNKNOWN') |
110 | ); |
111 | } |
112 | } |
70c28808 |
113 | |
e0b2dc74 |
114 | next if DBIx::Class::_ENV_::BROKEN_NAMESPACE_CLEAN(); |
90cfe42b |
115 | |
70c28808 |
116 | # some common import names (these should never ever be methods) |
117 | for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) { |
118 | if ($mod->can($f)) { |
119 | my $via; |
120 | for (reverse @{mro::get_linear_isa($mod)} ) { |
121 | if ( ($_->can($f)||'') eq $all_method_like{$f} ) { |
122 | $via = $_; |
123 | last; |
124 | } |
125 | } |
126 | fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at " |
127 | . ($via || 'UNKNOWN') |
128 | ); |
129 | } |
130 | else { |
131 | pass ("Import $f not leaked into method list of $mod"); |
132 | } |
133 | } |
9c1700e3 |
134 | } |
135 | } |
136 | |
137 | sub find_modules { |
138 | my @modules; |
139 | |
140 | find({ |
141 | wanted => sub { |
142 | -f $_ or return; |
143 | s/\.pm$// or return; |
144 | s/^ (?: lib | blib . (?:lib|arch) ) . //x; |
145 | push @modules, join ('::', File::Spec->splitdir($_)); |
146 | }, |
147 | no_chdir => 1, |
148 | }, (-e 'blib' ? 'blib' : 'lib') ); |
149 | |
150 | return sort @modules; |
151 | } |
152 | |
153 | |
154 | done_testing; |