Overhaul populate code - fix \[] support and exotic values (arrays, etc.)
[dbsrgits/DBIx-Class.git] / t / 55namespaces_cleaned.t
CommitLineData
9c1700e3 1use strict;
2use warnings;
3
4use Test::More;
5
6use File::Find;
7use File::Spec;
8use B qw/svref_2object/;
90cfe42b 9use Package::Stash;
9c1700e3 10
11# makes sure we can load at least something
12use DBIx::Class;
90cfe42b 13use DBIx::Class::Carp;
9c1700e3 14
15my @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
32my $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',
90cfe42b 43
44 # exempt due to the __BROKEN_NC constant
45 'DBIx::Class::Carp',
9c1700e3 46) };
47
48my $has_cmop = eval { require Class::MOP };
49
50# can't use Class::Inspector for the mundane parts as it does not
51# distinguish imports from anything else, what a crock of...
52# Class::MOP is not always available either - hence just do it ourselves
53
54my $seen; #inheritance means we will see the same method multiple times
55
56for my $mod (@modules) {
57 SKIP: {
58 skip "$mod exempt from namespace checks",1 if $skip_idx->{$mod};
59
90cfe42b 60 my %all_method_like = (map
61 { %{Package::Stash->new($_)->get_all_symbols('CODE')} }
62 (reverse @{mro::get_linear_isa($mod)})
63 );
9c1700e3 64
65 my %parents = map { $_ => 1 } @{mro::get_linear_isa($mod)};
66
67 my %roles;
68 if ($has_cmop and my $mc = Class::MOP::class_of($mod)) {
69 if ($mc->can('calculate_all_roles_with_inheritance')) {
70 $roles{$_->name} = 1 for ($mc->calculate_all_roles_with_inheritance);
71 }
72 }
73
74 for my $name (keys %all_method_like) {
75
90cfe42b 76 next if ( DBIx::Class::Carp::__BROKEN_NC() and $name =~ /^carp(?:_unique|_once)?$/ );
77
9c1700e3 78 # overload is a funky thing - it is neither cleaned, and its imports are named funny
79 next if $name =~ /^\(/;
80
81 my $gv = svref_2object($all_method_like{$name})->GV;
82 my $origin = $gv->STASH->NAME;
83
9c1700e3 84 TODO: {
85 local $TODO = 'CAG does not clean its BEGIN constants' if $name =~ /^__CAG_/;
70c28808 86 is ($gv->NAME, $name, "Properly named $name method at $origin" . ($origin eq $mod
87 ? ''
88 : " (inherited by $mod)"
89 ));
9c1700e3 90 }
91
70c28808 92 next if $seen->{"${origin}:${name}"}++;
93
9c1700e3 94 if ($origin eq $mod) {
95 pass ("$name is a native $mod method");
96 }
97 elsif ($roles{$origin}) {
98 pass ("${mod}::${name} came from consumption of role $origin");
99 }
100 elsif ($parents{$origin}) {
101 pass ("${mod}::${name} came from proper parent-class $origin");
102 }
103 else {
104 my $via;
105 for (reverse @{mro::get_linear_isa($mod)} ) {
106 if ( ($_->can($name)||'') eq $all_method_like{$name} ) {
107 $via = $_;
108 last;
109 }
110 }
111 fail ("${mod}::${name} appears to have entered inheritance chain by import into "
112 . ($via || 'UNKNOWN')
113 );
114 }
115 }
70c28808 116
90cfe42b 117 next if DBIx::Class::Carp::__BROKEN_NC();
118
70c28808 119 # some common import names (these should never ever be methods)
120 for my $f (qw/carp carp_once carp_unique croak confess cluck try catch finally/) {
121 if ($mod->can($f)) {
122 my $via;
123 for (reverse @{mro::get_linear_isa($mod)} ) {
124 if ( ($_->can($f)||'') eq $all_method_like{$f} ) {
125 $via = $_;
126 last;
127 }
128 }
129 fail ("Import $f leaked into method list of ${mod}, appears to have entered inheritance chain at "
130 . ($via || 'UNKNOWN')
131 );
132 }
133 else {
134 pass ("Import $f not leaked into method list of $mod");
135 }
136 }
9c1700e3 137 }
138}
139
140sub find_modules {
141 my @modules;
142
143 find({
144 wanted => sub {
145 -f $_ or return;
146 s/\.pm$// or return;
147 s/^ (?: lib | blib . (?:lib|arch) ) . //x;
148 push @modules, join ('::', File::Spec->splitdir($_));
149 },
150 no_chdir => 1,
151 }, (-e 'blib' ? 'blib' : 'lib') );
152
153 return sort @modules;
154}
155
156
157done_testing;