Rewire OptDeps to not attempt any module loads under missing envvars
[dbsrgits/DBIx-Class.git] / t / 00describe_environment.t
1 ###
2 ### This version is rather 5.8-centric, because DBIC itself is 5.8
3 ### It certainly can be rewritten to degrade well on 5.6
4 ###
5
6
7 BEGIN {
8   if ($] < 5.010) {
9
10     # Pre-5.10 perls pollute %INC on unsuccesfull module
11     # require, making it appear as if the module is already
12     # loaded on subsequent require()s
13     # Can't seem to find the exact RT/perldelta entry
14     #
15     # The reason we can't just use a sane, clean loader, is because
16     # if a Module require()s another module the %INC will still
17     # get filled with crap and we are back to square one. A global
18     # fix is really the only way for this test, as we try to load
19     # each available module separately, and have no control (nor
20     # knowledge) over their common dependencies.
21     #
22     # we want to do this here, in the very beginning, before even
23     # warnings/strict are loaded
24
25     unshift @INC, 't/lib';
26     require DBICTest::Util::OverrideRequire;
27
28     DBICTest::Util::OverrideRequire::override_global_require( sub {
29       my $res = eval { $_[0]->() };
30       if ($@ ne '') {
31         delete $INC{$_[1]};
32         die $@;
33       }
34       return $res;
35     } );
36   }
37 }
38
39 # Explicitly add 'lib' to the front of INC - this way we will
40 # know without ambiguity what was loaded from the local untar
41 # and what came from elsewhere
42 use lib qw(lib t/lib);
43
44 use strict;
45 use warnings;
46
47 use Test::More 'no_plan';
48 use Config;
49 use File::Find 'find';
50 use Module::Runtime 'module_notional_filename';
51 use List::Util 'max';
52 use ExtUtils::MakeMaker;
53 use DBICTest::Util 'visit_namespaces';
54
55 # load these two to pull in the t/lib armada
56 use DBICTest;
57 use DBICTest::Schema;
58
59 # do !!!NOT!!! use Module::Runtime's require_module - it breaks CORE::require
60 sub req_mod ($) {
61   # trap deprecation warnings and whatnot
62   local $SIG{__WARN__} = sub {};
63   local $@;
64   eval "require $_[0]";
65 }
66
67 sub say_err {
68   print STDERR "\n", @_, "\n";
69 }
70
71 my @lib_display_order = qw(
72   sitearch
73   sitelib
74   vendorarch
75   vendorlib
76   archlib
77   privlib
78 );
79 my $lib_paths = {
80   (map
81     { $Config{$_} ? ( $_ => $Config{"${_}exp"} || $Config{$_} ) : () }
82     @lib_display_order
83   ),
84
85   # synthetic, for display
86   './lib' => 'lib',
87 };
88
89 sub describe_fn {
90   my $fn = shift;
91
92   $lib_paths->{$_} and $fn =~ s/^\Q$lib_paths->{$_}/<<$_>>/ and last
93     for @lib_display_order;
94
95   $fn;
96 }
97
98 sub md5_of_fn {
99   # we already checked for -r/-f, just bail if can't open
100   open my $fh, '<:raw', $_[0] or return '';
101   require Digest::MD5;
102   Digest::MD5->new->addfile($fh)->hexdigest;
103 }
104
105 # first run through lib and *try* to load anything we can find
106 # within our own project
107 find({
108   wanted => sub {
109     -f $_ or return;
110
111     # can't just `require $fn`, as we need %INC to be
112     # populated properly
113     my ($mod) = $_ =~ /^ lib [\/\\] (.+) \.pm $/x
114       or return;
115
116     req_mod join ('::', File::Spec->splitdir($mod));
117   },
118   no_chdir => 1,
119 }, 'lib' );
120
121 # now run through OptDeps and attempt loading everything else
122 #
123 # some things needs to be sorted before other things
124 # positive - load first
125 # negative - load last
126 my $load_weights = {
127   # Make sure oracle is tried last - some clients (e.g. 10.2) have symbol
128   # clashes with libssl, and will segfault everything coming after them
129   "DBD::Oracle" => -999,
130 };
131 req_mod $_ for sort
132   { ($load_weights->{$b}||0) <=> ($load_weights->{$a}||0) }
133   keys %{
134     DBIx::Class::Optional::Dependencies->req_list_for([
135       grep
136         # some DBDs are notoriously problematic to load
137         # hence only show stuff based on test_rdbms which will
138         # take into account necessary ENVs
139         { $_ !~ /^rdbms_/ }
140         keys %{DBIx::Class::Optional::Dependencies->req_group_list}
141     ])
142   }
143 ;
144
145 my $has_versionpm = eval { require version };
146
147 # at this point we've loaded everything we ever could, let's drill through
148 # the *ENTIRE* symtable and build a map of versions
149 my $version_list = { perl => $] };
150 visit_namespaces( action => sub {
151   my $pkg = shift;
152
153   # keep going, but nothing to see here
154   return 1 if $pkg eq 'main';
155
156   # private - not interested
157   return 0 if $pkg =~ / (?: ^ | :: ) _ /x;
158
159   no strict 'refs';
160   # that would be some synthetic class, or a custom sub VERSION
161   return 1 if (
162     ! defined ${"${pkg}::VERSION"}
163       or
164     ${"${pkg}::VERSION"} =~ /\Qset by base.pm/
165   );
166
167   # make sure a version can be extracted, be noisy when it doesn't work
168   # do this even if we are throwing away the result below in lieu of EUMM
169   my $mod_ver = eval { $pkg->VERSION };
170   if (my $err = $@) {
171     $err =~ s/^/  /mg;
172     say_err
173       "Calling `$pkg->VERSION` resulted in an exception, which should never "
174     . "happen - please file a bug with the distribution containing $pkg. "
175     . "Follows the full text of the exception:\n\n$err\n"
176     ;
177   }
178   elsif( ! defined $mod_ver ) {
179     say_err
180       "Calling `$pkg->VERSION` returned 'undef', which should never "
181     . "happen - please file a bug with the distribution containing $pkg."
182     ;
183
184   }
185   elsif( ! length $mod_ver ) {
186     say_err
187       "Calling `$pkg->VERSION` returned the empty string '', which should never "
188     . "happen - please file a bug with the distribution containing $pkg."
189     ;
190     undef $mod_ver;
191   }
192
193   # if this is a real file - extract the version via EUMM whenever possible
194   my $fn = $INC{module_notional_filename($pkg)};
195
196   my $eumm_ver = eval { MM->parse_version( $fn ) }
197     if $fn and  -f $fn and -r $fn;
198
199   if (
200     $has_versionpm
201       and
202     defined $eumm_ver
203       and
204     defined $mod_ver
205       and
206     $eumm_ver ne $mod_ver
207       and
208     (
209       ( eval { version->parse( do { (my $v = $eumm_ver) =~ s/_//g; $v } ) } || 0 )
210         !=
211       ( eval { version->parse( do { (my $v = $mod_ver) =~ s/_//g; $v } ) } || 0 )
212     )
213   ) {
214     say_err
215       "Mismatch of versions '$mod_ver' and '$eumm_ver', obtained respectively "
216     . "via `$pkg->VERSION` and parsing the version out of @{[ describe_fn $fn ]} "
217     . "with ExtUtils::MakeMaker\@@{[ ExtUtils::MakeMaker->VERSION ]}. "
218     . "This should never happen - please check whether this is still present "
219     . "in the latest version, and then file a bug with the distribution "
220     . "containing $pkg."
221     ;
222   }
223
224   if( defined $eumm_ver ) {
225     $version_list->{$pkg} = $eumm_ver;
226   }
227   elsif( defined $mod_ver ) {
228     $version_list->{$pkg} = $mod_ver;
229   }
230
231   1;
232 });
233
234 # compress identical versions as close to the root as we can
235 # unless we are dealing with a smoker - in which case we want
236 # to see every MD5 there is
237 unless ( $ENV{AUTOMATED_TESTING} ) {
238   for my $mod ( sort { length($b) <=> length($a) } keys %$version_list ) {
239     my $parent = $mod;
240
241     while ( $parent =~ s/ :: (?: . (?! :: ) )+ $ //x ) {
242       $version_list->{$parent}
243         and
244       $version_list->{$parent} eq $version_list->{$mod}
245         and
246       ( ( delete $version_list->{$mod} ) or 1 )
247         and
248       last
249     }
250   }
251 }
252
253 ok 1, (scalar keys %$version_list) . " distinctly versioned modules";
254
255 # do not announce anything under ci - we are watching for STDERR silence
256 exit if DBICTest::RunMode->is_ci;
257
258 # sort stuff into @INC segments
259 my $segments;
260
261 MODULE:
262 for my $mod ( sort { lc($a) cmp lc($b) } keys %$version_list ) {
263   my $fn = $INC{module_notional_filename($mod)};
264
265   my $tuple = [
266     $mod,
267     ( ( $fn && -f $fn && -r $fn ) ? $fn : undef )
268   ];
269
270
271   if ($fn) {
272     for my $lib (@lib_display_order, './lib') {
273       if ( $lib_paths->{$lib} and index($fn, $lib_paths->{$lib}) == 0 ) {
274         push @{$segments->{$lib}}, $tuple;
275         next MODULE;
276       }
277     }
278   }
279
280   # fallthrough for anything without a physical filename, or unknown lib
281   push @{$segments->{''}}, $tuple;
282 }
283
284 # diag the result out
285 my $max_ver_len = max map { length $_ } values %$version_list;
286 my $max_mod_len = max map { length $_ } keys %$version_list;
287
288 my $diag = "\n\nVersions of all loadable modules within the configure/build/test/runtime dependency chains present on this system (both core and optional)\n\n";
289 for my $seg ( '', @lib_display_order, './lib' ) {
290   next unless $segments->{$seg};
291
292   $diag .= sprintf "=== %s ===\n\n",
293     $seg
294       ? "Modules found in " . ( $Config{$seg} ? "\$Config{$seg}" : $seg )
295       : 'Misc'
296   ;
297
298   $diag .= sprintf (
299     "   %*s  %*s%s\n",
300     $max_ver_len => $version_list->{$_->[0]},
301     -$max_mod_len => $_->[0],
302     ($_->[1]
303       ? "  [ MD5: @{[ md5_of_fn( $_->[1] ) ]} ]"
304       : ''
305     ),
306   ) for @{$segments->{$seg}};
307
308   $diag .= "\n\n"
309 }
310
311 diag $diag;